Okay, I know I can't literally call a twig template function from a controller, but to make links, I usually do the {{ path('_routeName') }}
and that's great.
However, now I want to formulate some links in the controller that will then be passed to the template via parameters like this:
$params = array(
'breadcrumbs' = array(
'Donuts' => '/donuts',
'Bearclaws' => '/donuts/bearclaws',
'Strawberry bearclaw' => null,
),
);
return $this->render('Bundle:Donut:info.html.twig', $params);
Except I don't want to hard-code those links. What I'd like is to be able to do
'Donuts' => path('_donutRoute'),
but how to reach the path method or equivalent?
You can get the current URL in Twig/Silex 2 like this: global. request. attributes. get('_route') .
It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher. Symfony PHP framework comes with a bundled support for Twig as its default template engine since version 2. Twig. Original author(s)
Twig is a template framework and is a direct replacement for PHP template. Twig extension provides more flexibility to process almost anything inside twig. Twig extends in many ways such as tags, filters, operators, global variables, and functions.
If your controller is extending the Symfony2
Controller (Symfony\Bundle\FrameworkBundle\Controller\Controller
) you can use the following to generate urls like this :
$this->generateUrl('_donutRoute')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With