How do we get an element at index i in *.volt view?
I know this for loop
{% for robot in robots %}
{% if robot.type == "cyborg" %}
{{ robot.name }}
{% endif %}
{% endfor %}
but I would like to print the name of robot at index 5 only, and I don't care about other robot names.
Can I access robot at index 5 without using for loop?
Volt templates are compiled to PHP code (you can manually check it in the *.volt.php files), so you can use similar syntax to access array keys in the loop:
{% for key, robot in robots %}
{% if key == 5 %}
{{ robot.name }}
{% endif %}
{% endfor %}
or you can use regular PHP syntax to access element by index without loop:
{{ robots[5] }}
Also, looks like there is a bug with object in array case, so you can use PHP code in the Volt template to resolve your issue without loop:
<?php echo $robots[5]->name ?>
Volt is indeed based on twig but there are some functions which aren't implemented, so you should do better using the original volt documentation: http://docs.phalconphp.com/en/latest/reference/volt.html#variables
Anyways the answer is almost correct, you can access arrays via index but keep in mind that the array index begins with "0" so the correct answer is:
{{ robots[4] }}
You should be able to access a particular index of an array like this:
{{ robots[5] }}
Related
Good morning,
Im trying to build a component where the data arrives in json form from a cms. I need to be able to use an object from the array as the file to include in the twig template. Something like this..
{%cards [{component: '#-card', cardOptions: {''}}, {component: '#-card_small', cardOptions: {''}}] %}
{% for card in cards %} {% include card.component with card.cardOptions only %} {% endfor %}
The problem I have it doesn't seem to parse the '#' from the json and I get the error "file.indexOf is not a function". The project is set up to use '#' as the prefix so I have no way to change that. Iv tried to concatenate in various ways but nothing works. Without the '#' symbol it parses as I expected.
Thank you for any help.
Thanks to your replace suggestion DarkBee, I fixed it by using
{% for card in cards %}
{% set card = '#' ~ card.component %}
{% include card with {card.cardOptions, card:card} only %}
{% endfor %}
I'm trying in my code to build a list of courses in an html file (using twig) - but the thing is - I need to use it twice.
Once to get the list (and that obviously worked), and once to create a form with multiple checkboxes running in another for loop.
I read that the 'unset' function can fix this problem - but it says in doesn't exist in Twig.
here's my code:
{% for course in courses %}
<a href="/course/{{course.id}}">
<div id="info-box">
<img src="/views/{{course.image_link}}" alt="" width=95>
<p style="margin-left: 1rem;">{{course.name}}</p>
</div>
</a>
{% endfor %}
and then I need to write a form:
<form>
{% for course in courses %}
<input type="checkbox" name="course" value={{course.id}}>{{course.name}}
{% endfor %}
</form>
The second loop doesn't work.
I'd love for some help please! :)
and the courses variable comes from a different index file that sends it to an html file.
I'm not sure why u only can use twig as it would be better to fetch the result in actual array than rather passing the PDOStatement.
The reason u can only foreach the collection once is because PDOStament is a forward-only result set. This means you can only get the data from this object once.
To resolve this issue in twig this would mean u need to build an array first and then use that array to display your html
{% set courses_array = [] %}
{% for course in courses %}
{% set courses_array = courses_array | merge([course]) %}
{% endfor %}
It sure is better to do this in PHP, rather then in twig
<?php
$stmt = $pdo->prepare('SELECT * FROM courses');
$stmt->execute();
$courses = $stmt->fetchAll(PDO::FETCH_ASSOC);
$twig->render('courses.twig', [ 'courses' => $courses, ]);
Another solution would be to do the fetch inside twig, to access a constant you can use the function constant
{% set courses_array = courses.fetchAll(constant('PDO::FETCH_ASSOC')) %}
note: I did not test the last solution
I saved my translation data in a database, each line contains a language in the twig template ,i set the variable so
{% if app.request.getLocale()== 'en' %}
{% set language = 1 %}
{%else%}
{% set language = 0 %}
{% endif %}
i use this for display data from database
<p>{{indexpage.0.titpage}}</p>
I wanted to use the variable language to change the display
<p>{{indexpage.language.titpage}}</p>
I have tried it with concatenation and other ways but does not
<p>{{indexpage.~language~.titpage}}</p>
How i can fix this ,thank very much
Assuming it's an array you can do this:
<p>{{ indexpage[language].titpage }}</p>
I have an array of json composed this way :
[{"name":K,"age":14,"job":null}]
I want to know have to read it in twig. I've tried so many solution like those:
array|json_encode()
array|raw
{{ attribute(array, item) }}
But no one of those solutions are working
Thanks
you can simply iterate the json without any preparements
[{"name":K,"age":14,"job":null},{"name":K,"age":17,"job":null}]
{% for key, value in array %}
{{ value.age ~ "-" }}
{% endfor %}
prints 14-17-
I wanted to do something like this:
{{ include("tpl.html")|f }}
But that doesn't seem to work, it just printed tpl.html without any filtering, then I tried:
{% filter f %}
{% include "tpl.html" %}
{% endfilter %}
And it worked. I just wonder, why can't I use shorter one? Do I misunderstand something?
Thanks in advance.
Sorry for being that long to come back :-)
The fact is that the include function writes on the template.
If you do :
{% set s = include('FuzHomeBundle:Default:test.html.twig') %}
Which is not supposed to display something, you'll get the content of the file output anyway, and the s variable will be set to null.
If you do instead :
{% filter upper %}
{% include 'FuzHomeBundle:Default:test.html.twig' %}
{% endfilter %}
or
{% filter upper %}
{{ include('FuzHomeBundle:Default:test.html.twig' }}
{% endfilter %}
The filter tag will compile some code that control output buffer.
To apply a filter on a section of code, you have to wrap it with the filter tag:
{% filter f %}
...
{% endfilter %}
What you were trying originally is to filter a variable which in twig is defined by the double parenthesis:
{{ variable name|filter }}
to read more check out the twig documentation on filters here