Backward incompatibilities:
* The short notation of the `block` tag changed.
+ * added ignore_invalid_variables setting to throw an exception when an invalid variable is used in a template (disabled automatically when debug is true)
* added the lexer, parser, and compiler as arguments to the Twig_Environment constructor
* changed the cache option to only accepts an explicit path to a cache directory or false
* added a way to add token parsers, filters, and visitors without creating an extension
protected $filters;
protected $runtimeInitialized;
protected $loadedTemplates;
+ protected $ignoreInvalidVars;
/**
* Constructor.
* If you don't provide the auto_reload option, it will be
* determined automatically base on the debug value.
*
+ * * ignore_invalid_variables: Whether to ignore invalid variables in templates (default to the opposite value of debug).
+ *
* @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance
* @param array $options An array of options
*/
$this->baseTemplateClass = isset($options['base_template_class']) ? $options['base_template_class'] : 'Twig_Template';
$this->autoReload = isset($options['auto_reload']) ? (bool) $options['auto_reload'] : $this->debug;
$this->extensions = array('core' => new Twig_Extension_Core());
+ $this->ignoreInvalidVars = isset($options['ignore_invalid_variables']) ? (bool) $options['ignore_invalid_variables'] : !$this->debug;
$this->runtimeInitialized = false;
if (isset($options['cache']) && $options['cache']) {
$this->setCache($options['cache']);
$this->autoReload = (Boolean) $autoReload;
}
+ public function enableignoreInvalidVars()
+ {
+ $this->ignoreInvalidVars = true;
+ }
+
+ public function disableignoreInvalidVars()
+ {
+ $this->ignoreInvalidVars = false;
+ }
+
+ public function ignoresInvalidVars()
+ {
+ return $this->ignoreInvalidVars;
+ }
+
public function getCache()
{
return $this->cache;
public function compile($compiler)
{
- $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this->name, $this->name));
+ $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $this->name, $this->name));
}
public function getName()
throw new Twig_RuntimeError(sprintf('The filter "%s" does not exist', $name));
}
+ protected function getContext($context, $item)
+ {
+ if (isset($context[$item])) {
+ return $context[$item];
+ }
+
+ if ($this->env->ignoresInvalidVars()) {
+ return null;
+ }
+
+ throw new InvalidArgumentException(sprintf('Item "%s" from context does not exist.', $item));
+ }
+
protected function getAttribute($object, $item, array $arguments = array(), $arrayOnly = false)
{
$item = (string) $item;
}
if ($arrayOnly || !is_object($object)) {
- return null;
+ if ($this->env->ignoresInvalidVars()) {
+ return null;
+ }
+
+ throw new InvalidArgumentException(sprintf('Key "%s" for array "%s" does not exist.', $item, $object));
}
if (isset($object->$item)) {
} elseif (isset($this->cache[$class]['__call'])) {
$method = $item;
} else {
- return null;
+ if ($this->env->ignoresInvalidVars()) {
+ return null;
+ }
+
+ throw new InvalidArgumentException(sprintf('Method "%s" for object "%s" does not exist.', $item, get_class($object)));
}
if ($this->env->hasExtension('sandbox')) {