In my symfony2 application, I have a getPorfolioUser method which return a specific user variable.
I am looking forward to be able to call
{%
I was having some difficult with this as well and finally solved it by doing the following:
Setup your bundle (e.g: MyVendor/MyBundle)
$ app/console generate:bundle
In this class file, create the function
public function getExample(){
return "it works!!!";
}
In app/config/services.yml create a new service like so:
myvendor.mybundle.myservice
class: MyVendor\MyBundle\DependencyInjection\MyService
In app/config/config.yml under the twig configuration section
twig:
globals:
mystuff: '@myvendor.mybundle.myservice'
Then in your twig templates you can reference the variable like so:
{{ mystuff.example }}
DISCLAIMER
this is just how I got it to work....
hope this helps.
When you look here : http://symfony.com/doc/current/reference/twig_reference.html#app
You can read this :
The app variable is available everywhere and gives access to many commonly needed objects and values. It is an instance of GlobalVariables.
GlobalVariables
is Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables
I never do it but I think one way is to overide this class in order to put your special needs in.
One approach is use a CONTROLLER event listener. I like to use CONTROLLER instead of REQUEST because it ensures that all the regular request listeners have done their thing already.
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProjectEventListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array
(
KernelEvents::CONTROLLER => array(
array('onControllerProject'),
),
);
}
private $twig;
public function __construct($twig)
{
$this->twig = $twig;
}
public function onControllerProject(FilterControllerEvent $event)
{
// Generate your data
$project = ...;
// Twig global
$this->twig->addGlobal('project',$project);
}
# services.yml
cerad_project__project_event_listener:
class: ...\ProjectEventListener
tags:
- { name: kernel.event_subscriber }
arguments:
- '@twig'
Listeners are documented here: http://symfony.com/doc/current/cookbook/service_container/event_listener.html
Another approach would be to avoid the twig global altogether and just make a twig extension call. http://symfony.com/doc/current/cookbook/templating/twig_extension.html
Either way works well.
You can define your custom service as twig globals variable
as follow:
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
globals:
myGlobaService: "@acme.demo_portfolio_service" #The id of your service
{% if myGlobaService.portfolio_user() %}
Hope this help