简体   繁体   English

数据属性内的Twig变量

[英]Twig variable inside data attribute

I have a problem with a twig variable with a filter inside a data- attribute in order to pass some content into a bootstrap modal. 我在twig变量中遇到了一个问题,该变量在data-attribute中具有过滤器,以便将某些内容传递到引导程序模态中。 The problem is the filter obfuscateEmail which changes the content of the variable: https://github.com/Propaganistas/Email-Obfuscator#twig 问题是过滤器obfuscateEmail更改了变量的内容: https : //github.com/Propaganistas/Email-Obfuscator#twig

My code: 我的代码:

{% for i in 1..20 %}
    <a data-content="{{ _context['content_' ~ i ~ '_raw']|obfuscateEmail }}" data-toggle="modal" href="#modal" class="btn btn-primary">...</a>
{% endfor %}

I already tried to escape it without any success. 我已经试图逃脱它,但没有成功。 The problem is always the same either the html code is wrong because of quotes inside the variable coming from the obfuscateEmail filter or the modal wont work or displays pure html. 问题总是相同的,要么是html代码是错误的,要么是由于变量中的引号来自obfuscateEmail过滤器,还是模态无效或显示了纯HTML。

The problem lays in the fact that the plugin is marking the output as safe, thus returning valid HTML , which breaks your HTML . 问题在于,该插件将输出标记为安全,从而返回有效的HTML ,从而破坏了HTML

You could tweak the extension to fit your needs 您可以根据需要调整扩展名

<?php 
    namespace Propaganistas\EmailObfuscator\Twig;

    use Twig_Extension;
    use Twig_SimpleFilter;

    class Extension extends Twig_Extension
    {
        /**
         * Returns the name of the extension.
         *
         * @return string The extension name
         */
        public function getName()
        {
            return 'propaganistas.emailObfuscator';
        }
        /**
         * Returns a list of filters to add to the existing list.
         *
         * @return array An array of filters
         */
        public function getFilters()
        {
            return array(
                new Twig_SimpleFilter(
                    'obfuscateEmail',
                    array($this, 'parse')
                ),
            );
        }
        /**
         * Twig filter callback.
         *
         * @return string Filtered content
         */
        public function parse($content, $is_safe = false)
        {
            $content = obfuscateEmail($content);
            return $is_safe ? new Twig_Markup($content, 'UTF-8') : $content;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM