Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Follow-up to #2447049: Add a render filter to twig
Problem/Motivation
We have {{ render_var(foo) }}
, but {{ render(foo) }}
would be so much nicer for the use cases that make sense (more like set tags or other use cases, {{ render_var(foo) }} is the same as {{ foo }}).
Proposed resolution
Add render
in addition to render_var
. Keep render_var
for BC.
Remaining tasks
Patch- Review
User interface changes
N/A
API changes
New function available in Twig templates: render
Beta phase evaluation
Issue category | Task, small functional addition |
---|---|
Issue priority | Normal, helps with some use cases |
Prioritized changes | This is not a prioritized change for the beta phase. |
Disruption | Not disruptive at all for core or contrib, but potentially disruptive for Symfony applications that use Drupal 8 services as there is a namespace conflict on 'render'. |
Comment | File | Size | Author |
---|---|---|---|
#9 | interdiff.txt | 3.12 KB | star-szr |
#9 | 2455233-8.patch | 2.65 KB | star-szr |
Comments
Comment #1
star-szrWorking on this, by the way :)
Comment #2
joelpittetLooks good.
Comment #3
joelpittetJust kidding, making vuvuzela
Comment #4
star-szrPatch!
Edit: By the way I don't know if it's cool to use t() in tests or not.
Comment #5
star-szrtwig_theme_test dependency no longer needed for the test, in an earlier version I did a whole hook_theme() and template and everything.
Comment #6
joelpittetOk for real this time;)
Has tests
Should the filters be quoted or something in the docs to distinguish them from other words?
Who left the dogs out?
Comment #7
joelpittetThe answer is actually don't put the t function in here because we aren't testing it:) Minimum viable code to test the feature.
Comment #9
star-szrIncorporates #6 and #7, thanks for the reviews :)
Comment #10
joelpittetThat looks like the ticket. Has tests, tests only fails in #1 as it should, very simple addition for TX/DX. Beta evaluation is in the Issue Summary.
Comment #11
Fabianx CreditAttribution: Fabianx commentedJust food for thought, the reason render was changed to render_var, was that Symfony already has a 'render' function:
http://symfony.com/doc/current/reference/twig_reference.html#functions
So we have a name clash here. As the symfony thing is not a filter, it is less of a nameclash.
Maybe we need to use drupal_render? (which is known from D7 at least) as name?
Comment #12
Fabianx CreditAttribution: Fabianx commentedComment #13
star-szrThanks for that @Fabianx, didn't know about that. For others that didn't know, the Symfony
render
is used to embed controllers: http://symfony.com/doc/current/reference/twig_reference.html#render. I'm also excavating a related issue to this discussion that talks about a use case for a Symfony-esquerender
in Drupal-land as a data point.Personally I think it's OK for us to have our own render function in Twig because so far our rule has been not to change too much from the upstream Twig library (only additive whenever possible in our TwigExtension) but Symfony's Twig bundle is not upstream, and I think already our
url
and potentially others are already different than Symfony's.One of the reasons I like
render
is people might think to use it based on their D7 templates, and I don't think we can ignore that fact.drupal_render
is in my experience not known by themers that live in templates all day, butrender
is known.We already have
|render
in so consistency should rule and since the naming conflict is not in Twig itself but in Symfony's Twig bundle, I'm fine with going ahead here, so tentatively setting back to RTBC.Just as a note, when working on this issue I discovered Twig does have a useful fallback when you call a nonexistent function:
(You can see that by adding
{{ render(content) }}
to your active theme's page.html.twig.Comment #14
Fabianx CreditAttribution: Fabianx commentedAssigning to alexpott for core committer feedback:
#1873442: Refactored core Twig integration renamed render to render_var for this exact reason.
If we rename it back now its a regression to our Symfony integration and while I am not a fan (for various reasons), I don't think its good to undo a commit of the opposite effect.
Comment #15
star-szrBasically if this won't fly, we should still add the tests and probably rename the just-added render filter to render_var as well IMO.
I don't really understand the practicalities of the Symfony "integration".
Comment #16
alexpottWhy do we have the expectation that our twig functions and filters won't clash with https://github.com/symfony/TwigBridge? If we do have that expectation things seem really fragile.
It seems even
render_var()
is a namespace clash given:From https://github.com/symfony/TwigBridge/blob/e848d30c148ceb0a0dc573bba383d...
I talked to @Fabianx about this in IRC - he said:
I not sure I understand what this means and whether or not this is a reasonable expectation.
Comment #17
Fabianx CreditAttribution: Fabianx commentedThis fine to go in, symfony should use render_esi, render_[etc.] instead when they need it or provide a symfony drupal bridge to use symfonyRender instead.
As there are ways of symfony projects using twig bridge to circumvent this and as usually a strategy is chosen, this is fine to go in.
Comment #18
alexpottI still feel odd about reverting a change that the lead developer of twig implemented.
Comment #19
Fabianx CreditAttribution: Fabianx commentedSo some more background:
The main concern was especially about the DrupalTwigExtension in regards to the automatic adding of passing all renderable output through 'twig_render_var'.
Because the NodeVisitor would also run for Symfony integrated code, it would have called the Symfony 'render' then (if the extension was registered afterwards).
Also this was at a time where the function was registered always in twig and not in a pluggable extension, so maybe Symfony now does not load the twig extension at all anymore ...
However in this patch 'render' is only added as a convenience / consistency function, so it would at least be possible to override all contrib/ custom/ themes templates with one that has no 'render' function in it.
Given our coding standards advise to use the filter |render, this is likely not too big a problem either.
Edit:
So this is not actually reverting the original change, but adding a conflicting function name back into our namespace.
Comment #20
alexpottI'm leaning towards closing this as won't fix - but before I do I want to talk with @Cottser on IRC.
Comment #21
Fabianx CreditAttribution: Fabianx commentedComment #22
star-szrHad a quick chat with @alexpott in IRC, let's postpone for now. The Twig parser will tell people about render_var (#13), it's not a super nice name but at least we avoid conflicts and see how this shakes out. Potentially we could add this in a later release.
Comment #23
mgiffordWhat is this postponed on? It isn't clear to me from @Cottser's message.
Comment #24
joelpittetComment #37
bardiuk CreditAttribution: bardiuk commentedHere is the documentation of render_var function in case you came here from Google.