Symfony2 url_decode problem

Twig doesn't come with a url_decode filter to match its url_encode one, so you'll need to write it.

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

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';
  }
}

And then add it to your services configuration in app/config/config.yml

services:
    your.twig.extension:
        class: Your\Bundle\Twig\Extension\YourExtension
        tags:
            -  { name: twig.extension }

Now you're ready to use it :D

{{ var|url_decode }}
 
 

SHARE ON:

Hello guys, I'm Tien Tran, a freelance web designer and Wordpress nerd. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae.

    Blogger Comment

0 komentar:

Posting Komentar