Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:6.1fromGromNaN:twig-warmup
Apr 1, 2022

Conversation

@GromNaN
Copy link
Member

@GromNaNGromNaN commentedMar 25, 2022
edited
Loading

QA
Branch?6.1
Bug fix?yes
New feature?no
Deprecations?no
Tickets-
LicenseMIT
Doc PR-

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.

front/├─ components/│  ├─ video/│  │  ├─ templates/│  │  │  ├─ video.html.twig│  │  ├─ styles/│  │  │  ├─ video.css│  │  ├─ scripts/│  │  │  ├─ video.js

This patch limit warmup to*.twig files.
This PR adds an option to restrict file pattern, for cache warmup and lint.

twig:file_name_pattern:"*.twig"
Before After
Time to generate25s9s
Storage80MB26MB
Number of files48001500

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), thesource() function must be used ; the cache is not used in this case.

Edit: Here is the documentationhttps://symfony.com/doc/current/reference/configuration/twig.html#file-name-pattern

yceruto reacted with thumbs up emojiKocal, kaznovac, and crtl reacted with heart emojikaznovac reacted with rocket emoji
@carsonbotcarsonbot added this to the4.4 milestoneMar 25, 2022
@carsonbotcarsonbot changed the title[TwigBundle] Pre-compile only *.twig files in cache warmup[TwigBundle]  Pre-compile only *.twig files in cache warmupMar 25, 2022
@fabpot
Copy link
Member

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).

@GromNaNGromNaN changed the base branch from4.4 to6.1March 26, 2022 09:42
@GromNaN
Copy link
MemberAuthor

GromNaN commentedMar 26, 2022
edited
Loading

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.twig extension?


The alternative in my code is to decorate the iterator and filter each template name.

class TemplateIteratorimplements \IteratorAggregate{publicfunction__construct(privateiterable$iterator) {}publicfunctiongetIterator():\Generator    {foreach ($this->iteratoras$filename) {if (str_ends_with($filename,'.twig')) {yield$filename;            }        }    }}

And use a compiler pass to inject the decorator in the cache warmer.

class TemplateIteratorPassimplements CompilerPassInterface{publicfunctionprocess(ContainerBuilder$container)    {$warmer =$container->getDefinition('twig.template_cache_warmer');$iterator =$container->register(TemplateIterator::class, TemplateIterator::class)->addArgument($warmer->getArgument(1));$warmer->setArgument(1,$iterator);    }}

@fabpot
Copy link
Member

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.twig extension?

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.

@GromNaN
Copy link
MemberAuthor

GromNaN commentedMar 26, 2022
edited
Loading

I added an option to theTwigBundle,the plan is to change its default value in 7.0:

twig:file_name_pattern:"*.twig"

The majority of projects having only*.twig files in the template directories, they will not have the deprecation notice and not need to update their configuration.

For projects that mix file extensions (like mine), the deprecation notice will be triggered and it will be easy to solve by adding the config.

@GromNaN
Copy link
MemberAuthor

GromNaN commentedMar 26, 2022
edited
Loading

Unfortunately, the deprecation is not reported in logs when runningbin/console cache:warmup. And there is very few chances the cache is warmed in tests. I've removed the deprecation to simply add the option. Updating the default value in 7.0 could be part of an other PR.

@GromNaN
Copy link
MemberAuthor

GromNaN commentedMar 26, 2022
edited
Loading

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.twig extension?

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.

In Symfony WebProfilerBundle itself, all svg are included as templates withinclude tag.

<h1>{{include('@WebProfiler/Icon/symfony.svg') }} Symfony <span>Profiler</span></h1>

Zurb templates uses thesource() function correctly.

{{source("@email/zurb_2/main.css") }}
{{source("@email/zurb_2/notification/local.css") }}

See#45858

@GromNaNGromNaN modified the milestones:4.4,6.1Mar 26, 2022
@GromNaN
Copy link
MemberAuthor

Thanks@yceruto. This proves this issue is common. Your PR was almost the same, with excluded directories in addition.

@fabpotpresented the argument thattemplates directory is for templates only, but experience shows that grouping files by their type is not optimal for maintenance. A twig loader namespace can target a directory with severaltemplates directories.

yceruto reacted with thumbs up emoji

@fabpot
Copy link
Member

Thank you@GromNaN.

@fabpotfabpot merged commit8075e1d intosymfony:6.1Apr 1, 2022
@GromNaNGromNaN deleted the twig-warmup branchApril 1, 2022 06:46
GromNaN added a commit that referenced this pull requestApr 4, 2022
…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
@fabpotfabpot mentioned this pull requestApr 15, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot approved these changes

@ycerutoycerutoAwaiting requested review from ycerutoyceruto is a code owner

+1 more reviewer

@stloydstloydstloyd left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

6.1

Development

Successfully merging this pull request may close these issues.

5 participants

@GromNaN@fabpot@yceruto@stloyd@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp