Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2+Twig - Using TwigBundle's path() in my own filter
    text
    copied!<p>I'm trying to create a Twig extension for my project. The goal is to generate a user popup with the jquery.cluetip plugin, which can display the result of an AJAX query in a nice tooltip. The HTML code generated should look like this:</p> <pre><code>&lt;span class="userdata" rel="/profile/123/ajaxUserdata.html"&gt;W00d5t0ck&lt;/span&gt; </code></pre> <p>Currently, my code looks like this:</p> <pre><code>class UserDataSpanExtension extends \Twig_Extension { protected $_securityContext; public function __construct(SecurityContextInterface $security) { $this-&gt;_securityContext = $security; } public function getFilters() { return array( 'userdataspan' =&gt; new \Twig_Filter_Method($this, 'getUserDataSpan', array('is_safe' =&gt; array('html'))), ); } public function getUserDataSpan(User $user) { if (!is_object($this-&gt;_securityContext-&gt;getToken()) || !is_object($this-&gt;_securityContext-&gt;getToken()-&gt;getUser())) return '&lt;span class="userdata-secret" title="|User information|User information is available only to logged in users."&gt;[not available]&lt;/span&gt;'; return '&lt;span class="userdata" rel=""&gt;' . $user-&gt;getDisplayName() . '&lt;/span&gt;'; } public function getName() { return 'twig_userdataspan'; } } </code></pre> <p>Now I'm a bit stuck as I don't know how should I fill my <code>rel</code> attribute. In a Twig template I would use</p> <pre><code>{{ path('AcmeDemoBundle_ajaxUserdata', { id: user.id }) }} </code></pre> <p>but this won't work in a Twig extension. Could anyone point me at a usable example?</p>
 

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