How to access dynamic variable names in twig?

twig access object property dynamic
twig array
twig attribute
twig get html attribute value
twig for loop
twig dump
twig check if attribute exists
twig set attribute

I have some variables in twig like

placeholder1
placeholder2
placeholderx

To call them, I am looping through the array of objects "invoices"

{% for invoices as invoice %}
    need to display here the placeholder followed by the invoice id number
    {{ placeholedr1 }}

I just had the same issue - and using this first answer and after some additional research found the {{ attribute(_context, 'placeholder'~invoice.id) }} should work (_context being the global context object containing all objects by name)

Symfony2 - How to access dynamic variable names in twig, I have some variables in twig like placeholder1 placeholder2 placeholderx To call them, I am looping through the array of objects "invoices" {% for invoices as� Then in the TWIG I would like to use form_widget to output the fields. The normal approach, when knowing the fields is like shown bellow: {{ form_widget(form.field1) }} In this case, we cannot know whether we will have a field1, field2, etc. on beforehand. Though we have the field names in a variable called key.

Instead of using the attribute function, you can also access values of the _context array with the regular bracket notation:

{{ _context['placeholder' ~ id] }}

I would personally use this one as it's more concise and in my opinion clearer.

If the environment option strict_variables is set to true, you should also use the default filter:

{{ _context['placeholder' ~ id]|default }}

{{ attribute(_context, 'placeholder' ~ id)|default }}

Otherwise you'll get a Twig_Error_Runtime exception if the variable doesn't exist. For example, if you have variables foo and bar but try to output the variable baz (which doesn't exist), you get that exception with the message Key "baz" for array with keys "foo, bar" does not exist.

A more verbose way to check the existence of a variable is to use the defined test:

{% if _context['placeholder' ~ id] is defined %} ... {% endif %}

With the default filter you can also provide a default value, e.g. null or a string:

{{ _context['placeholder' ~ id]|default(null) }}

{{ attribute(_context, 'placeholder' ~ id)|default('Default value') }}

If you omit the default value (i.e. you use |default instead of |default(somevalue)), the default value will be an empty string.

strict_variables is false by default, but I prefer to set it to true to avoid accidental problems caused by e.g. typos.

Twig define dynamic generate variable, I have field names: field_en_image; field_sv_image. node_lang is set as a variable in the .theme file under _preprocess_node hook. I want to print the field� The included template render_box.html is able to access the box variable. The name of the template depends on the template loader. For instance, the \Twig\Loader\FilesystemLoader allows you to access other templates by giving the filename. You can access templates in subdirectories with a slash:

I guess you could use the Twig attribute function.

http://twig.sensiolabs.org/doc/functions/attribute.html

Variable variables in Twig, In order to pull this off, you'll need to take advantage of the not very well documented _context variable in Twig. This should do it: If you use a dynamic object, you can reference the id attribute as divElement.id. Dynamic objects also provide convenient access to dynamic languages such as IronPython and IronRuby. You can use a dynamic object to refer to a dynamic script that is interpreted at run time. You reference a dynamic object by using late binding.

My solution for this problem:

Create array of placeholder(x). Like:

# Options
$placeholders = array(
    'placeholder1' => 'A',
    'placeholder2' => 'B',
    'placeholder3' => 'C',
);

# Send to View ID invoice
$id_placeholder = 2;

Send both variables for view and in your template call:

{{ placeholders["placeholder" ~ id_placeholder ] }}

This print "B".

I hope this help you.

Get variable variables in Twig � Issue #1862 � twigphp/Twig � GitHub, You can use an array with a dynamic key, using dynamic names for a variable in Twig is a horrible idea. Having something like var1, var2 is a� First of all I don't have the abillity to create custom functions of somekind. My question need to be done with the frontend options of Twig. And yes I'm pretty new to Twig :) I'm trying to add some Twig variables to a javascript variable. That javascript variable is used to translate text to differ

I found the solution:

attribute(_context, 'placeholder'~invoice.id)

Dynamic access to object properties � Issue #41 � twigphp/Twig , obj['prop'] will return value only if obj is array and there is no way to get object property if we have it's name in a variable: obj[propName] will� Dynamic pages. Inside the Twig section of a page template, you can use any functions, filters, and tags provided by October. Any dynamic page requires variables. In October, variables may be prepared by the page, layout PHP section, or by Components. In this article, we describe how to prepare variables in the PHP section. Page execution life cycle

Accessing dynamic field names in Symfony's TWIG using attribute , Accessing dynamic field names in Symfony's TWIG using attribute and The only question is…. why didn't you just use an array for those form� The include() Twig function takes as argument the path of the template to include. The included template has access to all the variables of the template that includes it (use the with_context option to control this). You can also pass variables to the included template. This is useful for example to rename variables.

attribute - Documentation - Twig, attribute �. The attribute function can be used to access a “dynamic” attribute of a variable: 1 2 3. {{ attribute(object, method) }} {{ attribute(object, method,� Firstly I want to address that there are several ways to obtain the value of a property and store it in a dynamic Variable. The first most popular, and easiest way IMHO would be: let properyValue = element.style['enter-a-property'];

Access Twig object variables using custom accessors, Twig provides two ways of accessing object properties in its templates: either its object access logic, Twig offers out of the box a quick way to access dynamic PHP magic method __isset() and providing it with the name of the property to� The challenge is Twig is assumes I'm assigning a value to a literal, when I want to assign the value to a evaluated variable name. This way I can just reference each item in the array like {{firstName}} and get back "Adam" in the template. I've tried a number of different ways to force Twig to dynamically create a variable array, such as:

Comments
  • where do you define placeholder 1 - x? Are they global accessable does every invoice got his own placeholder?
  • I am defining them in the controler and passing them to the twig template.
  • Can you show me the code where you're defining and passing them? Then i should be able to help you :)
  • Could you not set create an array of the placeholders and call them like {{ placeholder[invoice.id] }}, or, alternatively, generate the placeholder in a service and then call it in a TwigExtensions using your invoice or invoice.id?
  • Thanks you all, but as a workarround, I created a new method in my Invoice entity where I am setting up the Placeholder.
  • It seems if you use _context in the macro, it will see only the vars which are defined in macro scope, but doesn't see global vars.
  • Thanks, I sow that but how can this apply to my example?
  • Does this work? {{ attribute(app, 'placeholder'~invoice.id) }}
  • No, it tells me that the global variable placeholdersxx does not exists.