I have the following:
{% if promo.monday_unavailable == 1 %}
not available mondays
{% elseif promo.monday_available == 1%}
available mondays
{% else %}
available mondays from {{promo.monday_start}} until {{promo.monday_end}}
{% endif %}
<br />
{% if promo.tuesday_unavailable == 1 %}
not available tuesdays
{% elseif promo.tuesday_available == 1%}
available tuesdays
{% else %}
available tuesdays from {{promo.tuesday_start}} until {{promo.tuesday_end}}
{% endif %}
<br />
...
That I would like to do for each day of the week.
I'm wondering if there is a way I can simplify the code to read
{% for i in ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'] %}
{% if promo.~i~"_unavailable" == 1 %}
not available mondays
{% elseif promo.~i~"_available" == 1%}
available mondays
{% else %}
available mondays from {{promo.~i~"_start"}} until {{promo.~i~"_end"}}
{% endif %}
<br />
{% endfor %}
With Twig.
Any help would be appreciated. I'm at a loss for what keywords to search for anymore.
I know this is an old thread but twig has support for inline interpolation like:
{{i18n("language_#{langId}")}}
Important that the string to interpolated is with double-quotes.
Found answer by mashing my forehead on the keyboard.
rather than
{% if promo.~i~"_unavailable" == 1 %}
use
{% promo[i~"_unavailable"] == 1 %)
You can try using my code
{% for i in ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'] %}
{% set key = i ~ '__unavailable' %}
{% if (promo[key]) eq something %}
//
{% endif %}
{% endfor}
Related
I am writing a unit test with Codeception to test a twig template. However, I am unable to find a way to mock the block.richTextFields.all() function. The block.richTextField is passed by the Twig context in this case so my initial thought was to create TwigFunction to mock it, but I can't find a way to have all function inside block.richTextField namespace.
{% for text in block.richTextFields.all() %}
{% if (loop.length < 3 and loop.length > 1 ) or (loop.length == 4) or (loop.length < 3 and loop.index == 2) or (loop.length < 6 and (loop.index == 4 or loop.index == 5)) %}
{% set topRowColWidth = "col-lg-6 margin-top-10" %}
{% elseif loop.length == 1 and loop.last %}
{% set topRowColWidth = "col-lg-12 margin-top-10" %}
{% else %}
{% set topRowColWidth = "col-lg-4" %}
{% endif %}
<div class="{{ topRowColWidth }} dynamic-rich-text">
{% if text.richTextField|length %}
{{text.richTextField}}
{% endif %}
</div>
{% endfor %}
I am using Twig and Timber for a WordPress project. I have the following loop in my template that prints my custom post type titles into a HTML structure.
{% for company in companies %}
{% set dot = "<div class='company-dot'></div>" %}
{% set range = range(10, 20) %}
{{dot}}
{{random(range)}}
<div class="company">
<div class="company-dot dot-active"></div>
<p class="dot-caption">{{ company.title }}</p>
</div>
{% endfor %}
I would like to print my {{dot}} variable x amount of times based upon the number that is generated by {{random(range)}}. How can I do this?
The simplest solution would be to iterate random(range) times with a for loop:
{% for i in 0..random(range(10, 20)) %}
{{ dot }}
{% endfor %}
I don't really know Twig but my guess is that you could to the following:
{% for i in random(range) %}
{{dot}}
{% endfor %}
You already know how to use range, just use it again :
{% set dots_count = random(range) %}
{% for dot_index in range(1,dots_count) %}
{{dot}}
{% endfor %}
{{dots_count}}
Here is a fiddle : https://twigfiddle.com/ko595z
We are making a website with a search form. In that form users can search with text and checkboxes. With the checkboxes users can search only in the related categories.
We need to accomplish this with Craft CMS and Twig.
We tried the following method, but that didnt worked.
{% set query = craft.request.getParam('search-results') %}
{% set nietBijdragePlichtig = craft.request.getParam('plg-Nee') %}
{% if nietBijdragePlichtig == 'on' %}
{% for entry in craft.entries.section('producten').limit(null).order(asc).search('query, nietBijdragePlichtig').find() %}
<div id=”test”>test</div>
{% endfor %}
{% endif %}
and we tried:
{% set query = craft.request.getParam('search-results') %}
{% set nietBijdragePlichtig = craft.request.getParam('plg-Nee') %}
{% set alle = query ~ ' ' ~ nietBijdragePlichtig %}
{% if nietBijdragePlichtig == 'on' %}
{% for entry in craft.entries.section(‘producten’).limit(null).order(asc).search(alle).find() %}
<div id=”test”>test</div>
{% endfor %}
{% endif %}
We also tried to use multiple .search()
We tried both codes with different syntaxes.
How can we accomplish this?
Really thanks!
I have a (very big) twig html template, where I display a table and loop through the rows:
{% for assignment in assignments %}
...
{% if assignment.zip.contractor1 is not null %}
{% for priceEK in assignment.zip.contractor1.pricecontractor %}
{% if priceEK.zip == assignment.zip %}
{% set bookDates = date(assignment.start|date).diff(assignment.end).days + 1 %}
{% set priceDay = 0 %}
{% set priceSide = 0 %}
{% if bookDates > 1 %}
{% set priceDay = priceEK.priceDay * (bookDates - 1) %}
{% set priceSide = priceEK.priceAdd * (bookDates) %}
{% endif %}
{% if assignment.meter == 15 %}
{{ (priceEK.price15 + priceDay + priceSide)|number_format(2) }}€
{% elseif assignment.meter == 20 %}
{{ (priceEK.price20 + priceDay + priceSide)|number_format(2) }}€
{% else %}
{{ (priceEK.price25 + priceDay + priceSide)|number_format(2) }}€
{% endif %}
{% endif %}
{% endfor %}
{%endif %}
Now this works like a charm IF, assignment.zip.contractor1 is not null is only true ONE TIME. If it is true a second time while looping through assignments it gives me the following error in my logs:
Error: Nesting level too deep - recursive dependency?
I assume this may be a complicated problem without knowing the database relations, please feel free letting me know what further info is needed (and how to insert here), I'll update accordingly.
//EDIT Maybe there is a way to 'reset' the nested for (priceEK)? {% set priceEK = null %} has no effect though...
//EDIT2: Found sth. else, changed all == to is same as() as described HERE. Now I can get a second loop. If I then have a third one, Firefox crashes and wants to debug the script...
//EDIT: ok, found a solution... Maybe this helps for someone else:
{% for pc in assignment.zip.pricecontractor %}
{% if pc.contractor is same as(assignment.zip.contractor1) %}
{% set bookDates = date(assignment.start|date).diff(assignment.end).days + 1 %}
{% set priceDay = 0 %}
{% set priceSide = 0 %}
{% if bookDates > 1 %}
{% set priceDay = pc.priceDay * (bookDates - 1) %}
{% set priceSide = pc.priceAdd * (bookDates) %}
{% endif %}
{% if assignment.meter is same as(15) %}
{{ (pc.price15 + priceDay + priceSide)|number_format(2) }}€
{% elseif assignment.meter is same as(20) %}
{{ (pc.price20 + priceDay + priceSide)|number_format(2) }}€
{% else %}
{{ (pc.price25 + priceDay + priceSide)|number_format(2) }}€
{% endif %}
{% endif %}
{% endfor %}
I have the follow string:
{% if a == 3 %}
Yes
{% else %}
{% if b == 2 %}
Maybe
{% else %}
{% if c == 1 %}
Hm... Not
{% else %}
No way!
{% endif %}
{% endif %}
{% endif %}
{% if d == 0 %}
Ok
{% endif %}
If I use this regexp
/\{%\s*if\s*(.*?)\s*%\}(.*)(\{%\s*else\s*%\}(.*))?\{%\s*endif\s*%\}/ism
(in preg_match_all function on PHP), my return is all code above. But when I use
/\{%\s*if\s*(.*?)\s*%\}(.*?)(\{%\s*else\s*%\}(.*?))?\{%\s*endif\s*%\}/ism
(just out of greedy mode), my return ends at {% endif %} of {% if c == 1 %} if (the first endif found).
I want to get the following return:
1.
{% if a == 3 %}
Yes
{% else %}
{% if b == 2 %}
Maybe
{% else %}
{% if c == 1 %}
Hm... Not
{% else %}
No way!
{% endif %}
{% endif %}
{% endif %}
2.
{% if d == 0 %}
Ok
{% endif %}
How should be my regex to archive this return?
PS. I know, if I put some name on ifs and use this name on endif, it could be easily resolved using backreference... But I DO NOT want a palliative answer
Thanks in advance.
You can use this recursive pattern:
$pattern = '~{% if [^%]+%}(?>[^{]+|(?R))*(?>{% else %}(?>[^{]+|(?R))*)?{% endif %}~';
online demo
pattern details:
~
{% if [^%]+%}
(?> # this atomic group describes the content
# between if/else/endif markups:
[^{]+ # - all that is not an opening curly bracket
| # OR
(?R) # - recursion (repeat the whole pattern)
)* # repeat the group zero or more times
(?>
{% else %} # The "else" part works exactly in the same way,
(?>[^{]+|(?R))*
)? # but is optional
{% endif %}
~
I extended this example to solve my own problem, specially to have some random {% ... %} in it: regex101 online demo