Float Comparison in twig - symfony

I'm trying to compare two floats in twig but I don't have the good result :
I have two 'equal' floats : float1 and float2
{% if float1 == float2 %}
<span>Floats are equal</span>
{% else %}
<span>Floats are different</span>
{% endif %}
{{ float1 == float2 }}
Display :
<span>Floats are different</span>
1
How can I compare two floats in Twig ?
I don't understand why the result of the comparison is true but the result of the if statement is false

Here is a working example that I tried on Twigfiddle:
{% set float1 = 1.0123456789012 %}
{% set float2 = 1.0123456789011 %}
{% if float1 == float2 %}
<span>Floats are equal</span>
{% else %}
<span>Floats are different</span>
{% endif %}
The limit (at least on Twigfiddle) is 13 decimal places. So 1.0123456789012 works; but if you increase to 1.01234567890123, the comparison won't work.
Here is the Twigfiddle for you to see it working: https://twigfiddle.com/r3hi48
Is your code any different than that? In other words in the above I use set to declare the two float variables. Have you also tried {{ dump(float1) }} to see what the value prints out as?

Related

Random values in array with Timber (Twig) and Wordpress

I merged values of two arrays in a new array.
But I would like to take random values from this array and put them in a loop. That those values iterate in this loop.
{% set myArray = [] %}
{% set list1 = options.transitions_repeater %}
{% set list2 = options.transitions_wahou_repeater %}
{% set myArray = list1|merge(list2) %}
{% for key, val in myArray %}
{{ val|join(', ') }}
{% endfor %}
{% for item in options.projets %}
<li data-transisition="{{ myArray }}"></li>
{% endfor %}
I got the message : Array to string conversion in XX on line XX
Output :
animBottom
animTop
animLeft
directionRight
circles
cube
Your merged list is still a multidimensional array.You could solve your issue with the following code, however it's preferable to move the logic of creating the (single dimensional) array to your controller (then you could remove the filter first in the snippet)
{% for item in options.projets %}
<li data-transisition="{{ myArray[random(myArray| keys)] | first }}"></li>
{% endfor %}
demo

Twig - Get corresponding alphabet letter in loop

I want to use the loop.index variable in twig to get the corresponding alphabet letter (1 = A, 2 = B, etc).
{% for item in form.items %}
{% set nom_item = 'Item'~loop.index %}
{% endfor %}
How could I do to get alphabet letter in loop ? I can't find a twig function for that.
Simplest solution
{{ range('A','Z')[loop.index0] }}
try with this!
{% set foo = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] %}
{% for index,item in form.items %}
{% set nom_item = 'Item'~foo[index] %}
{% endfor %}

Setting conditional value in twig do something block

I am trying to setting a conditional value using if and else if inside a twig template, so that is what I have to do
{% set x = '1' %}
{% if a and b %}
{% set x = '2' %}
{% elseif a or b %}
{% set x = '3' %}
{% else %}
{% set x = '4' %}
{% endif %}
The above works fine for me. But my question is, is there a way I can achieve the same using only one {% %} (do something) block.
Something like:
{%
set x = '1'
if a and b
...
elseif a or b
...
else
%}

Trying to locate some objects in a space

I'm trying to locate some objects in a field using Twig given some conditions (yes, I know it s simple, but I'm having too much troubles to have results). It's all right, but I'm having troubles giving some distance between objects.
This is my code:
{% for key, positions in teams %}
{% for key1, position in positions %}
{% for key2, player in position %}
{% set x = 100 %}
{% set counter = player.positionId|length + 1%}
{% set d = x/counter %}
{% if player.positionId == 1 %}
{% set top = 0.4 %}
{% set xpos = 42 %} //That value is correct because it's a goalkeeper
{% elseif player.positionId == 2%}
{% set top = 9.5 %}
{% set xpos = %}
{% elseif player.positionId == 3%}
{% set top = 20.5 %}
{% set xpos = %}
{% elseif player.positionId == 4%}
{% set top = 32.5 %}
{% set xpos = %}
{% endif %}
In summary, I have some players that are located on different y-coodinates given their positions, but also I need each one to have some distance between them if they have the same position, being xpos its position in the x-axis.
If you don't know Twig, you can help me using another lannguage too. Thanks in advance
Ah, and well, I want to know how to separate the teams (because it renders all players at this moment)
There are multiple ways to pass values to twig, one way is to do this through routing,
# app/config/routing.yml
blog_show:
path: /blog/{YOUR_VARIABLE_OR_VALUE_HERE}
defaults: { _controller: AcmeBlogBundle:Blog:show }
see this reference: http://symfony.com/doc/current/book/routing.html
you can also set global variables: http://symfony.com/doc/current/cookbook/templating/global_variables.html
or create an array in controller using repository or on your on, then send this to the view through the params
ie)
$repository2 = $this->getDoctrine()->getRepository('YOUR_BUNDLE_NAME_Bundle:YOUR_REPOSITORY');
$YOUR_ARRAY_NAME = $repository2->REPOSITORY_METHOD_OR_GENERIC_METHOD($IDENTIFIER);
$params = array('form' => $form->createView(), 'YOUR_ARRAY_NAME' => $YOUR_ARRAY_NAME');
return $this->render('SystemCheckoutBundle:Checkout:checkout.html.twig', $params);
these params can then be accessed in the view via:
{% for YOUR_ARRAY_NAME in YOUR_ARRAY_NAME %}
Client ID: {{ YOUR_ARRAY_NAME.contactId }}<br>
First Name: {{ YOUR_ARRAY_NAME.firstName }}<br>
Last Name:{{ YOUR_ARRAY_NAME.lastName }}<br>
Email: {{ YOUR_ARRAY_NAME.email }}<br>
{% endfor %}

Twig: How to get the first character in a string

I am implementing an alphabetical search.
We display a table of Names. I want to highlight only those alphabets, which have names that begin with the corresponding alphabet.
I am stumped with a simple problem.
How to read the first character in the string user.name within twig.
I have tried several strategies, including the [0] operation but it throws an exception.
Here is the code
{% for i in ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0-9'] %}
{% set has_user_starting_with_alphabet = false %}
{% for user in pagination %}
{% if user.name[0]|lower == i %}
{% set has_user_starting_with_alphabet = true %}
{% endif %}
{% endfor %}
{% if has_user_starting_with_alphabet %}
<li><span>{{ i }}</span></li>
{% endif %}
{% endfor %}
Is there some function like "starts_with" in twig?
Since twig 1.12.2 you can use first:
{% if user.name|first|lower == i %}
For older version you can use slice:
{% if user.name|slice(0, 1)|lower == i %}
Note: You may also use this notation:
{% if user.name[:1]|lower == i %}

Resources