12

I see lots of phtml files generated in var/view_preprocessed/html, from core modules as well as from custom modules. It looks like these are all used templates with stripped whitespace.

Alan Kent wrote in How do I update Magento 2 phtml files in Grunt?:

You don't "deploy" PHTML files. They are referenced by blocks in layout files and processed on the server side. So "grunt" is not relevant here, and the static content deployment is similarly not relevant.

And when analyzing the static content deployment and grunt workflow I did not find anything related to templates, so I guess this still holds true.

But when are these files generated? And what is actually happening there and why?

1
  • As I figured out, these files will be generated with static-content:deploy command. For some reason you have to delete all files in var/view_preprocessed to get your changes work. Commented Mar 6, 2016 at 17:04

1 Answer 1

12
+100

When calling bin/magento static-content:deploy this gets called: \Magento\Deploy\Console\Command\DeployStaticContentCommand::execute()

This method calls

    $deployer = $this->objectManager->create(
        'Magento\Deploy\Model\Deployer',
        ['filesUtil' => $filesUtil, 'output' => $output, 'isDryRun' => $options[self::DRY_RUN_OPTION]]
    );
    $deployer->deploy($this->objectManagerFactory, $languages);

which translates to \Magento\Deploy\Model\Deployer::deploy. At the bottom of this method you will find

   foreach ($this->filesUtil->getPhtmlFiles(false, false) as $template) {
        $this->htmlMinifier->minify($template);
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
            $this->output->writeln($template . " minified\n");
        } else {
            $this->output->write('.');
        }
        $this->count++;
    }

If you want you can dig deeper in \Magento\Framework\App\Utility::getPhtmlFiles to see how the templates are retrieved.
Short version:
It calls accumulateModuleTemplateFiles and accumulateThemeTemplateFiles from the same class. These methods are using (in the end) glob and some regular expressions to retrieve all the template files from the modules and from the themes and minifying is done by \Magento\Framework\View\Template\Html\Minifier (that implements \Magento\Framework\View\Template\Html\MinifierInterface) based on some strange regular expressions. (see minify method).
The thing I don't know yet, it how are these templates loaded for use. most probably they are loaded by the template engine. I will post back here if/when I find that.

[EDIT]
I found when the minified templates are used. When you set the value for Store->Configuration->Developer->Template Settings->Minify HTML to Yes and when on production mode.

1
  • Thanks for the answer. I have found that Store->Configuration->Developer->Template Settings->Minify HTML it is still not loading Minified HTML. Is there anything that can be blocking this? Does this only work in production mode? Commented Apr 2, 2017 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.