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 代码?

      时间:2023-10-20

      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 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Twig 之间呈现的隐形字符包括 下一篇:Symfony 的一个视图中有多个控制器?

                相关文章

                <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>