1

I would like to output a field from a variable like this:

{% set foo = "entry.title" %}
{{ foo }}

In this example it outputs "entry.title".

Other Example like this:

{% set prefixVar = "en_" %}
{% set foo = "entry."~ prefixVar ~"title" %}
{{ foo }}

In this example it outputs "entry.en_title". Is it possible in Twig to get the content of the entry field instead of the string?

2

Surrounding text in quotes declares it as a string, whereas no quotes will assume variables. If you remove the quotes, you should get what you need.

{% set foo = entry.title %}
{{ foo }}

EDIT:

I realy want to render a string and get the fieldvalue. A other example would be like this: I have a Variable with a Prefix and now i would like to output a field with this prefix: {% set foo = "entry."~ prefixVar ~ "_fieldName" %} {{ foo }}. Is that possible?

I am not sure if that's possible when accessing keys through the dot formatting (ie. array.keyname)

But you could tweak the approach a little bit, to access dynamic keys in entry using the attribute() twig function, such as:

{% set foo = attribute(entry, prefixVar~"_fieldName") %}
  • I realy want to render a string and get the fieldvalue. A other example would be like this: I have a Variable with a Prefix and now i would like to output a field with this prefix: {% set foo = "entry."~ prefixVar ~ "_fieldName" %} {{ foo }}. Is that possible? – Tom Bauer Dec 2 '15 at 5:05
  • Edited my answer, hopefully that should accomplish what you need! – taylor Dec 2 '15 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.