Hello I am trying to try something that is expieremental and not really sure if it's possible. I have a twig template with some html that is duplicated on the page and I am wondering if it's possible to create a variable in twig that holds a snippet of html (must include html markup) that i can then call throughout the page instead of repeating myself.... thanks in advance
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
</head>
<body>
{% set greet = "<strong>hello</strong>" %}
<div id="1"> {{ greet }} Jeremy</div>
<div id="1"> {{ greet }} Davis</div>
</body>
</html>
Hello I am trying to try something that is expieremental and not really sure if it's possible. I have a twig template with some html that is duplicated on the page and I am wondering if it's possible to create a variable in twig that holds a snippet of html (must include html markup) that i can then call throughout the page instead of repeating myself.... thanks in advance
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
</head>
<body>
{% set greet = "<strong>hello</strong>" %}
<div id="1"> {{ greet }} Jeremy</div>
<div id="1"> {{ greet }} Davis</div>
</body>
</html>
Share
Improve this question
asked Apr 11, 2016 at 18:33
samperelesssampereless
3214 silver badges10 bronze badges
3
-
1
{{ greet|raw }}
? – Jon Commented Apr 11, 2016 at 18:37 - @Jon wow simple as that! Thanks man! – sampereless Commented Apr 11, 2016 at 19:11
- Possible duplicate of Display a string that contains HTML in twig template – Veve Commented Apr 12, 2016 at 7:11
2 Answers
Reset to default 5 {% set greet = "<strong>hello</strong>" %}
<div id="1"> {{ greet|raw }} Jeremy</div>
<div id="1"> {{ greet|raw }} Davis</div>
for those looking for a better way you could do this
{% set greet %}
<strong>hello</strong>
{% endset %}
and use it like this
<div id="1"> {{ greet|raw }} Jeremy</div>
<div id="2"> {{ greet|raw }} Davis</div>