Define twig function for template include -
i have quick question:
i use project many different elements. have editable group them , include them template this:
{% include '::fields/special.html.twig' {'avg': 'avgvalue','class':'test','value':'myvalue'} %}
is there possibillity make shortcut function this, like:
{{ special(fieldvalues) }}
if so, how can that?
create twig function
like:
<?php // src\bw\mainbundle\twig\bwextension.php namespace bw\mainbundle\twig; use symfony\component\dependencyinjection\containerinterface; class bwextension extends \twig_extension { protected $container; public function __construct(containerinterface $container) { $this->container = $container; } public function getfunctions() { return array( new \twig_simplefunction('special', array($this, 'specialfunction')), ); } public function specialfunction($fieldvalues) { // handle of field values return $this->container->get('templating')->render('bwmainbundle:main:test.html.twig'); // return string of rendered twig template } }
and define service:
# src\bw\mainbundle\resources\config\services.yml services: bw_main.twig.bw_extension: class: bw\mainbundle\twig\bwextension arguments: ["@service_container"] tags: - { name: twig.extension }
Comments
Post a Comment