Tuan nguyen
Tuan nguyen

Reputation: 570

Make Twig syntax from string be parsed

I stored a format of shopping order in database, it's like #{{ number }} (it's just a string), how can use this {{ number }} as a execution of Twig.

For example, in my Controller, when I render the view, I also pass a $number variable:

return $this->render('MyBundle:View:index.html.twig', array('number' => 123));

and in my index.html.twig file, it's something like

{% set orderFormat = some_function_to_get_order_format() %} 
// orderFormat will be #{{ number }}
// What can I do to print orderFormat to #123

Upvotes: 1

Views: 677

Answers (1)

Alexey B.
Alexey B.

Reputation: 12033

Try template_from_string function.

{{ include(template_from_string("Hello {{ name }}") }}

Upvotes: 1

Related Questions