Converting a String to Float in Twig - twig

Basically, I have set a parameter called "rating" that's equal to a product.DETAILS.STAR_RATING which is a value imported from a database-driven field which happens to be a string, I want to multiply this value by 20 but since "rating" is a string I cannot multiply it.
How do I convert the string to a float value?
{% set rating = product.DETAILS.STAR_RATING %}
{{rating * 20}}

Very simple way, maybe strange but...
{% set rating = 0 + product.DETAILS.STAR_RATING %}
{{ rating * 20}}

Related

How to separate the two values? Twig return sql group concat

i need a help for make the result.
my sql request :
$posts = $db->prepare('SELECT
CM.idpost,
CM.title,
CM.slug,
CM.content,
CM.cover,
CM.date,
GROUP_CONCAT(PC.id SEPARATOR ";") AS list_id,
GROUP_CONCAT(PC.cat_name SEPARATOR ";") AS list_cat_name,
GROUP_CONCAT(PC.icon SEPARATOR ";") AS list_icon,
GROUP_CONCAT(PC.bg SEPARATOR ";") AS list_bg,
GROUP_CONCAT(PC.slug_cat SEPARATOR ";") AS list_slug_cat,
U.iduser,
U.username,
U.avatar
FROM cms_posts CM
LEFT JOIN relation_posts RP ON RP.id_post = CM.idpost
LEFT JOIN cms_postcategory PC ON PC.id = RP.id_category
LEFT JOIN users U ON U.iduser = CM.author
GROUP BY CM.idpost
ORDER BY CM.date DESC LIMIT '.$paginationStart.','.$limit.'');
this is what my sql query for my table returns: Marketing;Général in Twig :
{{ post.list_cat_name }}
Picture demo
I need the result :
Marketing
Général
Thanks for help <3
So you've got a string like Marketing;Général. You need to create 2 links using the text for each as the link text.
I assume you similarly need to split the list_id for your idcategory value.
So something like this:
{% set categoryNames = post.list_cat_name | split(';') %}
{% set categoryIds = post.list_id | split(';') %}
{% for category in categoryNames %}
{{category}}
{% endfor %}
https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
https://twig.symfony.com/doc/2.x/filters/split.html

Parametrized twig date

I need how to check if a custom date is inside a range.
The problem is that the API returns a calendar but year and month are sent separately and I can't create a date with twig.
This part of the code seems to return my variables as expected
<script>console.log("Year"+{{CurrYear| json_encode()| raw}});</script>
<script>console.log("Month: "+{{CurrMonth| json_encode()| raw}});</script>
while I can retrieve the day within the loop
<script>console.log("Day: "+{{key| json_encode()| raw}});</script>
I'm trying to create a data item so I can use the native comparisons. I know I can split the other date and compare it one by one, but I'm trying to avoid this
<script>console.log({{ annoForm."-".mesForm."-".key |date('Y-m-d')}}) ;</script>
So I can use it afterwards like
{% set auxDate = annoForm."-".mesForm."-".key |date('Y-m-d') %}
BUT, I can't seem to construct a valid date here so I could use it in:
{% datestart < auxDate and dateend > auxDate %}
{# condition met #}
{% endif %}
Main problem here was concatenating correctly the string when instantiating date():
{% set auxDate = ("#{annoForm}-#{mesForm}-#{key} " | date('Y-m-d') ) %}
I want to add a couple of issues i found while working on this.
First the importance of using date('Y-m-d') instead of "date()" because it will consider July before June (due alphabetic order).
Take nulls or open fields into account (in my case datestart is mandatory)
{% if datestart < auxDate and ( dateend is null or dateend > auxDate ) %}
Hope it helps!

Twig handle multiple comparisons

I'm struggeling to compare multiple values with Twig. I don't have access to any core code since this is for a SaaS platform similar like Shopify.
I want to compare product weights and set a variable if the comparison is true.
So I have multiple weights (in grams) like this (price behind it)
751 - 9990 $5
9990 - 19799 $10
19800 - 19849 $5
19850 - 19950 $14
29700 - 29749 $5
What is a good way to compare those weights and set a price accordingly? I thought something like this:
{% if (product.weight >= 751) or (product.weight <= 9990) %}
{% set shippingCosts = '5' %}
{% elseif (product.weight >= 9991) or (product.weight <= 19799) %}
{% set shippingCosts = '10' %}
However I have a very long list (12+) with different weights. So is there a quick way to handle this list? Otherwise it will become a long list with if/else :(
Good way is make custom Twig Extension
{% set shippingCosts = product.weight|custom_twig_extension %}

Strange Twig behaviour when using "in" operator

Today I noticed strange behaviour then trying to check if array has value.
I tried to do {% if key in array|keys %} ... {% endif %}
and condition was equal to true always.
I tried to do this later: {{ dump('a' in [0, 1, 2]) }}.
And guess what value was dumped? It was "true" somehow.
Do you guys have any idea why is it happening?
I can workaround it by using 'a' in [0, 1, 2]|join but that's not what I want to figure out.
It's not twig, it's php. The following code:
var_dump(in_array('a', array(0, 1, 2)));
prints:
bool(true)
When comparing a string to an int, the string gets converted to int. In this case, 'a' becomes 0, thus matching one of the array keys.
Try doing a var_dump("foobar" == 0) and you'll see it is true as well.
You can use foo['a'] is defined instead, demo here: http://twigfiddle.com/y126mg

Coding Implode In Twig

How can one use the equivalent of the implode function in a Twig environment?
For example, the contents of the variable $data1 = "input your name" and the variable $data2 = "input your address".
How to create a variable $result = "input your name, input your address" in Twig?
I'm guessing you're looking for the join filter. It works exactly like implode does in php. Quoting from the manual page:
The join filter returns a string which is the concatenation of the items of a sequence:
{{ [1, 2, 3]|join }}
{# returns 123 #}
The separator between elements is an empty string per default, but you can define it with the optional first parameter:
{{ [1, 2, 3]|join('|') }}
{# returns 1|2|3 #}

Resources