How to show/print same content twice or more using drupal_view in a template? - twig

I have a page where I need to show/print the same content twice. For this, I am using drupal_view.
{{ drupal_view('product_content', 'page_1', node.id) }}
This code works only for the first position and for the second position it doesn't work. What can be done so that the content shows up in both places?

Got the solution from the Drupal community:
When something is rendered, the render system remembers what has been
rendered and won't re-render that. Assign the render output to a twig
variable and print the variable twice:
> {% set product_content = drupal_view( ... )|render %}
> {{ product_content }}
> {{ product_content }}

Related

Convert string into node path

So i am working on a Shopware shop, and i want to read a MediaEntity in Twig. To do so, i am creating a string with the node path (adding the product ID as a variable), which just works fine.
To actually access the MediaEntity, i need to convert this string into a real node path. How do i do that? Or is there maybe another way to create this path?
Here's my code:
{% block component_product_box %}
{{ parent() }}
{% set coverIds = "context.extensions.#{product.coverId}.elements" %}
{{ dump() }}
{% endblock %}
I tried it roughly and something like this should work:
{% set coverIds = _context['extensions'][product.coverId]['elements'] %}
This should solve your problem, I hope.
If you really need to work with a string and "dots" notation, this could be of help:
How to use Twig's attributed function to access nested object properties

Variables concatenation

After about an year of using Smarty i wanted to try Twig.
I am facing a problem concatenating a string and a variable to build dynamically the titles of the page when i switch the language.
In Smarty, the controller passes to the template the variables:
$title_it and $title_en
For the title of the page i do <title>{$title_{$lang}}</title> (where $lang is a global variable) and i can switch the values of the variables when i change the language.
I am not able to replicate this behaviour in Twig.
I tried the following methods without having success:
{{ title_ ~ {{ lang }} }} (I think Twig sees the variable "title_" doesn't exists.
'title_'~{{ lang }} (This prints 'title_it' and not it's content)
Is there a way to keep this logic and continuing to use this approach or do i have to handle the titles of the pages differenly?
Thanks a lot,
Manuel
The _context variable holds all variables in the current context, so you can do:
{{ _context['title_' ~ lang]|default }}
This is basically the same as using the attribute function:
{{ attribute(_context, 'title_' ~ lang)|default }}
I would personally use the former as it's more concise and in my opinion clearer.
The default filter is needed when the environment option strict_variables is set to true (the default value is false, but I prefer to set it to true to avoid accidental problems caused by e.g. typos), otherwise you'll get a Twig_Error_Runtime exception if the variable doesn't exist. For example, if you have variables title_en and title_it but try to output the variable title_de (which doesn't exist), you get that exception with the message Key "title_de" for array with keys "title_en, title_it, lang" does not exist.
A more verbose way to check the existence of a variable is to use the defined test:
{% if _context['title_' ~ lang] is defined %} ... {% endif %}
With the default filter you can also provide a default value:
{{ _context['title_' ~ lang]|default('Default title') }}
If you omit the default value (i.e. you do |default instead of |default('some value')), the default value will be an empty string.
See TwigFiddle
I think here the solutions which can fix your problems.
Controller code:
return $this->render('myTwig.html.twig', array(
'lang'=>'en',
'title_en'=>'English Title',
'title_it'=>'Italian Title'
));
Twig Code:
{% set myVar='title_'~lang %}
{{ attribute(_context, myVar) }}
This will display "English Title" on your page.
Here _context variable is a magic variable in twig where you can find all parameters which you passed from your controller to this twig.
attribute(_context,myVar)
it displays the value from passed parameters key to value.
Hope this will solve your problems.
Thanks
I would change the controller to pre-calculate the language dependent value for title before passing it to the twig template. If you are unable (unwilling) to do that, then ...
<title>{% if lang == 'it' %}{{ title_it }}{% else %}{{ title_en }}{% endif %}</title>

Twig translate dynamic value/string from database

Explanation:
I pull these values from my local database and try to display them on the front-end. The issue is, that I have 2 languages that I need to cater to.
Example:
{% if activeLocale == "si" %}
{{ record.estate_type_SI|raw }}
{% elseif activeLocale == "en" %}
{{ record.estate_type_EN|raw }}
{% endif %}
This works, but when I have multiple items it gets gruesome because I have to write everything down two times. What this does is that depending on the language a value from a different column in the database is pulled.
I am wondering if I can do something similar to this:
{{ record.estate_type_{{"SI"|trans}}|raw }}
I will gladly buy you a beer if you can help me out with this.
Cheers!
EDIT: Variables
Using attribute , you can access a property of an object in a dynamic way. Then you just have to use upper filter to match what you need.
{{ attribute(record, 'estate_type_'~ activeLocale|upper)|raw }}

Twig - using variable

I want to achive this is twig:
{{ form_widget(form.orderItems.0.enabled) }}
{{ form_widget(form.orderItems.1.enabled) }}
{{ form_widget(form.orderItems.2.enabled) }}
....
but the number to be a variable.
I tried this:
{% set index = 0 %}
{{ form_widget(form.orderItems.index.enabled) }}
Error: Method "index" for object "Symfony\Component\Form\FormView" does not exist
and this:
{{ form_widget(form.orderItems.{{index}}.enabled) }}
Error: Expected name or number
and this:
{{ form_widget(form.orderItems.~index~.enabled) }}
Error: Expected name or number
It is possible to achieve this :(
Some digging suggests you use the 'attribute' function - see Accessing array values using array key from Twig.
I suppose that would be something like
form_widget(attribute(form.orderItems, index).enabled)
Unfortunately I can't easily test that at the moment, but it should get you on the right track.
I am solving the same problem now and getting "expected name or number error" when I want to access variable dynamically.
I can not find simple answer, how to dynamically replace some part of variable in twig.
But It works without first dot as was first comment here.
{{ form[othervariable value].vars.label }}
and NOT
{{ form.[value].name }}

Twig Access Array Index?

Is it possible to directly access an array index from within a Twig template?
Here's my setup, using Silex:
return $app['twig']->render('template', array('numbers' => array('one', 'two', 'three')));
so can I do something like this?
{{numbers[0]}}
Just before posting this I realized, that's exactly what you can do, but as I didn't find the answer anywhere in the docs or google (correct me if I'm wrong), I've posted this anyway.
{{numbers[0]}}
The answer of Adam, is correct, only to make it clear and improve,
you can have access directly to array index
{{ myArray[0] }}
if you need to access in a loop
{% set arrayOfItems = ['ZERO', 'ONE'] %}
{% set myArray = ['APPLE', 'ORANGE'] %}
{% for oneItem in arrayOfItems %}
<p>{{ oneItem }} equals {{ myArray[loop.index0] }}</p>
{% endfor %}
in this example I used an array inside a non related loop so the result is:
ZERO equals APPLE
ONE equals ORANGE
Thats actually something what doesnt work for me when using Twig with shopware 6.
I try to access an object like
{{ page.cart.lineItems.elements[0].quantity }}
what will lead into a parsing error of the Twig Template
I can use
{{ page.cart.lineItems.elements | first }}
to get the first Element, but dont know how i can then access a property of this first element

Resources