symfony - Creating my first twig extension to provide global variables to base templates -
i need populate variable html code , make available base.html.twig file.
to achive have made twig extension. first time using twig extentions im not sure if correct way of doing things.
here have far:
extension code:
class globalfooterextension extends \twig_extension { public function getfilters() { return array( new \twig_filter_function('globalfooter', array($this, 'globalfooter')), ); } public function globalfooter() { $globalfooter = file_get_contents('http://mysite.co.uk/footer/footer.html.twig'); return $globalfooter; } public function getname() { return 'globalfooter_extention'; } }
config.yml:
services: imagine.twig.globalfooterextension: class: imagine\gdmbundle\twig\globalfooterextension tags: - { name: twig.extension }
base.html.twig:
{{globalfooter}}
this give following error:
twig_error_runtime: variable "globalfooter" not exist in "imaginegdmbundle:default:product.html.twig" @ line 2
im sure im missing obvious. how make $globalfooter globalfooterextension class available base.hmtl.twig file?
you want set global variable, not function.
just use getglobals
, return variable:
class globalfooterextension extends \twig_extension { public function getglobals() { return array( "globalfooter" => file_get_contents('http://mysite.co.uk/footer/footer.html.twig'), ); } public function getname() { return 'globalfooter_extention'; } }
or, if want lazy load value of variable, create function , change template to:
{{ globalfooter() }}
besides this, if footer file on same site, it's better use {% include '...' %}
tag.
Comments
Post a Comment