Adding prefix to a dynamic tag

375 Views Asked by At

I need some help with the following implementation. I wanted to add a prefix to my dynamic tag and then want twig to fetch the value for this tag.

{% set result = 'text.journey_service_'~ data.addServ.serviceName %}

{{ result }}

Actual output => text.journey_service_SMALL_PET

Expected output => {{ text.journey_service_SMALL_PET }} = > Dog
1

There are 1 best solutions below

7
On BEST ANSWER

This does not look valid:

{% set result = 'text.journey_service_'~{{ data.addServ.serviceName }} %}

If you want to concatenate a string and a variable, you should better use:

{% set result = 'text.journey_service_'~ data.addServ.serviceName %}

If you want to use result as the variable name to print something, you can use the following code (as given in Twig: Print the value of a variable where the variable name is String):

{{ attribute(_context, result) }}