sampereless
sampereless

Reputation: 311

PHP put html inside a twig variable?

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>

Upvotes: 3

Views: 4499

Answers (2)

Ismael Lastlevel
Ismael Lastlevel

Reputation: 54

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>

Upvotes: 0

Imanali Mamadiev
Imanali Mamadiev

Reputation: 2654

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

 <div id="1"> {{ greet|raw }} Jeremy</div>
 <div id="1"> {{ greet|raw }} Davis</div>

Upvotes: 5

Related Questions