I need to send a parameter to all my views (twig template) with Symfony2.
Each view extend a special view : base.html.twig, so I think I just need to send this parameter to this base view.
Here is the way I get this parameter in my php file:
$svn = File('.svn/entries');
$svnrev = $svn[3];
Which represents the number of current revision.
.yml?You should register a global variable using a twig extension.
// src/Acme/YourBundle/Twig/SvnExtension.php
namespace Acme\YourBundle\Twig;
class SvnExtension extends \Twig_Extension
{
public function getGlobals()
{
// you might need to adapt the file path
$svn = File('.svn/entries');
return array(
'svn_rev' => $svn[3]
);
}
public function getName()
{
return 'svn_extension';
}
}
# i.e. app/config/config.yml
services:
acme.twig.svn_extension:
class: Acme\DemoBundle\Twig\SvnExtension
tags:
- { name: twig.extension }
Now your variable is accessible in every twig template as usual:
{{ svn_rev }}
Further improvements would include:
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