-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBundle] Pre-compile only *.twig files in cache warmup #45845
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
Conversation
I'm not sure about this change, but this can only be merged in 6.1 anyway as this is a behavior change and not a bug fix (we always consider performance improvements as a new feature). |
Ok for 6.1. The question to answer before merging this PR is: is it expected to have twig templates that does not have the The alternative in my code is to decorate the iterator and filter each template name. class TemplateIterator implements \IteratorAggregate
{
public function __construct(private iterable $iterator) {}
public function getIterator(): \Generator
{
foreach ($this->iterator as $filename) {
if (str_ends_with($filename, '.twig')) {
yield $filename;
}
}
}
} And use a compiler pass to inject the decorator in the cache warmer. class TemplateIteratorPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$warmer = $container->getDefinition('twig.template_cache_warmer');
$iterator = $container->register(TemplateIterator::class, TemplateIterator::class)->addArgument($warmer->getArgument(1));
$warmer->setArgument(1, $iterator);
}
} |
I think that this is indeed a great question. And the answer is: you can have templates with another extension. It's probably not widespread but nothing forbids it. |
I added an option to the twig:
file_name_pattern: "*.twig"
|
Unfortunately, the deprecation is not reported in logs when running |
src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php
Outdated
Show resolved
Hide resolved
In Symfony WebProfilerBundle itself, all svg are included as templates with symfony/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/header.html.twig Line 3 in 24304ad
symfony/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig Line 150 in 24304ad
Zurb templates uses the symfony/src/Symfony/Bridge/Twig/Resources/views/Email/zurb_2/notification/body.html.twig Lines 6 to 7 in 24304ad
See #45858 |
Thanks @yceruto. This proves this issue is common. Your PR was almost the same, with excluded directories in addition. @fabpot presented the argument that |
Thank you @GromNaN. |
…colas-grekas) This PR was merged into the 6.1 branch. Discussion ---------- [TwigBridge] Make LintCommand::$namePatterns private | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Dunno why this wasn't done in #45845. /cc `@GromNaN` any idea? Commits ------- 627a296 [TwigBridge] Make LintCommand::$namePatterns private
In my project, the template directory contains all the frontend assets (CSS, JS and images), this is a choice to locate all component files in the same directories. But the asset files are compiled as if they were twig template, which is a waste of time and storage.
This patch limit warmup to*.twig
files.This PR adds an option to restrict file pattern, for cache warmup and lint.
The filter on
*.twig
files is already in thelint:twig
command.Note: If a non-twig template is included in a template (ex: to inline CSS), the
source()
function must be used ; the cache is not used in this case.Edit: Here is the documentation https://symfony.com/doc/current/reference/configuration/twig.html#file-name-pattern