Insert a variable into a display string in twig - php

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>

Related

Twig posting empty modules on page

I'm working with Twig in Craft and am trying to include a module I've made, I have added all the content into the modules in the CMS and saved it, they show on the page correctly, but a tonne of empty paragraph tags show also, does anyone know why this is?
Steps I took:
I have a twig file called "Abilities.twig", inside abilities I have the below code:
<p>{{ module.Abilities }}</p>
In Index.twig I have the below:
{% for module in entry.modals %}
{% include '_modals/Abilities' %}
{% endfor %}
An example below of how they show on the page
<p>This is the first example of an ability</p>
<p>This is the second example of an ability</p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
In the CMS the only modules that are shown are the top 2 examples, there are no empty modules saved.
If anyone can help would appreciate it
If you don't want to have the "extra" <p>'s you need to verify the content is not empty
{% if module.Abilities | trim != '' %}
<p>{{ module.Abilities }}</p>
{% endif %}
You can add an if statement in you loop
{% for module in entry.modals if module != null %}
{% include '_modals/Abilities' %}
{% endfor %}

Concatenate output from template file inside twig for loop to variable?

I have a situation where I need to include a template file inside another template and output it in a for loop. The problem I am having though, is that it's outputting to a container and I only need the container to render once, but I need the included template to render on all iterations of the loop.
{% for details in array %}
{% set data_details %}
{{ include('data.html.twig') }}
{% endset %}
<!-- Other HTML needed in the loop -->
{% if loop.first %}
<tr class="table-row">
<td>
{{ data_details | raw }}
</td>
</tr>
{% endif %}
{% endfor %}
As you can see, the {% if loop.first %} prevents the container from repeating. But I need data.html.twig to loop on every iteration and append the HTML to the data_details variable but the variable only contains the last iteration. I am not that knowledgeable with Twig so maybe I am going about this the wrong way. The documentation says if you wrap something in a set it will "capture" the output but that isn't happening for some reason.
You're close, the iteration does overwrite the already set variable.
To append to the already set variable , you can repeat/output the variable in the wrap to recapture/concatenate it
{% set data_details %}
{{ data_details | default('') }}
{{ include('data.html.twig') }}
{% endset %}

How to include twig file from array?

Hi how do I pass a array to a twig include?
{% set navbar_logo %}["{{sprinkle|raw}}/components/content/navbar/navbar-logo.html.twig", "/components/content/navbar/navbar-logo.html.twig"]{% endset %}
{% include navbar_logo %}
this results in:
Unable to find template "["#admin/components/content/navbar/navbar-logo.html.twig", "/components/content/navbar/navbar-logo.html.twig"]"
this works fine:
{% include ["{{sprinkle|raw}}/components/content/navbar/navbar-logo.html.twig", "/components/content/navbar/navbar-logo.html.twig"] %}
But i need to get it from the variable.
This also works:
{% set navbar_logo %}{{sprinkle|raw}}/components/content/navbar/navbar-logo.html.twig{% endset %}
{% include navbar_logo %}
But I need the backup incase the first one does not exist.
How can I do this?
What about:
{% set navbar_logo = include(sprinkle|raw ~ '/components/content/navbar/navbar-logo.html.twig') %}
I don't exactly know what you're trying to achieve, but a better approach would be to set this in a block of your base layout.
{% block navbar_logo %}
{{ include(sprinkle|raw ~ '/components/content/navbar/navbar-logo.html.twig') }}
{% endblock %}
Then when you later need to dump your logo, use {{ block('navbar_logo') }}.
More about blocks
If you're using {% set .. %}... {% endset %}, Twig is treating the variable as a string
You should switch your code to this and then it works
{% set navbar_logo = [ sprinkle~"/components/content/navbar/navbar-logo.html.twig", "/components/content/navbar/navbar-logo.html.twig"] %}

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