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

Remove HTML comments from template (minifying) #1274

Open
Pique7 opened this issue Oct 11, 2023 · 9 comments
Open

Remove HTML comments from template (minifying) #1274

Pique7 opened this issue Oct 11, 2023 · 9 comments

Comments

@Pique7
Copy link

Pique7 commented Oct 11, 2023

Hello,
What would be an appropriate method to remove HTML comments from my templates when they are rendered?
I haven't found anything about it in the documentation.

I tried \Template::instance()->extend() but this does not work with HTML comments.

I also found View::afterrender() but I don't know whether might be helpful and how to utilize it. I am not that familiar with PHP and F3.

@httpneo
Copy link

httpneo commented Oct 11, 2023

Use the exclude Tags: <exclude><!-- HTML Comment --></exclude>

@geniuswebtools
Copy link

Here's the link to the documenation:
https://fatfreeframework.com/3.8/views-and-templates#ExclusionofSegments

@Pique7
Copy link
Author

Pique7 commented Oct 11, 2023

Oh, I really overlooked this in the documentation because I was focused on removing existing <!-- HTML comments -->.
I think for my case the provided solution is acceptable. I can replace/complement all my comments with the <exclude> tag.
Okay, thanks for your help!

@pauljherring
Copy link

Meanwhile, on the subject of minification, there is some functionality in core, but it's only for js and css:

@Pique7
Copy link
Author

Pique7 commented Oct 11, 2023

Meanwhile, on the subject of minification, there is some functionality in core, but it's only for js and css:

* https://fatfreeframework.com/3.8/optimization#KeepingJavascriptandCSSonaHealthyDiet

* https://fatfreeframework.com/3.8/web#minify

Yes, initially I hoped this could be an answer to my problem but ironically my project barely contains custom CSS and JavaScript (yet).
As already mentioned, for now the solution provided by @httpneo and @geniuswebtools seems to be suitable.

@dabcorp
Copy link

dabcorp commented Jul 29, 2024

Hello use afterrender :

\Template::instance()->afterrender(function($html) {
return Minify::MinifyHtml($html);
});

@GlassGruber
Copy link

return Minify::MinifyHtml($html);

@dabcorp Interesting where is that from?

@dabcorp
Copy link

dabcorp commented Sep 25, 2024

@GlassGruber , Minify is a class like this (simply) :

`<?php

// Définition de la classe Minify qui étend la classe de base Prefab
class Minify extends \Prefab
{
// Méthode statique pour minifier le HTML
public static function MinifyHtml(string $src): string
{
// Stocke temporairement les sections de script et de style pour les traiter séparément
$placeholders = [];

    // Capture et remplace les balises <script> par des placeholders tout en minifiant leur contenu JavaScript
    $src = preg_replace_callback('/(<script\b[^>]*>)(.*?)(<\/script>)/is', function ($matches) use (&$placeholders) {
        $placeholder = "<!--" . count($placeholders) . "-->";
        $minifiedJs = self::minifyJs($matches[2]);
        $placeholders[$placeholder] = $matches[1] . $minifiedJs . $matches[3];
        return $placeholder;
    }, $src);

    // Capture et remplace les balises <style> par des placeholders tout en minifiant leur contenu CSS
    $src = preg_replace_callback('/(<style\b[^>]*>)(.*?)(<\/style>)/is', function ($matches) use (&$placeholders) {
        $placeholder = "<!--" . count($placeholders) . "-->";
        $minifiedCss = self::minifyCss($matches[2]);
        $placeholders[$placeholder] = $matches[1] . $minifiedCss . $matches[3];
        return $placeholder;
    }, $src);

    // Définit un tableau de motifs regex et leurs remplacements pour nettoyer le HTML
    $patterns = [
        '/>\s+</' => '><', // Supprime les espaces blancs entre les balises HTML
        '/<\?php\s+/' => '<?php ', // Normalise les balises d'ouverture PHP
        "/\s+/" => ' ', // Réduit les espaces multiples en un seul espace
        "/\s+>/" => '>', // Supprime les espaces de fin avant les balises fermantes
        "/<\s+/" => '<', // Supprime les espaces de début après les balises ouvrantes
    ];

    // Applique tous les remplacements en une seule opération
    $src = preg_replace(array_keys($patterns), array_values($patterns), $src);

    // Réinsère les sections de script et de style
    foreach ($placeholders as $placeholder => $content) {
        $src = str_replace($placeholder, $content, $src);
    }

    // Retourne la source HTML minifiée
    return $src;
}

// Méthode privée pour minifier le CSS
private static function minifyCss($css) {
    $css = preg_replace('/\/\*.*?\*\/|\/\/.*(?=\n)/s', '', $css); // Supprime les commentaires CSS
    $css = preg_replace('/\s+/', ' ', $css); // Réduit les espaces multiples en un seul espace
    return trim($css);
}

// Méthode privée pour minifier le JavaScript
private static function minifyJs($js) {
	// Suppression des commentaires de bloc
	$js = preg_replace('/\/\*[\s\S]*?\*\//', '', $js);
	return trim($js);
}

}`

@GlassGruber
Copy link

Oh I see thank you! I thought I missed again something from the docs or similar!

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

6 participants