I have a Symfony 2.1 version with a controller that returns the total of points.
I am rendering this in my twig template as follows:
{%render "AdminBundle:Reports:getExpiringPoints" with {'id':dealer.id}%}
This prints the total points. I need to check that value and print it if the rendering value is greater that 0.
Is it possible in Symfony 2.1?
i found that :
{% set x %}
{%render "AdminBundle:Reports:getExpiringPoints" with {'id':dealer.id}%}
{% endset %}
{% if x> 0 %}
//display
{% endif %}
and after you can use it.
Tell me if it works
You can check it directly in the controller or with twig :
{% if var > 0 %}
//display
{% endif %}
Twig supports all of the standard logical operators ==, !=, <, >, >=, and <=.
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 have a dropdown menu with 2 options
Ordered
Unordered
I want to check if the value is Unordered or Ordered
{% set viewModel = {
cards: content['cards'] ?? [],
cardListType: content.cardListType ?? ''
} %}
{{ viewModel.cardListType|json_encode() }}
Returns this to me
{"0":{"label":"Unordered","value":"unordered","selected":true}}
I want to check if the value of viewModel.cardListType is unordered and that it's selected.
I have tried
{% if viewModel.cardListType.value is unordered %}
<p>UNORDERED</p>
{% endif %}
and
{% if viewModel.cardListType.value:unordered %}
<p>UNORDERED</p>
{% endif %}
and several other variations of this.
I don't understand the documentation/ syntax for dropdowns in craft or twig.
Can someone please explain like I'm 5 how this works and how to check the value?
I am using Craft 3 if this changes the syntax
Thanks in advance
I've bend over backwards to solve this one, but no luck yet.
{% for field in fields %}
{{dump(form.children.~(field.label)~.vars.value)}}
{% endfor %}
U see the above code in twig? How do I get twig to evaluate (field.label) first and then evaluate the rest of the expression((form.children.Age.vars.value)). For now it throws the twig syntax error
Expected name or number.
Any ideas?
You can use:
{% for field in fields %}
{{ dump(form.children[field.label].vars.value) }}
{% endfor %}
Cheers
I want to reuse pretty heavy logic only code a few times, in php I would use a function, but in twig I went with a solution from this old question.
In short, I use a macro like that:
{% import _self as test %}
{% macro check() %}
{{ test }}
{% endmacro %}
{% set v = test.check() %}
{% if v == 'test' %}
this should display
{% endif %}
Here is a fiddle: https://twigfiddle.com/kyv3zr/2
The problem is that v is a Twig_markup object. It doesn't seem to have any public properties. Running dump on it gives me this:
object(Twig_Markup)#1244 (2) { ["content":protected]=> string(13) " 1 " ["charset":protected]=> string(5) "UTF-8" }
How do I use it in an if statement?
Or is there a better way of storing a logic only code for reuse across templates?
If the object is called v then the dump seems to show it has a content value, so try:
{% if v.content == '1' %}
{# do something here #}
{% endif %}
not certain though, but try it.
EDIT #2 - based on comments question.
So I guess if you want to use v in an if statement, you would use it like so:
{% if v == '1' %}
{# do something here #}
{% endif %}
This presumes it does equal to "1".
Using twig, how can I translate all items in an array and join them with a slash?
Do I have to use an additional variable or is there a cleverer method?
For the moment, I'm doing something like this:
{% set labels = [] %}
{% for feature in menu_item.features %}
{% set labels = labels|merge([feature|trans([], 'features')]) %}
{% endfor %}
{{ labels | join(' / ')}}
It sucks.
Why not just output the content while you're looping ?
{% for feature in menu_item.features %}
{% if loop.index0 > 0 %}/{% endif %}
{{feature|trans}}
{% endfor %}
Maybe I'm late to the party, but you can now do this easily with the map filter:
{{ menu_item.features|map(feature => feature|trans)|join(' / ') }}
See documentation:
Twig >v1.41: https://twig.symfony.com/doc/1.x/filters/map.html
Twig >v2.10: https://twig.symfony.com/doc/2.x/filters/map.html
Twig v3.x: https://twig.symfony.com/doc/3.x/filters/map.html
Not everything should be done within the "view".
This type of code is probably much better placed within your controller logic and then passed into the view as the merged+joined result. Because in your example all you're doing is compiling a result which can much more easily be done within code.