how make addition from 2 variable twig? - symfony

Anyone knows how to addition two variable in twig I want to do something like:
{{ var1 }} + {{ var2 }}

Just do it inside the {{ }}
{{ var1 + var2 }}
If you want to assign it to some other variable:
{% set foo = var1 + var2 %}
{{ foo }}

Declaration :
{% set counter = 0 %}
Doing the addition :
{% set counter = counter + 1 %}
For displaying in twig template :
{{ counter }}

Related

Round up a variable with Shopify Liquid

I wish to assign a dummy variable to a math value so I can then take the ceiling.
My current code is:
{% if variant.compare_at_price > variant.price %}
SAVE {{ variant.compare_at_price | minus:variant.price | times:100 | divided_by:variant.compare_at_price }}%
{% endif %}
Output is SAVE 20% (for example, but if it's 19.99 it'll be 19% rather than 20%)
But I want to call:
x= {{ variant.compare_at_price | minus:variant.price | times:100 | divided_by:variant.compare_at_price }}%
then take {{ x | ceil }}
How do I assign x?
By using the assign variable tag:
{% assign x = variant.compare_at_price | minus:variant.price | times:100.0 | divided_by:variant.compare_at_price %}
{% assign x = x | ceil %}
SAVE {{x}}%

Twig setting variable in parent template

I have several templates in which I need to write the literals for the day of the week and month. I created a small twig file:
{% set numDay = target.dateStop | date ("w", user_timezone) %}
{% set daysOfWeek = {0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday'} %}
{% set weekDay = daysOfWeek[numDay] %}
{% set numMonth = target.dateStop | date ("n", user_timezone) %}
{% set months = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'} %}
{% set month = months[numMonth] %
In the template where I need these values I first include the "helper" template
{% include '#.../Email/HTML/en_US/localized_day_and_month.twig' with [user_timezone] %}
Then simply use the variable
... {{ weekDay }} ...
I still get this error:
failed: Variable "weekDay" does not exist in ".../HTML/it_IT:do_deadline_new.html.twig" at line 9
I know there's probably a better solution to my "localized date problem" but that's not my point here
EDIT
I moved the first snippet inside the base class I inherit in every template, but those variables still seem to be ignored:
base.html.twig
{% block dateTimeVariables %}
{% set numDay = target.dateStop | date ("w", user_timezone) %}
{% set daysOfWeek = {0: 'Domenica', 1: 'Lunedì', 2: 'Martedì', 3: 'Mercoledì', 4: 'Giovedì', 5: 'Venerdì', 6: 'Sabato'} %}
{% set weekDay = daysOfWeek[numDay] %}
{% set numMonth = target.dateStop | date ("n", user_timezone) %}
{% set months = {1: 'gennaio', 2: 'febbraio', 3: 'marzo', 4: 'aprile', 5: 'maggio', 6: 'giugno', 7: 'luglio', 8: 'agosto', 9: 'settembre', 10: 'ottobre', 11: 'novembre', 12: 'dicembre'} %}
{% set month = months[numMonth] %}
{% endblock %}
And in the child template:
{% extends '#.../Email/HTML/it_IT/base.html.twig' %}
And yet:
failed: Variable "weekDay" does not exist in ".../HTML/it_IT:do_deadline_new.html.twig" at line 6
The line 6 is this:
[{{ group.name }}] Today at {{ target.created | date("H:i", user_timezone) }} {{ agent.name }} {{ agent.surname }} has created a file due on {{ weekDay | lower }} {{ target.dateStop | date ("d", user_timezone) }} {{ month }} at {{ target.dateStop | date ("H:i", user_timezone) }}.
From twig doc :
The include statement includes a template and returns the rendered content of that file into the current namespace.
AFAIK the included file will only be rendered , any variable that had been set in the included file will not be added to the parent namespace.
So try to use extends instead. Something like this :
parent.twig
{% set numDay = target.dateStop | date ("w", user_timezone) %}
{% set daysOfWeek = {0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday'} %}
{% set weekDay = daysOfWeek[numDay] %}
{% set numMonth = target.dateStop | date ("n", user_timezone) %}
{% set months = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'} %}
{% set month = months[numMonth] %}
child.twig
{% extends 'parent.twig'%}

How to build vertical data form in Symfony2, where fields are key-value rows?

Lets say I got some kind of metadata table.
| ID | Key | Value | Data Type | Label |
|----|------|--------|-----------|-------|
| 1 | key1 | value1 | text | ... |
| 2 | key2 | value2 | boolean | ... |
| 3 | key3 | value3 | file | ... |
| 4 | ... | ... | ... | ... |
And it should be displayd as form using Symfony2 Form component and Doctrine2 ORM:
<form>
<label>{{ label1 }}</label><input type="text" name="{{ key1 }}" value="{{ value1 }}">
<label>{{ label2 }}</label><input type="checkbox" name="{{ key2 }}" value="1">
<label>{{ label3 }}</label><input type="file" name="{{ key3 }}" value="{{ value3 }}">
</form>
That is enough to get started :-)
{% for x in xy %}
<label>{{ x.label }}</label>
<input type="
{% if x.datatype == "boolean" %}
checkbox
{% else %}
{{ x.datatype }}
{% endif %}
" name="{{ x.key }}" value="{{ x.value }}">
{% endfor %}

Render SQL result in twig file

How can i render result of following query in twig ?
$q = $em->createQuery('
SELECT mark, student.studentName FROM DemoTemplateBundle:TblMarkDetails mark, DemoTemplateBundle:TblStudentDetails student
WHERE student.id = mark.studentId'
);
$marks = $q->getArrayResult();
Table contents are
Table tbl_mark_details || tbl_student_details
---------------------------------------------------------------
id | student_id | exam_id | score || id | student name
1 |1 | 1 |10 || 1 | Student 1
2 |2 | 1 |5 || 2 | Student 2
3 |2 | 2 |25 ||
I tried the following code, but no student name for the third row
{% set i=0 %}
{% for mark in marks %}
{% if i%2 == 0 %}
<tr>
<td>{{ mark.score }}</td>
<td>{{ mark.examId }}</td>
{% else %}
<td>{{ mark.studentName }}</td>
</tr>
{% endif %}
{% set i = i+1 %}
{% endfor %}
Thanks
Vishnu V
Your should try like this :
$q = $em->createQuery('
SELECT mark, student FROM DemoTemplateBundle:TblMarkDetails mark, DemoTemplateBundle:TblStudentDetails student
WHERE student.id = mark.studentId'
);
$marks = $q->getResult();
and your twig is all right ..

Symfony - Twig - dateTime HH:MM:SS.MMM in SS.MMM?

How to convert a datetime HH:MM:SS:mmm in SS.mmm with a filter in Twig please ( `` )?
exemple :
00:01:30.600 => 90.600
I tried that {{ Object.time | date("s") }} but it doesn't work ...
thanks !
As #john Smith told in his comment (unfortunately I can't +1 but he deserves it), you can use:
{{ Object.time | date("s.u") }}
To change microseconds to miliseconds, you have 2 choices:
Round to 3 digits:
{{ Object.time | date("s.u") | round(3) }}
Slice the 3 last chars:
{{ Object.time | date("s.u") | slice(0, -3) }}

Resources