How PHTML Templates are Generated in view_preprocessed – Magento 2

code generationmagento-2.0.2magento2template

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?

Best Answer

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.

Related Topic