I am trying to display the word "comments" in the correct form in Russian depending on the number of comments.
{% set numberOfComments = 11 %}
{{ numberOfComments }} {% if numberOfComments|last == 1 and not (numberOfComments ends with 11) %}комментарий{% elseif numberOfComments|last > 1 and numberOfComments|last < 5 %}комментария{% else %}комментариев{% endif %}
But it ignores and not (numberOfComments ends with 11) and displays "комментарий" instead of "комментариев" even if the number of comments is 11/111/etc. What can I do about it?
As was adviced in a comment, I used a regular expression. So I came up with this:
{{ numberOfComments }} {% if numberOfComments|last == 1 and not (numberOfComments matches '{11$}') %}комментарий{% elseif numberOfComments|last > 1 and numberOfComments|last < 5 and not (numberOfComments matches '{1[2-4]$}') %}комментария{% else %}комментариев{% endif %}
The regex {11$} selects any given string that ends with 11. {} are just to define the regex in Twig.
UPD: added missing logic for numbers ending in 12-14 that was missing from the question but in fact is needed.
Related
I'm having some problems using twig.
When I try to bring an array to javascript, the array always returns the maximum length of 100.
i.e.
{% set getnumbers%}
{% for number in numberLists %}
{{number['odd'] | json_encode() }},
{% endfor%}
{% endset%}
var getNumbers = [
1,
3,
5 ...
197,
199
];
console.log (getNumbers);
On the console:
(100) [
1,
3,
5 ...
197,
199]
length: 100
Also
{{ numberLists | length }} returns = [100];
Although the length returns as 100, it is 100 numbers per page. the last number of the array should be 503.
I believe that the array is limited in some way due to the pagination that is set for every 100 elements.
this is the code for the pagination that is by default in the template
{% if pagination['count'] > 100 %}
{% if searchList %}
{% set params = {} %}
{% for search in searchList %}
{% set params = params | merge([search['name'] ~ '=' ~ search['value']]) %}
{% endfor %}
{% set params = '?' ~ params|join('&') %}
{% endif %}
I'm using a third-party system with an integrated API.
Unfortunately I don't have access to the back-end to be making any adjustments there, the only part I have access to, are the templates to configure some java, html and use some variables and arrays informed by the developer.
in the documentation provided by the developer, there is nothing relevant about this.
The dump function is appearing as unrecognized, from what I searched, it is not enabled by default, so it must be disabled.
I'm attempting to clean up my Titles in my October CMS Project and I'm running into an issue.
I have a set of pages set to be "Singles" and thus have the title [category]-single which is not great.
So to that end I'm trying to use the [x] in [y] function in twig for an if function as follows;
{% else if ('single' in this.page.baseFileName) %}
<title>[formatted title]</title>
{% else %}
this doesn't help and throws an "Unexpected token "name" of value "if" ("end of statement block" expected)." exception.
No variation I can think of (bracketing out the record pointer, for example) and I'm reaching an impasse on this.
Any help?
it seems you are adding extraspace to else if just remove it and use elseif
{% elseif ('single' in this.page.baseFileName) %}
{# ^ use like this #}
<title>[formatted title]</title>
{% else %}
docs : https://twig.symfony.com/doc/2.x/tags/if.html
if any doubt please comment.
So, here's my situation, I have a beginning date and an ending date, with Twig I want to be able to loop through all days in the so called period so that I could print out every day. Of course, that's just for understanding how to do it, the goal is to get them into a chart. Anyway, I have the following code (with what are my vars) :
{% set start_year = date(start) | date('d-m-Y') %}
{% set end_year = date(end)| date('d-m-Y') %}
{% for i in start_year..end_year %}
{{ i }}
{% endfor %}
My start var is 01-01-2003 and my end var is 10-05-2014. The values don't matter as they could change, but that's the format I have.
This actually prints out 0 1 which I don't understand at all. If anyone has an idea either how to do this or how to fix what I'm doing, it would be really nice. Thanks.
You cannot define a range of exact dates (neither as range in php) but you can create a range of seconds with step of a 24 hours second which is 86400; if you use date('U') it will convert date string to seconds since the Unix Epoch (same as Time() in php)
{% set start_date = '01-06-2014' %}
{% set end_date = '05-06-2014' %}
{% for x in range(start_date|date('U'), end_date|date('U'), 86400 ) %}
{{ x|date('d/m/Y') }}<br>
{% endfor %}
Tip
Pay attention the format of date to use - as separator not / because it will lead to totally different result
{% set pd_begdate = header_data.pd_begdate %}
{% set pd_enddate = header_data.pd_enddate ~ ' 23:59:59' %}
{% for key, count_item in range(pd_begdate|date('z'), pd_enddate|date('z'), 1)|slice(column_offset, column_count, 'preserve_keys') %}
{% set item = pd_begdate|date_modify('+'~ key ~' day') %}
<td class="tr5 td9"><p class="p9 ft0">{{ item|date("M") }}<br>{{ item|date("d") }}<br>{{ item|date("D") }}<br>{{ key + 1 }}</p></td>
{% endfor %}
This solution works for day light savings.
I had trouble with the solution when I tried to iterate over an entire year - after about 8 months, I found it would drop a day.
But based on the solution How to add n-days to twig date format within a for-loop?, I found that this would work:
{% set yearstart = "01-01-2003"|date('m-d-Y') %}
{% set eachday = "" %}
{% for i in range(0,365) %}
{% set eachday = yearstart|date_modify("+" ~ i ~ "day")|date('m-d-y') %}
{% endfor %}
here is a twig error again: (Don't know, why twig punish me..)
Snippet:
{% if (formDataValues.id OR formDataValues.ID) %}
....
{% endif %}
What i wanna do:
Check, if one of these values are given in my array.
My Error:
Twig_Error_Syntax [ 0 ]: An opened parenthesis is not properly closed. Unexpected token "name" of value "OR" ("punctuation" expected with value ")") in "MY/PATH/TO/FILE" at line 1
May you have some creative ideas..
Did you try with a lowercase or and parentheses around the values?
{% if (formDataValues.id) or (formDataValues.ID) %}
....
{% endif %}
I am looking to count the number of entries I have in an array in Twig. This is the code I've tried:
{%for nc in notcount%}
{{ nc|length }}
{%endfor%}
This however only produces the length of the string of one of the values in the array.
{{nc}} will produce an output of all the values of the array (there are 2) but I want the output to be just the number 2 (the count) and not all the information in the array.
Just use the length filter on the whole array. It works on more than just strings:
{{ notcount|length }}
This expands on the answer by Denis Bubnov.
I used this to find child values of array elements—namely if there was a anchor field in paragraphs on a Drupal 8 site to build a table of contents.
{% set count = 0 %}
{% for anchor in items %}
{% if anchor.content['#paragraph'].field_anchor_link.0.value %}
{% set count = count + 1 %}
{% endif %}
{% endfor %}
{% if count > 0 %}
--- build the toc here --
{% endif %}
Best practice of getting length is use length filter returns the number of items of a sequence or mapping, or the length of a string. For example: {{ notcount | length }}
But you can calculate count of elements in for loop. For example:
{% set count = 0 %}
{% for nc in notcount %}
{% set count = count + 1 %}
{% endfor %}
{{ count }}
This solution helps if you want to calculate count of elements by condition, for example you have a property name inside object and you want to calculate count of objects with not empty names:
{% set countNotEmpty = 0 %}
{% for nc in notcount if nc.name %}
{% set countNotEmpty = countNotEmpty + 1 %}
{% endfor %}
{{ countNotEmpty }}
Useful links:
length
set
for
{%for nc in notcount%}
{{ loop.index }}
{%endfor%}
loop.index -- The current iteration of the loop.
for reference:https://twig.symfony.com/doc/2.x/tags/for.html