Created
October 21, 2014 07:45
-
-
Save kachar/b2773353ef25a8f3100e to your computer and use it in GitHub Desktop.
twig extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Project_Set_TokenParser extends \Twig_TokenParser | |
{ | |
public function parse(\Twig_Token $token) | |
{ | |
$parser = $this->parser; | |
$stream = $parser->getStream(); | |
$name = $stream->expect(\Twig_Token::NAME_TYPE)->getValue(); | |
$stream->expect(\Twig_Token::OPERATOR_TYPE, '('); | |
$value = $parser->getExpressionParser()->parseExpression(); | |
$stream->expect(\Twig_Token::BLOCK_END_TYPE); | |
return new Project_Set_Node($name, $value, $token->getLine(), $this->getTag()); | |
} | |
public function getTag() | |
{ | |
return 'trans'; | |
} | |
} | |
class Project_Set_Node extends \Twig_Node | |
{ | |
public function __construct($name, \Twig_Node_Expression $value, $line, $tag = null) | |
{ | |
parent::__construct(array('value' => $value), array('name' => $name), $line, $tag); | |
} | |
public function compile(\Twig_Compiler $compiler) | |
{ | |
$compiler | |
->addDebugInfo($this) | |
->write('$this->translate(\''.$this->getAttribute('name')) | |
->subcompile($this->getNode('value')) | |
->write('\')') | |
->raw(";\n") | |
; | |
} | |
} | |
public function getTokenParsers() | |
{ | |
return array(new Project_Set_TokenParser); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment