php - How to properly enable the twig's sandbox extension in Symfony2? -


in symfony2, there twig module disabled default. 1 of them debug extension, adds {% debug %} tag (useful on development environment).

to enable it, nothing difficult, add service configuration :

  debug.twig.extension:     class: twig_extensions_extension_debug     tags:       - { name: 'twig.extension' } 

but how enable {% sandbox %} tag?

my issue extension's constructor takes security policies :

public function __construct(twig_sandbox_securitypolicyinterface $policy, $sandboxed = false) {     $this->policy            = $policy;     $this->sandboxedglobally = $sandboxed; } 

by reading twig documentation, seen way natively (without symfony2) :

$tags = array('if'); $filters = array('upper'); $methods = array(     'article' => array('gettitle', 'getbody'), ); $properties = array(     'article' => array('title', 'body'), ); $functions = array('range'); $policy = new twig_sandbox_securitypolicy($tags, $filters, $methods, $properties, $functions); $sandbox = new twig_extension_sandbox($policy); $twig->addextension($sandbox); 

i can inside service before using sandbox, that's not clear dependancy injection we're used to.

is there better / proper way enable twig's sandbox extension in symfony2?

why not create private service of security policy:

parameters:     twig.sandbox.tags:         - if     twig.sandbox.filters:         - upper     twig.sandbox.methods:         article: [gettitle, getbody]     twig.sandbox.properties:         article: [title, body]     twig.sandbox.functions:         - range  twig.sandbox.policy:     class: twig_sandbox_securitypolicy     arguments:         - %twig.sandbox.tags%         - %twig.sandbox.filters%         - %twig.sandbox.methods%         - %twig.sandbox.properties%         - %twig.sandbox.functions%     public: false 

you can inject service twig.sandbox.extension service:

twig.sandbox.extension:     class: twig_extension_sandbox     arguments:         - @twig.sandbox.policy     tags:         - { name: twig.extension } 

done. marking twig.sandbox.policy private ensures won't accessible using container (it can still injected other services, think that's not issue).

disclaimer: haven't tested , needs tweaking before works don't copy paste!


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -