We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



12.9k

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.

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 ?