* 1.4.0
+ * added Twig_Test_Node to allow more complex tests to have their own Node class
* added a better error message when a template is empty but contain a BOM
* fixed in operator for empty strings
* fixed the "defined" test and the "default" filter (now works with more than one call (foo.bar.foo) and for both values of the strict_variables option)
'even' => new Twig_Test_Function('twig_test_even'),
'odd' => new Twig_Test_Function('twig_test_odd'),
'defined' => new Twig_Test_Function('twig_test_defined'),
- 'sameas' => new Twig_Test_Function('twig_test_sameas'),
+ 'sameas' => new Twig_Test_Node('Twig_Node_Expression_Test_Sameas'),
'none' => new Twig_Test_Function('twig_test_none'),
'null' => new Twig_Test_Function('twig_test_none'),
'divisibleby' => new Twig_Test_Function('twig_test_divisibleby'),
public function parseTestExpression(Twig_Parser $parser, $node)
{
$stream = $parser->getStream();
- $name = $stream->expect(Twig_Token::NAME_TYPE);
+ $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$arguments = null;
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
$arguments = $parser->getExpressionParser()->parseArguments();
}
- return new Twig_Node_Expression_Test($node, $name->getValue(), $arguments, $parser->getCurrentToken()->getLine());
+ $testMap = $parser->getEnvironment()->getTests();
+ if (isset($testMap[$name]) && $testMap[$name] instanceof Twig_Test_Node) {
+ $class = $testMap[$name]->getClass();
+ } else {
+ $class = 'Twig_Node_Expression_Test';
+ }
+
+ return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
}
/**
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2011 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Checks if a variable is the same as another one (=== in PHP).
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class Twig_Node_Expression_Test_Sameas extends Twig_Node_Expression_Test
+{
+ public function compile(Twig_Compiler $compiler)
+ {
+ $compiler
+ ->raw('(')
+ ->subcompile($this->getNode('node'))
+ ->raw(' === ')
+ ->subcompile($this->getNode('arguments')->getNode(0))
+ ->raw(')')
+ ;
+ }
+}
$this->env = $env;
}
+ public function getEnvironment()
+ {
+ return $this->env;
+ }
+
public function getVarName()
{
return sprintf('__internal_%s_%d', substr($this->env->getTemplateClass($this->stream->getFilename()), strlen($this->env->getTemplateClassPrefix())), ++$this->tmpVarCount);
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2010 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Represents a template test as a Node.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class Twig_Test_Node implements Twig_TestInterface
+{
+ protected $class;
+
+ public function __construct($class)
+ {
+ $this->class = $class;
+ }
+
+ public function getClass()
+ {
+ return $this->class;
+ }
+
+ public function compile()
+ {
+ }
+}