【问题标题】:Symfony2 Twig stop escaping pathSymfony2 Twig 停止转义路径
【发布时间】:2012-05-24 21:49:15
【问题描述】:

我需要将路径生成的非转义 URL 放入输入元素中。

routing.yml

profile_delete:
  pattern: /student_usun/{id}
  defaults: { _controller: YyyXXXBundle:Profile:delete }

list.html.twig

<input id="deleteUrl" value="{{ path('profile_delete', {id: '$'}) }}"/>

结果是:

<input id="deleteUrl" value="/student_usun/%24"/>

我尝试了|raw 过滤器,还在{% autoescape false %} 标记之间放置了树枝代码,结果还是一样。

【问题讨论】:

    标签: symfony twig


    【解决方案1】:

    Twig 没有提供 url_decode 过滤器来匹配它的url_encode one,所以你需要编写它。

    src/Your/Bundle/Twig/Extension/YourExtension.php

    <?php
    
    namespace Your\Bundle\Twig\Extension;
    
    class YourExtension extends \Twig_Extension
    {
        /**
         * {@inheritdoc}
         */
        public function getFilters()
        {
            return array(
                'url_decode' => new \Twig_Filter_Method($this, 'urlDecode')
            );
        }
    
        /**
         * URL Decode a string
         *
         * @param string $url
         *
         * @return string The decoded URL
         */
        public function urlDecode($url)
        {
            return urldecode($url);
        }
    
        /**
         * Returns the name of the extension.
         *
         * @return string The extension name
         */
        public function getName()
        {
            return 'your_extension';
        }
    }
    

    然后将其添加到 app/config/config.yml

    中的服务配置中
    services:
        your.twig.extension:
            class: Your\Bundle\Twig\Extension\YourExtension
            tags:
                -  { name: twig.extension }
    

    然后使用它!

    <input id="deleteUrl" value="{{ path('profile_delete', {id: '$'})|url_decode }}"/>
    

    【讨论】:

    • 您应该使用 Twig_Filter_Method,而不是 Twig_Function_Method,因为您正在构建过滤器。
    • 我已将我的文件命名为“src/HQF/Twig/Extension/UrlDecode.php”。在该文件中,我将类命名为“UrlDecodeExtension”。在该类中,函数“getName()”返回'url_decode_extension'。在文件 app/config/config.yml 中,而不是 your.twig.extension 我使用了 url_decode_twig_extension。类声明是HQF\Twig\Extension\UrlDecodeExtension。然后我收到一条错误消息“Fatal error: Class 'HQF\Twig\Extension\UrlDecodeExtension' not found in /[..]/symfony/app/cache/dev/appDevDebugProjectContainer.php on line 2612”。请问有什么办法吗?
    • 为了解决我的问题,我首先使用了这里解释的示例:twig.sensiolabs.org/doc/advanced.html(完美无瑕,但仅适用于安装它的捆绑包),然后复制粘贴urldecode 功能。谢谢你让我走上正确的道路。
    【解决方案2】:

    如果您正在使用:

    'url_decode' => new \Twig_Function_Method($this, 'urlDecode') 
    

    并收到错误:

    Error: addFilter() must implement interface Twig_FilterInterface, instance of Twig_Function_Method given 
    

    替换:

    new \Twig_Function_Method($this, 'urlDecode')" 
    

    与:

    new \Twig_Filter_Method($this, 'urlDecode')"
    

    最好的

    【讨论】:

    • 如果这不起作用,请参阅:twig.sensiolabs.org/doc/tags/autoescape.html(我现在找了大约一个小时,写了一个过滤器扩展等)我的结论是,即使在过滤器之后,twig 也会转义输出被应用,因此你总是例如会得到 &而不是 & 因此需要自动转义模板的这一部分。希望有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多