Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
Problem/Motivation
Drupal's Twig implementation does not print anything for the following:
{% set var = 0 %}{{ var }}
This should print '0'.
Proposed resolution
Track down the bug and squash it. @Fabianx said that there is a check for (!$var) somewhere that should be refactored, and possibly we can check is_numeric().
Remaining tasks
TBD
User interface changes
n/a
API changes
n/a
Related Issues
#1757550: [Meta] Convert core theme functions to Twig templates
Comment | File | Size | Author |
---|---|---|---|
#14 | twig_null_vars-1970960-14.patch | 1.37 KB | alexrayu |
#12 | Twig_null_vars-1970960-12.patch | 1.69 KB | alexrayu |
#9 | Twig_null_vars-1970960-7.patch | 417 bytes | alexrayu |
#7 | Twig_null_vars-1970960-6.patch | 535 bytes | alexrayu |
#3 | Twig_null_vars-1970960-2.patch | 452 bytes | alexrayu |
Comments
Comment #1
alexrayu CreditAttribution: alexrayu commentedComment #2
alexrayu CreditAttribution: alexrayu commentedThe issue seems to reside in non-strict '==' in twig.engine@102. Making it strict '===' outputs var = 0.
Comment #3
alexrayu CreditAttribution: alexrayu commentedNeeds review.
Comment #5
Fabianx CreditAttribution: Fabianx commentedUnfortunately that is not the right fix.
The right fix is to add an if statement before to check for === 0.
Comment #6
Fabianx CreditAttribution: Fabianx commentedtagging
Comment #7
alexrayu CreditAttribution: alexrayu commentedYes, thank you!
Comment #9
alexrayu CreditAttribution: alexrayu commentedResubmitting
Comment #10
alexrayu CreditAttribution: alexrayu commentedComment #11
Fabianx CreditAttribution: Fabianx commentedLooks good to me.
Can we have some tests for this, please?
Comment #12
alexrayu CreditAttribution: alexrayu commented1. Rewrote the code, removed test for zero, but moved up the test for scalar values instead, so NULL-test does not intercept it.
2. Wrote a little test to test for NULL and scalar values. Test goes to twig_render() test.
Comment #13
Fabianx CreditAttribution: Fabianx commentedThanks for the tests.
> 1. Rewrote the code, removed test for zero, but moved up the test for scalar values instead, so NULL-test does not intercept it.
Uhm, the NULL check is there first for a reason.
A function call is very expensive in PHP and this is called very many times, so checking for 0 and then NULL is much faster, than calling is_scalar every time - even if the output is NULL or empty array.
So I would like to keep the behavior of #9
Thanks!
Comment #14
alexrayu CreditAttribution: alexrayu commentedReverted to initial approach ;)
Comment #15
Fabianx CreditAttribution: Fabianx commentedPasses tests, is a simple fix, does not make things slower, fixes a bug, has test coverage
=> RTBC
Comment #16
chx CreditAttribution: chx commentedUnrelated followup:
if ($arg == NULL)
is justif (!$arg)
.Comment #17
thedavidmeister CreditAttribution: thedavidmeister commentedThis patch just got merged into the patch at #1975462: Allow and test for NULL and integer 0 values in Twig templates.
Comment #17.0
thedavidmeister CreditAttribution: thedavidmeister commentedCode tags