簡體   English   中英

數據屬性內的Twig變量

[英]Twig variable inside data attribute

我在twig變量中遇到了一個問題,該變量在data-attribute中具有過濾器,以便將某些內容傳遞到引導程序模態中。 問題是過濾器obfuscateEmail更改了變量的內容: https : //github.com/Propaganistas/Email-Obfuscator#twig

我的代碼:

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

我已經試圖逃脫它,但沒有成功。 問題總是相同的,要么是html代碼是錯誤的,要么是由於變量中的引號來自obfuscateEmail過濾器,還是模態無效或顯示了純HTML。

問題在於,該插件將輸出標記為安全,從而返回有效的HTML ,從而破壞了HTML

您可以根據需要調整擴展名

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