exception instead (default to ``false``).
* ``autoescape``: If set to ``true``, auto-escaping will be enabled by default
- for all templates (default to ``true``).
+ for all templates (default to ``true``). As of Twig 1.8, you can set the
+ escaping strategy to use (``html``, ``js``, or ``false`` to disable).
* ``optimizations``: A flag that indicates which optimizations to apply
(default to ``-1`` -- all optimizations are enabled; set it to ``0`` to
* templates (default to Twig_Template).
*
* * cache: An absolute path where to store the compiled templates, or
- * false to disable compilation cache (default)
+ * false to disable compilation cache (default).
*
* * auto_reload: Whether to reload the template is the original source changed.
* If you don't provide the auto_reload option, it will be
* * strict_variables: Whether to ignore invalid variables in templates
* (default to false).
*
- * * autoescape: Whether to enable auto-escaping (default to true);
+ * * autoescape: Whether to enable auto-escaping (default to html):
+ * * false: disable auto-escaping
+ * * true: equivalent to html
+ * * html, js: set the autoescaping to one of the supported strategies
*
* * optimizations: A flag that indicates which optimizations to apply
* (default to -1 which means that all optimizations are enabled;
- * set it to 0 to disable)
+ * set it to 0 to disable).
*
* @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance
* @param array $options An array of options
'charset' => 'UTF-8',
'base_template_class' => 'Twig_Template',
'strict_variables' => false,
- 'autoescape' => true,
+ 'autoescape' => 'html',
'cache' => false,
'auto_reload' => null,
'optimizations' => -1,
$this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload'];
$this->extensions = array(
'core' => new Twig_Extension_Core(),
- 'escaper' => new Twig_Extension_Escaper((bool) $options['autoescape']),
+ 'escaper' => new Twig_Extension_Escaper($options['autoescape']),
'optimizer' => new Twig_Extension_Optimizer($options['optimizations']),
);
$this->strictVariables = (bool) $options['strict_variables'];
*/
class Twig_Extension_Escaper extends Twig_Extension
{
- protected $autoescape;
+ protected $defaultStrategy;
- public function __construct($autoescape = true)
+ public function __construct($defaultStrategy = 'html')
{
- $this->autoescape = $autoescape;
+ // for BC
+ if (true === $defaultStrategy) {
+ $defaultStrategy = 'html';
+ }
+
+ $this->defaultStrategy = $defaultStrategy;
}
/**
);
}
- public function isGlobal()
+ public function getDefaultStrategy()
{
- return $this->autoescape;
+ return $this->defaultStrategy;
}
/**
{
protected $statusStack = array();
protected $blocks = array();
-
protected $safeAnalysis;
protected $traverser;
return $this->statusStack[count($this->statusStack) - 1];
}
- if ($env->hasExtension('escaper') && $env->getExtension('escaper')->isGlobal()) {
- return 'html';
+ if ($env->hasExtension('escaper') && $defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy()) {
+ return $defaultStrategy;
}
return false;