How to read an array in twig - php

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-

Related

Using object key as the include file in twig template

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 %}

Not escaping HTML tags in twig so they display as markup

Quick question for everyone,
Is there something i am missing, I have not been able to find the correct answer to this, or i am reading the results i find wrong.
Bascially, I have a variable in php
ex $var="<b>#Something#</b>";
And i render it via TWIG like
{{ var }}
I don't want it to actually render <b>#Something#</b>,
But i want #Something#
Any ideas where i am failing at the simple task?
You can try raw method of twig
{% autoescape %}
{{ var|raw }} {# var won't be escaped #}
{% endautoescape %}
OR set false to autoescape
{% autoescape false %}
{{ var }}{# var won't be escaped #}
{% endautoescape %}
Reference: https://twig.symfony.com/doc/2.x/filters/raw.html
The default behaviour in Twig is to escape the vars before displaying them.
If you don'y want the var to be escaped, you have to use the raw filter
{{ var|raw }}
see the doc here

Applying twig filters in Drupal 8 Views Template

I'm building a themed view in Drupal 8 using twig. I'd like to use twig filters on the row.content variable in the views-view-unformatted.html.twig template.
It seems that row.content is an array so twig's string manipulation doesn't work. However, it prints onto the page as a string (view is just a list of taxonomy terms).
What I'd like to do is slugify the output so taxonomy terms with spaces can have valid hrefs. See the replace filter in the code below.
<ul class="nav nav-tabs" role="tablist">
{% for row in rows %}
<li role="presentation" class="{{loop.first ? 'active' : ''}}">
{{row.content}}
</li>
{% endfor %}
</ul>
This will just output <a href="#">. Does anyone know how to access the raw text value that is output during twigs interpolation?
Thanks!
I ran into this myself it was difficult because kint and dump crashes on views.
there is a quick workaround to get to the bits though, put this under {% for row in rows %} in your twig views style template.
<ol>
{% for key, value in row.content %}
<li>{{ key }}</li>
{% endfor %}
</ol>
load the page with that & gives you the keys to see, I checked them each with the following dump command, just added underneath to test.
{{ dump(row.content['#row']) }}
The above dump showed all the goods in #row, where I dug in and found the field I wanted inside _entity (may be different for you), then I wanted to replace spaces with dash and force lowercase.
Everything past row.content['#row'] is likely different for you, you'll need to dig in the array a bit with the dump command mentioned above.
Below is the line that got me what I wanted.
{{ row.content['#row']._entity.title[0].value|replace(' ', '-')|lower }}
Below is twig template example.
For filename change viewname and block-3 to your setup.
views-view-unformatted--viewname--block-3.html.twig
{% for row in rows %}
{%
set row_classes = [
default_row_class ? 'views-row',
'something',
'kint-cant',
]
%}
{# My field value unformatted #}
<!-- {{ row.content['#row']._entity.title[0].value }} -->
<section{{ row.attributes.addClass(row_classes) }} id="{{ row.content['#row']._entity.title[0].value|replace(' ', '-')|lower }}">
{{ row.content }}
</section>
{% endfor %}
Im sure there are plenty of other ways to do this, but it worked for me as a quick solution to print out views fields in the style template, which is really useful.

phalcon php : get element at index

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] }}

Twig filter included template

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

Categories