protected $charset;
protected $loader;
protected $trimBlocks;
+ protected $strictMode;
protected $debug;
protected $autoReload;
protected $cache;
* * auto_reload: Whether to reload the template is the original source changed.
* If you don't provide the auto_reload option, it will be
* determined automatically base on the debug value.
+ *
+ * * strict_mode: Whether to enable the strict mode or not. If enabled,
+ * everything must be explicit. For instance, all variables must be
+ * declared at the top of the template, you must pass explicitely the variables
+ * when including another template, and so on. If not, a StrictError is thrown.
*/
public function __construct(Twig_LoaderInterface $loader = null, $options = array())
{
$this->setLoader($loader);
}
+ $this->strictMode = isset($options['strict_mode']) ? (bool) $options['strict_mode'] : false;
$this->debug = isset($options['debug']) ? (bool) $options['debug'] : false;
$this->trimBlocks = isset($options['trim_blocks']) ? (bool) $options['trim_blocks'] : false;
$this->charset = isset($options['charset']) ? $options['charset'] : 'UTF-8';
$this->baseTemplateClass = $class;
}
+ public function enableStrictMode()
+ {
+ $this->strictMode = true;
+ }
+
+ public function disableStrictMode()
+ {
+ $this->strictMode = false;
+ }
+
+ public function isStrictMode()
+ {
+ return $this->strictMode;
+ }
+
public function enableDebug()
{
$this->debug = true;
new Twig_TokenParser_Import(),
new Twig_TokenParser_Set(),
new Twig_TokenParser_Debug(),
+ new Twig_TokenParser_Use(),
);
}
*/
public function getNodeVisitors()
{
- return array(new Twig_NodeVisitor_Filter());
+ return array(
+ new Twig_NodeVisitor_Filter(),
+ new Twig_NodeVisitor_StrictMode()
+ );
}
/**
class Twig_Node_Expression_Name extends Twig_Node_Expression
{
protected $name;
+ protected $declared = false;
public function __construct($name, $lineno)
{
public function compile($compiler)
{
- $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this->name, $this->name));
+ if ($this->declared)
+ {
+ $compiler->raw(sprintf('$context[\'%s\']', $this->name));
+ }
+ else
+ {
+ $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this->name, $this->name));
+ }
}
public function getName()
{
return $this->name;
}
+
+ public function setDeclared()
+ {
+ $this->declared = true;
+ }
}
{
switch ($type)
{
- case 0:
- return 'TEXT_TYPE';
- case -1:
- return 'EOF_TYPE';
- case 1:
- return 'BLOCK_START_TYPE';
- case 2:
- return 'VAR_START_TYPE';
- case 3:
- return 'BLOCK_END_TYPE';
- case 4:
- return 'VAR_END_TYPE';
- case 5:
- return 'NAME_TYPE';
- case 6:
- return 'NUMBER_TYPE';
- case 7:
- return 'STRING_TYPE';
- case 8:
- return 'OPERATOR_TYPE';
+ case self::EOF_TYPE:
+ $name = 'EOF_TYPE';
+ break;
+ case self::TEXT_TYPE:
+ $name = 'TEXT_TYPE';
+ break;
+ case self::BLOCK_START_TYPE:
+ $name = 'BLOCK_START_TYPE';
+ break;
+ case self::VAR_START_TYPE:
+ $name = 'VAR_START_TYPE';
+ break;
+ case selg::BLOCK_END_TYPE:
+ $name = 'BLOCK_END_TYPE';
+ break;
+ case self::VAR_END_TYPE:
+ $name = 'VAR_END_TYPE';
+ break;
+ case self::NAME_TYPE:
+ $name = 'NAME_TYPE';
+ break;
+ case self::NUMBER_TYPE:
+ $name = 'NUMBER_TYPE';
+ break;
+ case self::STRING_TYPE:
+ $name = 'STRING_TYPE';
+ break;
+ case self::OPERATOR_TYPE:
+ $name = 'OPERATOR_TYPE';
+ break;
+ default:
+ throw new InvalidArgumentException(sprintf('Token of type %s does not exist.', $type));
}
return $short ? $name : 'Twig_Token::'.$name;