1. <legend id='TUJpk'><style id='TUJpk'><dir id='TUJpk'><q id='TUJpk'></q></dir></style></legend>
      <tfoot id='TUJpk'></tfoot>

      <small id='TUJpk'></small><noframes id='TUJpk'>

    2. <i id='TUJpk'><tr id='TUJpk'><dt id='TUJpk'><q id='TUJpk'><span id='TUJpk'><b id='TUJpk'><form id='TUJpk'><ins id='TUJpk'></ins><ul id='TUJpk'></ul><sub id='TUJpk'></sub></form><legend id='TUJpk'></legend><bdo id='TUJpk'><pre id='TUJpk'><center id='TUJpk'></center></pre></bdo></b><th id='TUJpk'></th></span></q></dt></tr></i><div id='TUJpk'><tfoot id='TUJpk'></tfoot><dl id='TUJpk'><fieldset id='TUJpk'></fieldset></dl></div>
      • <bdo id='TUJpk'></bdo><ul id='TUJpk'></ul>

      如何使用 Twig 函数返回和插入 HTML 代码?

      How to return and insert HTML code with a Twig function?(如何使用 Twig 函数返回和插入 HTML 代码?)

      1. <tfoot id='7n3nv'></tfoot>

        <small id='7n3nv'></small><noframes id='7n3nv'>

        • <bdo id='7n3nv'></bdo><ul id='7n3nv'></ul>

            <tbody id='7n3nv'></tbody>

            1. <i id='7n3nv'><tr id='7n3nv'><dt id='7n3nv'><q id='7n3nv'><span id='7n3nv'><b id='7n3nv'><form id='7n3nv'><ins id='7n3nv'></ins><ul id='7n3nv'></ul><sub id='7n3nv'></sub></form><legend id='7n3nv'></legend><bdo id='7n3nv'><pre id='7n3nv'><center id='7n3nv'></center></pre></bdo></b><th id='7n3nv'></th></span></q></dt></tr></i><div id='7n3nv'><tfoot id='7n3nv'></tfoot><dl id='7n3nv'><fieldset id='7n3nv'></fieldset></dl></div>
              • <legend id='7n3nv'><style id='7n3nv'><dir id='7n3nv'><q id='7n3nv'></q></dir></style></legend>
              • 本文介绍了如何使用 Twig 函数返回和插入 HTML 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用 Twig 动态添加一些图像.所以我定义了一个名为 setImgService 的新 Twig 函数,它基本上检查服务名称,并根据它,根据它的图像返回一些 HTML 代码.

                I'm trying to add some images dynamically with Twig. So I defined a new Twig function called setImgService which basically chechs for a service name and depending on it, it will return some HTML code according to its image.

                功能代码:

                $setImgService = new Twig_SimpleFunction('setImgService', function($serviceName){
                    switch ($serviceName){
                        case "Analitycs":
                            return '<img src="assets/img/analitycs.png">';
                        case "Hosting":
                            return '<img src="assets/img/hosting.png">';
                        case "SEO":
                            return '<img src="assets/img/seo.png">';
                        case "Maintenance":
                            return '<img src="assets/img/maintenance.png">';
                        case "Your own domain":
                            return '<img src="assets/img/domain.png">';
                        case "BackUp":
                            return '<img src="assets/img/backup.png">';   
                    }
                });
                // Add function to Twig
                $twig->addFunction($setImgService);
                

                但是当我使用时

                {{ setImgService('serviceName') }}
                

                它像纯文本一样返回 HTML 代码.

                It returns HTML code like plain text.

                有什么帮助吗?

                推荐答案

                Twig 默认转义输出.要在函数的 twig 中显示 HTML,您可以使用过滤器 raw 或返回 Twig_Markup,它标记输出是安全的,因此不会被转义.

                Twig escapes output by default. To display HTML inside twig from a function you can either use the filter raw or return a Twig_Markup, which marks the output as safe so it won't be escaped.

                原始过滤器

                {{ setImgService('serviceName') | raw }}
                

                Twig_Markup

                $setImgService = new Twig_SimpleFunction('setImgService', function($serviceName){
                    switch ($serviceName){
                        case "Analitycs":
                            return new Twig_Markup('<img src="assets/img/analitycs.png">', 'UTF-8');
                        case "Hosting":
                            return new Twig_Markup('<img src="assets/img/hosting.png">', 'UTF-8');
                        case "SEO":
                            return new Twig_Markup('<img src="assets/img/seo.png">', 'UTF-8');
                        case "Maintenance":
                            return new Twig_Markup('<img src="assets/img/maintenance.png">', 'UTF-8');
                        case "Your own domain":
                            return new Twig_Markup('<img src="assets/img/domain.png">', 'UTF-8');
                        case "BackUp":
                            return new Twig_Markup('<img src="assets/img/backup.png">', 'UTF-8');  
                    }
                });
                

                这篇关于如何使用 Twig 函数返回和插入 HTML 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                Scale background image to fit ie8 window(缩放背景图像以适合 ie8 窗口)
                Safari 5.1 breaks CSS table cell spacing(Safari 5.1 打破 CSS 表格单元格间距)
                Put in bold part of description in metatag Drupal module(将描述的粗体部分放在元标记 Drupal 模块中)
                Is it possible to compile Coffeescript code in script tags in html files?(是否可以在 html 文件的脚本标签中编译 Coffeescript 代码?)
                  <tbody id='Lxc1q'></tbody>
                <i id='Lxc1q'><tr id='Lxc1q'><dt id='Lxc1q'><q id='Lxc1q'><span id='Lxc1q'><b id='Lxc1q'><form id='Lxc1q'><ins id='Lxc1q'></ins><ul id='Lxc1q'></ul><sub id='Lxc1q'></sub></form><legend id='Lxc1q'></legend><bdo id='Lxc1q'><pre id='Lxc1q'><center id='Lxc1q'></center></pre></bdo></b><th id='Lxc1q'></th></span></q></dt></tr></i><div id='Lxc1q'><tfoot id='Lxc1q'></tfoot><dl id='Lxc1q'><fieldset id='Lxc1q'></fieldset></dl></div>
                  <bdo id='Lxc1q'></bdo><ul id='Lxc1q'></ul>

                    <small id='Lxc1q'></small><noframes id='Lxc1q'>

                          <tfoot id='Lxc1q'></tfoot>
                          <legend id='Lxc1q'><style id='Lxc1q'><dir id='Lxc1q'><q id='Lxc1q'></q></dir></style></legend>