Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Volt to remove HTML comments

Is there a way to configure Volt in order to automatically remove all HTML comments from the final HTML output?

Example:

test.volt:

   {# test.volt #}
 <!-- I am an HTML comment -->
 <div class="my-class">
     {{ this_is_a_template_variable }}
 </div>

Final HTML:

   <div class="my-class">
     Value of the template variable.
 </div>

Thank you

Well i dont know about it. But Pagespeed module from google for Nginx/Apache can do such a stuff and many more.



44.7k

Use Volt comment blocks: {# blah blah #}



3.4k

Use Volt comment blocks: {# blah blah #}

I'm sorry but I don't think this is a valid answer. Many situations could require an HTML comment instead of a Volt one. My question was how to configure Volt in order to delete all HTML comments when compiling. If it's not possible, tell me it isn't.

Well yea, also there are much more options, scaling images, converting them to webp, lazy loading them, minimize javascript and much much more.



44.7k
edited Dec '15
{% if DEV %}
  <!-- HTML comment -->
{% endif %}
edited Dec '15

How about adding function :

$dependencyInjector->set('view', function () use ($dependencyInjector,$config) {

    $view = new View();

    $view->registerEngines(array(
        ".volt" => function ($view) use ($dependencyInjector,$config) {
            $volt = new Volt($view);
            $compiler = $volt->getCompiler();
            $compiler->addFunction('html_comment',function($resolvedArgs,$exprArgs) use ($dependencyInjector,$config) {
                if ($dependencyInjector->get('request')->getHttpHost() == $config->yourDevHost) {
                    return "<!-- $resolvedArgs -->";
                }
                return null;
            });
            return $volt;
        }
    ));


    return $view;
});

And then in view:

{{ html_comment('something') }} ?

Im not sure it will work tho cuz i didnt check it. Also instead of this nasty if much better it will be just on put this to constant.

Also we dont really know why you need it. You just want to remove all comments or maybe let them be on DEV and remove on PROD ?