07.
The for "loop" and inline "if" Syntax
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
9 Comments
We could also to use <a href="http://twig.sensiolabs.org/doc/filters/default.html">default</a> filter instead of
is definedtest, for example:This is more readable and elegant solution, imo.
You're absolutely right :) - I like using default like this too.
Hello beautiful people. I wonder why in the if statement that is just before the for loop we put the closing div tag before the div open tag class row. Thanks for your answers.
`
{% if loop.index is divisibleby(3) %}
`
Hey Beniamin T.
That's because we have to close the first "row" element before adding another row element into the layout. But it looks weird, I know :)
Cheers!
Why divisible by over batch filter ?
Hey Dylan Delobel ☕!
Great question :). And I don't have a good reason - the batch filter is great! Actually, when we created this tutorial, the batch filter either didn't exist or was quite new. So, it's an excellent tool.
Cheers!
Is the syntax of the "divisible by" test `divisible by` or `divisibleby` ?
(i.e. does it matter if there is a space between the words?)
The dox and the example don't match...
Hey Goz,
Yes, it does matter! Well, depends on Twig version. In Twig 2.x only "divisible by()" works, but in 1.x both syntax were allowed, but "divisible by()" were added as an alias since 1.14.2. See related docs:
https://twig.symfony.com/do...
https://twig.symfony.com/do...
Cheers!
Got it - thanks!
"Houston: no signs of life"
Start the conversation!