Get controller name in TWIG template - symfony

I am learning symfony2.3, and I am getting an error when I try to get controller name in twig template.
Controller:
namespace Acme\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function indexAction($name)
{
return $this->render('AcmeAdminBundle:Default:index.html.twig', array('name' => $name));
}
}
In my TWIG template:
{% extends '::base.html.twig' %}
{% block body %}
{{ app.request.get('_template').get('controller') }}
Hello {{ name }}!!!
{% endblock %}
Output:
Impossible to invoke a method ("get") on a NULL variable ("") in AcmeAdminBundle:Default:index.html.twig at line 3
I want output as "Default"
I am using symfony 2.3, I have also tried on symfony 2.1 but on both version generates the same error.

use this line to display a controller name in twig:
{{ app.request.attributes.get("_controller") }}

Many months ago I had the same problem as you, and "googling" I found a working code and I had adapted it to my necessities. Here we go:
1 - We need to define a TWIG extension for that. We create the folder structure Your\OwnBundle\Twig\Extension if you haven't defined yet.
2 - Inside this folder we create the file ControllerActionExtension.php which code is:
namespace Your\OwnBundle\Twig\Extension;
use Symfony\Component\HttpFoundation\Request;
/**
* A TWIG Extension which allows to show Controller and Action name in a TWIG view.
*
* The Controller/Action name will be shown in lowercase. For example: 'default' or 'index'
*
*/
class ControllerActionExtension extends \Twig_Extension
{
/**
* #var Request
*/
protected $request;
/**
* #var \Twig_Environment
*/
protected $environment;
public function setRequest(Request $request = null)
{
$this->request = $request;
}
public function initRuntime(\Twig_Environment $environment)
{
$this->environment = $environment;
}
public function getFunctions()
{
return array(
'get_controller_name' => new \Twig_Function_Method($this, 'getControllerName'),
'get_action_name' => new \Twig_Function_Method($this, 'getActionName'),
);
}
/**
* Get current controller name
*/
public function getControllerName()
{
if(null !== $this->request)
{
$pattern = "#Controller\\\([a-zA-Z]*)Controller#";
$matches = array();
preg_match($pattern, $this->request->get('_controller'), $matches);
return strtolower($matches[1]);
}
}
/**
* Get current action name
*/
public function getActionName()
{
if(null !== $this->request)
{
$pattern = "#::([a-zA-Z]*)Action#";
$matches = array();
preg_match($pattern, $this->request->get('_controller'), $matches);
return $matches[1];
}
}
public function getName()
{
return 'your_own_controller_action_twig_extension';
}
}
3 - After that we need to specify the service for TWIG to be recognized:
services:
your.own.twig.controller_action_extension:
class: Your\OwnBundle\Twig\Extension\ControllerActionExtension
calls:
- [setRequest, ["#?request="]]
tags:
- { name: twig.extension }
4 - Cache clear to make sure everything is ok:
php app/console cache:clear --no-warmup
5 - And now, if I'm not forgetting anything, you will be able to access those 2 methods in a TWIG template: get_controller_name() and get_action_name()
6 - Examples:
You are in the {{ get_action_name() }} action of the {{ get_controller_name() }} controller.
This will output something like: You are in the index action of the default controller.
You can also use to check:
{% if get_controller_name() == 'default' %}
Whatever
{% else %}
Blablabla
{% endif %}
And that's all!! I hope I helped you, mate :)
Edit: Take care about the clearing cache. If you don't use --no-warmup parameter maybe you will realize that nothing is shown in your templates. That's because this TWIG Extension uses the Request to extract the Controller and Action names. If you "warm up" the cache, the Request is not the same as a browser request, and the methods can return '' or null

Since Symfony 3.x, service request is replaced by request_stack, and Twig Extension declaration changed since Twig 1.12.
I will correct the answer of Dani (https://stackoverflow.com/a/17544023/3665477) :
1 - We need to define a TWIG extension for that. We create the folder structure AppBundle\Twig\Extension if you haven't defined yet.
2 - Inside this folder we create the file ControllerActionExtension.php which code is:
<?php
namespace AppBundle\Twig\Extension;
use Symfony\Component\HttpFoundation\RequestStack;
class ControllerActionExtension extends \Twig_Extension
{
/** #var RequestStack */
protected $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('getControllerName', [$this, 'getControllerName']),
new \Twig_SimpleFunction('getActionName', [$this, 'getActionName'])
];
}
/**
* Get current controller name
*
* #return string
*/
public function getControllerName()
{
$request = $this->requestStack->getCurrentRequest();
if (null !== $request) {
$pattern = "#Controller\\\([a-zA-Z]*)Controller#";
$matches = [];
preg_match($pattern, $request->get('_controller'), $matches);
return strtolower($matches[1]);
}
}
/**
* Get current action name
*
* #return string
*/
public function getActionName()
{
$request = $this->requestStack->getCurrentRequest();
if (null !== $request) {
$pattern = "#::([a-zA-Z]*)Action#";
$matches = [];
preg_match($pattern, $request->get('_controller'), $matches);
return $matches[1];
}
}
public function getName()
{
return 'controller_action_twig_extension';
}
}
3 - After that we need to specify the service for TWIG to be recognized:
app.twig.controller_action_extension:
class: AppBundle\Twig\Extension\ControllerActionExtension
arguments: [ '#request_stack' ]
tags:
- { name: twig.extension }
4 - Cache clear to make sure everything is ok:
php bin/console cache:clear --no-warmup
5 - And now, if I'm not forgetting anything, you will be able to access those 2 methods in a TWIG template: getControllerName() and getActionName()
6 - Examples:
You are in the {{ getActionName() }} action of the {{ getControllerName() }} controller.
This will output something like: You are in the index action of the default controller.
You can also use to check:
{% if getControllerName() == 'default' %}
Whatever
{% else %}
Blablabla
{% endif %}

I don't really see WHY you would need this.
You might better send parameters into your view.
But if you really need it this way, here's a solution:
Your error comes from the second get method
request = app.request // Request object
NULL = request.get('_template') // Undefined attribute, default NULL
NULL.get('controller') // Triggers error
If you want to get the controller called during the request you can access it via the key _controller of the request attribute
app.request.attribute.get('_controller')
Will return
Acme\AdminBundle\Controller\DefaultController::indexAction
You can then parse it the way you want.
Note that this won't return the controller instance, only its name and method called

To get the controller - {{ app.request.attributes.get('_controller') }}
To get the Action - {{ app.request.attributes.get('_template').get('name') }}
Found at - http://forum.symfony-project.org/viewtopic.php?f=23&t=34083

It can vary. If you're using annotations in the controller, e.g. #Template("AcmeDemoBundle:Default:index"), trying to access app.request.get('_template') in your Twig template will return a string e.g. "AcmeDemoBundle:Default:index". So you might need to access it like this:
{% set _template = app.request.get('_template')|split(':') %}
{% set controller = _template[1] %}
{% set bundle = _template[0] %}
If you're not using annotations then you can use the app.request.get('_template').get('_controller')

Controller:
{{ app.request.attributes.get('_template').get('controller') }}
Action:
{{ app.request.attributes.get('_template').get('name') }}
enjoy ;)

Related

is it possible to call an entity from view ( php template ) in symfony2

I got a dynamic content in layout which is takes vales from database. What is the best way to achieve this rather than passing values from controller.
Is it possible to call an entity from view? I am using php template.
there's no point in a MVC context to call a model entity without the use of a controller, at least you can fetch the updated content using a ajax call to a controller which returns a JsonResponse to avoid the page refresh
This is possible via the twig extension.
Register a twig extension
TWIG EXTENSION
Pass to __constructor() - #doctrine service
services.yml
my.twig.extension:
class: twig\namespace\path
arguments:
kernel: "#kernel"
doctrine: "#doctrine"
tags:
- { name: twig.extension }
Constructor of the new twig extension
protected $kernel;
protected $doctrine;
public function __construct($kernel, $doctrine)
{
$this->kernel = $kernel;
$this->doctrine = $doctrine;
}
Write up some method:
/** #var string $repository. Example: AppBundle:Product' */
public function myEntity($repository)
{
$manager = $this->doctrine->getManager();
return $manager->getRepository($repository);
}
Register myEntity method in twig extension:
public function getFunctions()
{
return array(
'myEntity' => new \Twig_Function_Method($this, 'myEntity'),
);
}
Now in your twig templates you can access any repository:
For example:
{#
myEntity('SomeBundle:coolEntity').find()
myEntity('SomeBundle:coolEntity').findAll()
myEntity('SomeBundle:coolEntity').findBy()
...
#}
{% for item in myEntity('SomeBundle:coolEntity').findAll() %}
{{ item.getId() }}
{% endfor %}

How to get list of all routes of a controller in Symfony2?

I have a controller which implements all routes/URL(s).
I had the idea to offer a generic index over all help-pages.
Is there a way to get all routes defined by a controller (from within a controller) in Symfony2?
What you can do is use the cmd with (up to SF2.6)
php app/console router:debug
With SF 2.7 the command is
php app/console debug:router
With SF 3.0 the command is
php bin/console debug:router
which shows you all routes.
If you define a prefix per controller (which I recommend) you could for example use
php app/console router:debug | grep "<prefixhere>"
to display all matching routes
To display get all your routes in the controller, with basically the same output
I'd use the following within a controller (it is the same approach used in the router:debug command in the symfony component)
/**
* #Route("/routes", name="routes")
* #Method("GET")
* #Template("routes.html.twig")
*
* #return array
*/
public function routeAction()
{
/** #var Router $router */
$router = $this->get('router');
$routes = $router->getRouteCollection();
foreach ($routes as $route) {
$this->convertController($route);
}
return [
'routes' => $routes
];
}
private function convertController(\Symfony\Component\Routing\Route $route)
{
$nameParser = $this->get('controller_name_converter');
if ($route->hasDefault('_controller')) {
try {
$route->setDefault('_controller', $nameParser->build($route->getDefault('_controller')));
} catch (\InvalidArgumentException $e) {
}
}
}
routes.html.twig
<table>
{% for route in routes %}
<tr>
<td>{{ route.path }}</td>
<td>{{ route.methods|length > 0 ? route.methods|join(', ') : 'ANY' }}</td>
<td>{{ route.defaults._controller }}</td>
</tr>
{% endfor %}
</table>
Output will be:
/_wdt/{token} ANY web_profiler.controller.profiler:toolbarAction
etc.
You could get all of the routes, then create an array from that and then pass the routes for that controller to your twig.
It's not a pretty way but it works.. for 2.1 anyways..
/** #var $router \Symfony\Component\Routing\Router */
$router = $this->container->get('router');
/** #var $collection \Symfony\Component\Routing\RouteCollection */
$collection = $router->getRouteCollection();
$allRoutes = $collection->all();
$routes = array();
/** #var $params \Symfony\Component\Routing\Route */
foreach ($allRoutes as $route => $params)
{
$defaults = $params->getDefaults();
if (isset($defaults['_controller']))
{
$controllerAction = explode(':', $defaults['_controller']);
$controller = $controllerAction[0];
if (!isset($routes[$controller])) {
$routes[$controller] = array();
}
$routes[$controller][]= $route;
}
}
$thisRoutes = isset($routes[get_class($this)]) ?
$routes[get_class($this)] : null ;
I was looking to do just that and after searching the code, I came up with this solution which works for a single controller (or any ressource actually). Works on Symfony 2.4 (I did not test with previous versions) :
$routeCollection = $this->get('routing.loader')->load('\Path\To\Controller\Class');
foreach ($routeCollection->all() as $routeName => $route) {
//do stuff with Route (Symfony\Component\Routing\Route)
}
If anyone is stumbling on this issue, this is how I exported the routes in the global twig scope (symfony 4).
src/Helper/Route.php
<?php
namespace App\Helper;
use Symfony\Component\Routing\RouterInterface;
class Routes
{
private $routes = [];
public function __construct(RouterInterface $router)
{
foreach ($router->getRouteCollection()->all() as $route_name => $route) {
$this->routes[$route_name] = $route->getPath();
}
}
public function getRoutes(): array
{
return $this->routes;
}
}
src/config/packages/twig.yaml
twig:
globals:
route_paths: '#App\Helper\Routes'
 
Then in your twig file to populate a javascript variable to use in your scripts
<script>
var Routes = {
{% for route_name, route_path in routes_service.routes %}
{{ route_name }}: '{{ route_path }}',
{% endfor %}
}
</script>
In Symfony 4 i wanted to get all the routes including controller and actions in one list. In rails you can get this by default.
In Symfony you need to add the parameter show-controllers to the debug:router command.
If somebody looking for the same feature it can be get with:
bin/console debug:router --show-controllers
this will produce a list like the following
------------------------------------------------------------------------- -------------------------------------
Name Method Scheme Host Path Controller
------------------------------------------------------------------------- -------------------------------------
app_some_good_name ANY ANY ANY /example/example ExampleBundle:Example:getExample
------------------------------------------------------------------------- -------------------------------------
The safest way to proceed is to use the symfony controller resolver, because you never know if your controller is defined as a fully qualified class name, a service, or whatever callable declaration.
foreach ($this->get('router')->getRouteCollection() as $route) {
$request = new Request();
$request->attributes->add($route->getDefaults());
[$service, $method] = $this->resolver->getController($request);
// Do whatever you like with the instanciated controller
}
Because you needed to get the route information from within a Controller, you can make use of Symfony's Autowiring. If it is configured properly, you can just pass RouterInterface to any routes in a Controller.
#[Route('/', name: 'app_default')]
public function someRoute(RouterInterface $router): JsonResponse
{
$routesPaths = [];
foreach ($router->getRouteCollection()->all() as $routeName => $route) {
$routesPaths["$routeName"] = $route->getPath();
}
return new JsonResponse([
'availableRoutes' => $routesPaths,
]);
}
However if you need this logic in multiple Controllers you should encapsulate the logic in a seperate class like #C Alex did in one of his answers.

Symfony2 - checking if file exists

I have a loop in Twig template, which returns multiple values. Most important - an ID of my entry. When I didn't use any framework nor template engine, I used simply file_exists() within the loop. Now, I can't seem to find a way to do it in Twig.
When I display user's avatar in header, I use file_exists() in controller, but I do it because I don't have a loop.
I tried defined in Twig, but it doesn't help me. Any ideas?
If you want want to check the existence of a file which is not a twig template (so defined can't work), create a TwigExtension service and add file_exists() function to twig:
src/AppBundle/Twig/Extension/TwigExtension.php
<?php
namespace AppBundle\Twig\Extension;
class FileExtension extends \Twig_Extension
{
/**
* Return the functions registered as twig extensions
*
* #return array
*/
public function getFunctions()
{
return array(
new Twig_SimpleFunction('file_exists', 'file_exists'),
);
}
public function getName()
{
return 'app_file';
}
}
?>
Register your service:
src/AppBundle/Resources/config/services.yml
# ...
parameters:
app.file.twig.extension.class: AppBundle\Twig\Extension\FileExtension
services:
app.file.twig.extension:
class: %app.file.twig.extension.class%
tags:
- { name: twig.extension }
That's it, now you are able to use file_exists() inside a twig template ;)
Some template.twig:
{% if file_exists('/home/sybio/www/website/picture.jpg') %}
The picture exists !
{% else %}
Nope, Chuck testa !
{% endif %}
EDIT to answer your comment:
To use file_exists(), you need to specify the absolute path of the file, so you need the web directory absolute path, to do this give access to the webpath in your twig templates
app/config/config.yml:
# ...
twig:
globals:
web_path: %web_path%
parameters:
web_path: %kernel.root_dir%/../web
Now you can get the full physical path to the file inside a twig template:
{# Display: /home/sybio/www/website/web/img/games/3.jpg #}
{{ web_path~asset('img/games/'~item.getGame.id~'.jpg') }}
So you'll be able to check if the file exists:
{% if file_exists(web_path~asset('img/games/'~item.getGame.id~'.jpg')) %}
I've created a Twig function which is an extension of the answers I have found on this topic. My asset_if function takes two parameters: the first one is the path for the asset to display. The second parameter is the fallback asset, if the first asset does not exist.
Create your extension file:
src/Showdates/FrontendBundle/Twig/Extension/ConditionalAssetExtension.php:
<?php
namespace Showdates\FrontendBundle\Twig\Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConditionalAssetExtension extends \Twig_Extension
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* Returns a list of functions to add to the existing list.
*
* #return array An array of functions
*/
public function getFunctions()
{
return array(
'asset_if' => new \Twig_Function_Method($this, 'asset_if'),
);
}
/**
* Get the path to an asset. If it does not exist, return the path to the
* fallback path.
*
* #param string $path the path to the asset to display
* #param string $fallbackPath the path to the asset to return in case asset $path does not exist
* #return string path
*/
public function asset_if($path, $fallbackPath)
{
// Define the path to look for
$pathToCheck = realpath($this->container->get('kernel')->getRootDir() . '/../web/') . '/' . $path;
// If the path does not exist, return the fallback image
if (!file_exists($pathToCheck))
{
return $this->container->get('templating.helper.assets')->getUrl($fallbackPath);
}
// Return the real image
return $this->container->get('templating.helper.assets')->getUrl($path);
}
/**
* Returns the name of the extension.
*
* #return string The extension name
*/
public function getName()
{
return 'asset_if';
}
}
Register your service (app/config/config.yml or src/App/YourBundle/Resources/services.yml):
services:
showdates.twig.asset_if_extension:
class: Showdates\FrontendBundle\Twig\Extension\ConditionalAssetExtension
arguments: ['#service_container']
tags:
- { name: twig.extension }
Now use it in your templates like this:
<img src="{{ asset_if('some/path/avatar_' ~ app.user.id, 'assets/default_avatar.png') }}" />
I've had the same problem as Tomek. I've used Sybio's solution and made the following changes:
app/config.yml => add "/" at the end of web_path
parameters:
web_path: %kernel.root_dir%/../web/
Call file_exists without "asset" :
{% if file_exists(web_path ~ 'img/games/'~item.getGame.id~'.jpg') %}
Hope this helps.
Here is my solution, using SF4, autowire and autoconfigure:
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Symfony\Component\Filesystem\Filesystem;
class FileExistsExtension extends AbstractExtension
{
private $fileSystem;
private $projectDir;
public function __construct(Filesystem $fileSystem, string $projectDir)
{
$this->fileSystem = $fileSystem;
$this->projectDir = $projectDir;
}
public function getFunctions(): array
{
return [
new TwigFunction('file_exists', [$this, 'fileExists']),
];
}
/**
* #param string An absolute or relative to public folder path
*
* #return bool True if file exists, false otherwise
*/
public function fileExists(string $path): bool
{
if (!$this->fileSystem->isAbsolutePath($path)) {
$path = "{$this->projectDir}/public/{$path}";
}
return $this->fileSystem->exists($path);
}
}
In services.yaml:
services:
App\Twig\FileExistsExtension:
$projectDir: '%kernel.project_dir%'
In templates:
# Absolute path
{% if file_exists('/tmp') %}
# Relative to public folder path
{% if file_exists('tmp') %}
I am new to Symfony so every comments are welcome!
Also, as initial question is about Symfony 2, maybe my answer is not relevant and I would better ask a new question and answer by myself?
Improving on Sybio's answer, Twig_simple_function did not exist for my version and nothing here works for external images for example. So my File extension file is like this:
namespace AppBundle\Twig\Extension;
class FileExtension extends \Twig_Extension
{
/**
* {#inheritdoc}
*/
public function getName()
{
return 'file';
}
public function getFunctions()
{
return array(
new \Twig_Function('checkUrl', array($this, 'checkUrl')),
);
}
public function checkUrl($url)
{
$headers=get_headers($url);
return stripos($headers[0], "200 OK")?true:false;
}
Just add a little comment to the contribution of Sybio:
The Twig_Function_Function class is deprecated since version 1.12 and
will be removed in 2.0. Use Twig_SimpleFunction instead.
We must change the class Twig_Function_Function by Twig_SimpleFunction:
<?php
namespace Gooandgoo\CoreBundle\Services\Extension;
class TwigExtension extends \Twig_Extension
{
/**
* Return the functions registered as twig extensions
*
* #return array
*/
public function getFunctions()
{
return array(
#'file_exists' => new \Twig_Function_Function('file_exists'), // Old class
'file_exists' => new \Twig_SimpleFunction('file_exists', 'file_exists'), // New class
);
}
public function getName()
{
return 'twig_extension';
}
}
The rest of code still works exactly as said Sybio.

Instanceof operator in Twig/Symfony 2?

I've a mixed array like this one (mobile numbers and entities):
$targets = array();
$targets[] = '+32647651212';
$targets[] = new Customer();
In my Twig template i have to call getMobile() if target is a Customer or just print the number if it's actually a number (string).
Is there something like instanceof operator in Twig?
<ul>
{% for target in targets %}
<li>{{ target instance of MyEntity ? target.getMobile : target }}</li>
{% else %}
<li>Nothing found.</li>
</ul>
In \Twig_Extension you can add tests
public function getTests()
{
return [
'instanceof' => new \Twig_Function_Method($this, 'isInstanceof')
];
}
/**
* #param $var
* #param $instance
* #return bool
*/
public function isInstanceof($var, $instance) {
return $var instanceof $instance;
}
And then use like
{% if value is instanceof('DateTime') %}
UPDATE 10-2021
Please be aware this answer has been written for symfony 3, and twig 2.
If you use a more recent version, please refer to the answer of #Garri Figueroa on this post.
As you can see in the twig documentation the class \Twig_Extension, \Twig_SimpleTest are now deprecated.
If you use a more recent version of symfony (I recommend it), please use the new class AbstractExtension, TwigFunction, etc
https://symfony.com/doc/5.3/templating/twig_extension.html
OLD VERSION : symfony 3.4
Here a nice way to do instanceof operator in twig with Extension :
1) Create your extention file where you want
(ex: src/OC/YourBundle/Twig/InstanceOfExtension.php )
With \Twig_Extension you can do many things, filter, fonction, but now we will create a Test.
So we implement function getTests(), and inside it we create a new \Twig_SimpleTest
The 1st arugment is the name of test you create, and the seconde a callable.
(can be a function() {}).
<?php
namespace OC\YourBundle\Twig;
class InstanceOfExtension extends \Twig_Extension {
public function getTests() {
return array(
new \Twig_SimpleTest('instanceof', array($this, 'isInstanceOf')),
);
}
public function isInstanceOf($var, $instance) {
$reflexionClass = new \ReflectionClass($instance);
return $reflexionClass->isInstance($var);
}
}
2) Register it in services.yml
(ex: src/OC/YourBundle/Resources/config/services.yml)
services:
[...may you have other services ...]
app.twig_extension:
class: OC\YourBundle\Twig\InstanceOfExtension
public: false
tags:
- { name: twig.extension }
3) Then use it in twig like this
{{ myvar is instanceof('\\OC\\YourBundle\\Entity\\YourEntityOrWhatEver') }}
Source from Adrien Brault => https://gist.github.com/adrienbrault/7045544
My solution for Symfony 4.3
1) Create the AppExtension class in src/Twig folder. (The class is automatically detected).
2) Extend the AbstractExtension class:
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigTest;
class AppExtension extends AbstractExtension
{
public function getTests()
{
return [
new TwigTest('instanceof', [$this, 'isInstanceof'])
];
}
/**
* #param $var
* #param $instance
* #return bool
*/
public function isInstanceof($var, $instance) {
return $var instanceof $instance;
}
}
3) Then use same code of valdas.mistolis answer:
{% if value is instanceof('DateTime') %}
4) Thanks valdas.mistolis and symfony documentation i got my own solution:
Twig Extension templating
Since PHP 5.5.0 you can compare class names next way:
{{ constant('class', exception) is constant('\\Symfony\\Component\\HttpKernel\\Exception\\HttpException') }}
This snippet can help in particular cases when you need strict comparison of class names. If you need to check implementation of interface or to check inheritance would be better to create twig extension described above.
Another solution :
class A {
...
public function isInstanceOfB() {
return $this instanceof B;
}
}
class B extends A {}
class C extends A {}
then in your twig :
{{ a.isInstanceOfB ? ... something for B instance ... : ... something for C instance ... }}
OR
{% if a.isInstanceOfB %}
... do something for B instance ...
{% else %}
... do something for C instance ...
{% endif %}
Another example when iterating through Symfony forms:
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigTest;
use App\Document\Embeded\Image;
use App\Document\Embeded\Gallery;
use App\Document\Embeded\Article;
class AppExtension extends AbstractExtension
{
public function getTests()
{
return [
new TwigTest('image', [$this, 'isImage']),
new TwigTest('gallery', [$this, 'isGallery']),
new TwigTest('article', [$this, 'isArticle']),
];
}
public function isImage($var) {
return $var instanceof Image;
}
public function isGallery($var) {
return $var instanceof Gallery;
}
public function isArticle($var) {
return $var instanceof Article;
}
}
Twig
{% if form.vars.data is gallery %}
This is a Gallery
{% elseif form.vars.data is article %}
This is an Article
{% endif %}
Other solution without ReflectionClass with a twig filter :
First you need a TwigExtension class. Then, add a function as twig filter or twig function,
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class TwigExtension extends AbstractExtension
/**
* #return array|\Twig_Filter[]
*/
public function getFilters()
{
return [
new TwigFilter('is_instance_of', [$this, 'isInstanceOf'])
];
}
/**
* #param $object
* #param $class
* #return bool
*/
public function isInstanceOf($object, $class): bool
{
return is_a($object, $class, true);
}
}
And in twig template :
{% if true is same as (object|is_instance_of('ClassName')) %}
// do some stuff
{% endif %}
Check http://php.net/manual/fr/function.is-a.php
Use default filter in Twig like this:
{{ target.mobile|default(target) }}
Quite old, but I can't see here one more good possibility to achive this:
Enity:
public function __toString()
{
return 'NameOfYourEntity';
}
Twig:
{{ entity }}

Symfony 2 & Twig, how to access block from extension

I'm creating my Twig Extension to extend the actual "FormExtension".
Reason for that is that I need to create new functions without overwriting the current ones and making this available across my entire project.
So building and extension seemed to be the right way to go.
Building the extension is not a problem, my problem is how to render block from there?
What I understood till here, is that I need to create a Twig_Environment where I have to load my actual twig template (containing my blocks).
From there I should be able to render those block using "$mytemplate->displayBlock()".
Sample code:
public function renderWidgetinline(FormView $view, array $variables = array())
{
$loader = new \Twig_Loader_Filesystem(__DIR__.'/../Resources/views/Form');
$twig = new \Twig_Environment($loader);
$this->template = $twig->loadTemplate("form_layout.html.twig");
ob_start();
$this->template->displayBlock(???WHAT-PARAMS???);
$html = ob_get_clean();
return $html;
}
I found those information by looking at the Symfony base FormExtension.php file.
My questions are:
How does displayBlock() works, where can I found the defintion of that function?
Is what I described above the right way to go?
How should I proceed to have access to that new TWIG template together with the base form_div_layout.html template? Can I somehow get the current environment without having to recreated one and load my additional template there?
Thanks!
Have you tried to use renderBlock instead?
The first parameter you need is the name of the block, and the second should be an associative array of values passed to the block.
So what you would have in the case of a service that is rendering a block is the following:
The Service Class:
<?php
namespace Acme\BlockBundle\Blocks;
use Doctrine\Common\Persistence\ObjectManager;
Class Block {
private $om;
private $environment;
private $template;
public function __construct( ObjectManager $om, Twig $environment )
{
$this->om = $om;
$this->environment = $environment;
}
public function render( $template, $data )
{
$this->template = $this->environment->loadTemplate( $template );
// maybe query the DB via doctrine, that is why I have included $om
// in the service arguments
// example:
$entities = $om->getRepository( 'AcmePizzaBundle:Pizza' )->getMeatyOnes()
return $this->template->renderBlock( 'acme_block', array(
'data' => $entities,
));
}
}
The Twig Extension Class
<?php
namespace Acme\BlockBundle\Twig\Extension;
use Twig_Extension;
use Twig_Function_Method;
class BlockExtension extends Twig_Extension
{
protected $container;
public function __construct( $container )
{
$this->container = $container;
}
public function getName()
{
return 'block_extension';
}
public function getFunctions()
{
return array(
'render_block' => new Twig_Function_Method( $this, 'renderBlock', array(
'is_safe' => array( 'html' ),
)),
);
}
public function renderBlock( $template, $data )
{
return $this->container->get( 'acme.block' )->render( $template, $data );
}
}
The services.yml
parameters:
acme.blocks.block.class: Acme\BlocksBundle\Blocks\Block
acme.twig.block_extension.class: Acme\BlocksBundle\Twig\Extension\BlockExtension
services:
acme.blocks.block:
class: '%acme.blocks.block.class%'
arguments:
- '#doctrine.orm.entity_manager'
- '#twig'
acme.twig.block_extension:
class: %acme.twig.block_extension.class%
arguments:
- '#service_container'
tags:
- { name: twig.extension }
don't forget your template:
{% block acme_block %}
{% spaceless %}
{# do something with your data here #}
{% endspaceless %}
{% endblock acme_block %}
Then when you want to display it, you just need to call the twig function you have just created:
{{ render_block( '::block_template.html.twig', someDataOneThePage ) }}
By no mean this is a complete solution, but I have used something similar and it proved to be working.
HTH
Tam
[edit: April 2016 - for reference: this solution was working on a Symfony 2.4 project]

Resources