Twig Join, display 0 value - twig

I have an array of integers I am rendering with join in a twig template. If the value is zero, however, it is not displaying. Wondering if there is a way to resolve this easily?
Thanks
<b>Snow Last 24 Hours:</b> {{ report.snowLast24Hours|join(' - ') }} in

You can try this (with join and replace):
{{ (snowLast24Hours|join()|replace({'0':''}) is empty) ? '' : snowLast24Hours|join(' - ') }}
Or with join and trim:
{{ (snowLast24Hours|join()|trim('0') is empty) ? '' : snowLast24Hours|join(' - ') }}

Neither of the suggested solutions seemed to work and the '0' values kept getting stripped. I worked around this by printing each individual value rather than using join.
ie.
{{ snow.min }} - {{ snow.max }}

Related

Add variable to a string in Twig

I am trying to modify the value 7 in the following twig code:
{{ "now"|date_modify("+7 days")|date("Y/m/d") }}
The above works fine, but if I try this I get an error:
{{ "now"|date_modify("+" . user.days . " days")|date("Y/m/d") }}
Is it possible to edit the number of days dynamically in twig?
Try to use ~ to concatenate like this :
{{ "now"|date_modify("+" ~ user.days ~ " days")|date("Y/m/d") }}

Array to string conversion, with split

I have a text like: 000325175
and I want to format it as: 000 325 175.
Nothing's easier (in theory) with the split filter, as:
{{ mynumber|split('', 3) }}
But I get a
An exception has been thrown during the rendering of a template ("Notice: Array to string conversion")
However I can apply a slice filter without any problem.
{{ mynumber|slice(9, 14) }}
So I don't understand. Thanks for help
The split filter return an array (with the spitted values), you should only iterate over the result to display it as follow:
{% for partial in mynumber|split('', 3) %}
{{ partial}}
{% endfor %}
Here a working solutions
EDIT:
You can also use the join filter and concatenate the results as example:
{{ mynumber|split('', 3)|join(' ') }}

Drupal8 Twig - convert string to integer

I just started using twig in drupal8. I'm trying to calculate the difference between two numerical drupal8 variables using views.
field_goals_for: 24
field_goals_against: 3
field_goals_difference: should return 21 but returns 1
I tried already something like this (with and without number_formats):
{% set diff = field_goals_for|number_format - field_goals_against|number_format %}
{{ diff }}
I assume the problem is that the two variables are strings instead of int.
There is a way to convert them to int and return the correct result using twig? If not do you have any alternative solution to suggest?
EDIT:
I tried to SUM and also MULTIPLE the two values:
{{ field_goals_for }} = 24
{{ field_goals_against }} = 3
{{ field_goals_for - field_goals_against }} = 0
{{ field_goals_for + field_goals_against }} = 2
{{ field_goals_for * field_goals_against }} = 1
Why are they considered equal to 1 instead of their real value?
EDIT 2: I found the problem. The value that has been to used is field_goals_for__value instead of field_goals_for. Unfortunately I can't find a way to used both of them in the same text field.
stripping off tags was what did the trick for me!
{% set diff = field_goals_for|striptags - field_goals_against|striptags %}
{{ diff }}
try that
{{ diff["#markup"]|number_format(2,',') }}
It works for me.
I had a similar issue - I had to add '#markup' to get it to work.
{{items[0].content['#markup']|number_format/5*100}}
The number_format filter formats numbers. You can control the number of decimal places, decimal point, and thousands separator using the additional arguments.
As example:
{% set diff = field_goals_for - field_goals_against %}
{{ diff|number_format(2,',') }}
Will print:
21,00
Check here a working example.
Hope this help
The number_format filter was added in Twig 1.5
please check your twig version
(sorry this was initially a comment but since I can't comment yet, I wrote an answer)
Try
{{ diff.__toString|number_format(2,',') }}

Empty variable output using Twig

I have a slim/paris/twig application and I have a strange problem with the output.
This is my code:
The title: {{ share.title }}
The output is nothing.
But if I use:
The title {{ share.title | lower }}
The output is:
The title first title
{{ share.title | raw }} works too, but I don't want to use it because it shows accented characteres as weird characters.
Any idea how to solve this ?
Thanks

Twig - using variable

I want to achive this is twig:
{{ form_widget(form.orderItems.0.enabled) }}
{{ form_widget(form.orderItems.1.enabled) }}
{{ form_widget(form.orderItems.2.enabled) }}
....
but the number to be a variable.
I tried this:
{% set index = 0 %}
{{ form_widget(form.orderItems.index.enabled) }}
Error: Method "index" for object "Symfony\Component\Form\FormView" does not exist
and this:
{{ form_widget(form.orderItems.{{index}}.enabled) }}
Error: Expected name or number
and this:
{{ form_widget(form.orderItems.~index~.enabled) }}
Error: Expected name or number
It is possible to achieve this :(
Some digging suggests you use the 'attribute' function - see Accessing array values using array key from Twig.
I suppose that would be something like
form_widget(attribute(form.orderItems, index).enabled)
Unfortunately I can't easily test that at the moment, but it should get you on the right track.
I am solving the same problem now and getting "expected name or number error" when I want to access variable dynamically.
I can not find simple answer, how to dynamically replace some part of variable in twig.
But It works without first dot as was first comment here.
{{ form[othervariable value].vars.label }}
and NOT
{{ form.[value].name }}

Resources