3

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>
3
  • 1
    {{ greet|raw }}? Commented Apr 11, 2016 at 18:37
  • @Jon wow simple as that! Thanks man! Commented Apr 11, 2016 at 19:11
  • Possible duplicate of Display a string that contains HTML in twig template Commented Apr 12, 2016 at 7:11

2 Answers 2

5
 {% set greet = "<strong>hello</strong>" %}

 <div id="1"> {{ greet|raw }} Jeremy</div>
 <div id="1"> {{ greet|raw }} Davis</div>
Sign up to request clarification or add additional context in comments.

2 Comments

if hello is in a variable then how it possible?
Late to the party, but like so: {% set var_hello = "hello" %} {% set greet = "<strong>" ~ var_hello ~ "</strong>" %}
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.