Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display string that contains HTML in twig template?

People also ask

How do you use variables in Twig?

In Twig templates variables can be accessed using double curly braces notation {{ variableName }} .

What is Twig HTML?

Twig is a templating language that compiles to optimized PHP code. It is primarily used for outputting HTML, but can also be used to output any other text-based format. It is a standalone component that can be easily integrated into any PHP project.

Can we write PHP code in Twig file?

Twig is a modern template engine for PHP The overhead compared to regular PHP code was reduced to the very minimum. Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.

What is Twig syntax?

Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates. It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher.


Use raw keyword, http://twig.sensiolabs.org/doc/api.html#escaper-extension

{{ word | raw }}

You can also use:

{{ word|striptags('<b>')|raw }}

so that only <b> tag will be allowed.


{{ word|striptags('<b>,<a>,<pre>')|raw }}

if you want to allow multiple tags


if you don't need variable, you can define text in
translations/messages.en.yaml :
CiteExampleHtmlCode: "<b> my static text </b>"

then use it with twig:
templates/about/index.html.twig
… {{ 'CiteExampleHtmlCode' }}
or if you need multilangages like me:
… {{ 'CiteExampleHtmlCode' | trans }}

Let's have a look of https://symfony.com/doc/current/translation.html for more information about translations use.