Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML Minification #853

Open
taufik-nurrohman opened this issue Jan 15, 2025 · 0 comments
Open

HTML Minification #853

taufik-nurrohman opened this issue Jan 15, 2025 · 0 comments

Comments

@taufik-nurrohman
Copy link

Hello! 👋 I’m currently exploring PHP-based content management systems to assist in implementing my PHP minification project to them. It seems HTMLy doesn’t have a plugin feature, even using Composer to add my project as a dependency doesn’t seem to be that seamless to do. It seems the only way to implement it in HTMLy is to do it manually.

Here are the steps you can do to dynamically minify the HTMLy layout using my project. I assume you already have a live HTMLy site.

Step 1

Go to the .\system\includes\ folder, then install my project as .\system\includes\minify\. You can use Git to install it, or just download it manually and then put it in the folder. Example when using Git to install my project:

cd system/includes
git clone https://github.com/taufik-nurrohman/minify.git --depth 1

Step 2

Go to .\system\includes\dispatch.php file the open it with your code editor.

Right after line 4, add this line:

require __DIR__ . '/minify/index.php';

On line 426, wrap the ob_get_contents() part with my minification function like this:

file_put_contents($cachefile, x\minify\h_t_m_l(ob_get_contents()), LOCK_EX);

On line 428, wrap the trim(ob_get_clean()) part with my minification function like this:

echo x\minify\h_t_m_l(trim(ob_get_clean()));

Step 3

Clear cache, then visit the site! Have a look at the source code to see that your site is now being minified by my project implementations 😉

Note

The HTML minifier will generally remove all white-spaces between HTML tags, but in some cases it will leave a space between two nodes, such as when an <img> element is preceded by text and a space, or followed by a space and text. You may experience some undesirable results like this, but it is not really a bug:

Screenshot 2025-01-15 at 09-41-29 Blog Post

Compare the part of it where I have marked it in red and in green. The solution to avoid this problem is to make sure that each part of the separation between the PHP code and the inline HTML code is preceded or followed by a space.

If you want to keep a space, then place the opening PHP tag right after the inline HTML code or the closing PHP tag right before the inline HTML code:

Before

<strong>asdf:</strong>
<?php

// asdf
echo $asdf;

?>
asdf asdf

After

<strong>asdf:</strong> <?php

// asdf
echo $asdf;

?> asdf asdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant