php - Using vendor prefix for Twig Extension -
i trying add own extension twig. basic usage working want can call functions vendor prefix.
example: linkviewhelper:
class linkviewhelper extends \twig_extension { public function getfunctions() { return array( 'link' => new \twig_filter_method( $this, 'link', array('is_safe' => array('html')) ) ); } public function link($text, $uri, array $additionalparams = array(), array $atagparams = array(), $external=false) { $uri = $this->uri($uri, $additionalparams, $external); // additional parsing return $url; // somehting <a href="http://www.google.de" target="_blank">go google now!</a> } public function uri($uri, array $additionalparams = array(), $external = false) { // parsing goes here return $uri; // http://www.google.com/ } /** * returns name of extension. * * @return string extension name */ public function getname() { return 'url'; } }
loading in bootstrap:
$this->environment->addextension(new linkviewhelper());
now can call function {{ link }}
want call {{ url.link }}
. how can achieve this?
regards, cefurox
edit: seriously, down voting question because don't understand question isn't useful @ :)
okay thought , mentioned in reply post: if set extension global, can use vendor prefix. escape return if need html stuff me have echo return instead of returning it:
public function link($text, $uri, array $additionalparams = array(), array $atagparams = array(), $external=false) { $uri = $this->uri($uri, $additionalparams, $external); // additional parsing echo $url; // somehting <a href="http://www.google.de" target="_blank">go google now!</a> }
in case need isn't elegant @ all... let question open , won't mark right answer because think there have more elegant way this.
thnx @matteo bringing me global solution :)
regards,
sebastian
Comments
Post a Comment