最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP put html inside a twig variable? - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

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>
发布评论

评论列表(0)

  1. 暂无评论