Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly enable the twig's sandbox extension in Symfony2?
    primarykey
    data
    text
    <p>In Symfony2, there is some Twig module disabled by default. One of them is the debug extension, that adds <code>{% debug %}</code> tag (useful on a development environment).</p> <p>To enable it, nothing really difficult, you add this service to your configuration :</p> <pre><code> debug.twig.extension: class: Twig_Extensions_Extension_Debug tags: - { name: 'twig.extension' } </code></pre> <p>But how to enable the <code>{% sandbox %}</code> tag?</p> <p>My issue is that the extension's constructor takes security policies :</p> <pre><code>public function __construct(Twig_Sandbox_SecurityPolicyInterface $policy, $sandboxed = false) { $this-&gt;policy = $policy; $this-&gt;sandboxedGlobally = $sandboxed; } </code></pre> <p>By reading the <a href="http://twig.sensiolabs.org/doc/api.html#sandbox-extension" rel="nofollow">twig documentation</a>, I seen the way to do it natively (without Symfony2) :</p> <pre><code>$tags = array('if'); $filters = array('upper'); $methods = array( 'Article' =&gt; array('getTitle', 'getBody'), ); $properties = array( 'Article' =&gt; array('title', 'body'), ); $functions = array('range'); $policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties, $functions); $sandbox = new Twig_Extension_Sandbox($policy); $twig-&gt;addExtension($sandbox); </code></pre> <p>I can do something like that inside a service just before using the sandbox, but that's not as clear as the dependancy injection we're used to.</p> <p><strong>Is there a better / proper way to enable the twig's sandbox extension in Symfony2?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload