Minify HTML di CodeIgniter menggunakan Hooks

Jika Anda mencari cara meminimalkan HTML di Codeigniter? Maka tutorial ini adalah untuk Anda.

Sebelum memulai, mari kita bahas secara singkat tentang Minifikasi.

Apa itu HTML Minification

Istilah “minify” dalam bahasa pemrograman, Ini menggambarkan proses menghapus karakter yang tidak perlu dalam kode sumber. Karakter-karakter ini termasuk spasi putih, jeda baris, komentar, dan pembatas blok yang berguna untuk pengembang tetapi tidak perlu untuk mesin dan itu tidak mempengaruhi output tetapi juga meningkatkan kinerja situs web. Kami memperkecil file situs web yang berisi CSS, HTML, dan kode Javascript sehingga browser web Anda dapat membacanya dengan lebih cepat.

Contoh HTML tanpa Minify

<html>
<head>
<title>Your Title Here</title>
</head>
<body>
<h1>Sample HTML without Minification</h1>
<p>The term "minify" in programming language, It's describes the processes of removing unnecessary characters in the source code. These characters include whitespaces, line breaks, comments, and block delimiters which are useful for developer but unnecessary for machines and it's not affecting the output but also improving the performance of the website.</p>
<p>We minify the files of a website containing CSS, HTML, and Javascript code so your web browser can read them faster.</p>
</body>
</html>

HTML Setelah di Minify

<html><head><title>Your Title Here</title></head><body><h1>What is HTML Minification</h1><p>The term "minify" in programming language, It's describes the processes of removing unnecessary characters in the source code. These characters include whitespaces, line breaks, comments, and block delimiters which are useful for developer but unnecessary for machines and it's not affecting the output but also improving the performance of the website.</p><p>We minify the files of a website containing CSS, HTML, and Javascript code so your web browser can read them faster.</p></body></html>

Cara Meminimalkan HTML di Codeigniter Langkah demi Langkah

Langkah 1: Aktifkan Hooks di Config

Buka application/config/ config.php dan pastikan di dalam file config.php $ config [‘enable_hooks’] pada posisi TRUE.

$config['enable_hooks'] = TRUE;

Langkah 2: Ubah file hooks.php

Buka folder application /config/hooks.php dan ubah seperti dibawah ini :

// compress output
$hook['display_override'][] = array(
'class' => '',
'function' => 'compress',
'filename' => 'compress.php',
'filepath' => 'hooks'
);

Langkah 3: Tambahkan file compress.php

buat sebuah file dengan compress.php di dalam aplikasi/hooks/ Folder dengan kode berikut:

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function compress()
{
$CI =& get_instance();
$buffer = $CI->output->get_output();
$search = array(
'/\n/',      // replace end of line by a space
'/\>[^\S ]+/s',    // strip whitespaces after tags, except space
'/[^\S ]+\</s',    // strip whitespaces before tags, except space
'/(\s)+/s'    // shorten multiple whitespace sequences
);
$replace = array(
' ',
'>',
'<',
'\1'
);
$buffer = preg_replace($search, $replace, $buffer);
$CI->output->set_output($buffer);
$CI->output->_display();
}
?>

Kode minify HTML siap Anda dapat memeriksa Output dari kode HTML akan minify Anda dapat menjalankan halaman aplikasi Anda dan memeriksa kode sumber HTML dengan CTR + U.

Baca Juga : Sistem informasi HRD