4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
15 class Twig_Extension_Escaper extends Twig_Extension
17 protected $defaultStrategy;
20 * @param string|false|callable $defaultStrategy An escaping strategy
22 * @see setDefaultStrategy()
24 public function __construct($defaultStrategy = 'html')
26 $this->setDefaultStrategy($defaultStrategy);
29 public function getTokenParsers()
31 return array(new Twig_TokenParser_AutoEscape());
34 public function getNodeVisitors()
36 return array(new Twig_NodeVisitor_Escaper());
39 public function getFilters()
42 new Twig_SimpleFilter('raw', 'twig_raw_filter', array('is_safe' => array('all'))),
47 * Sets the default strategy to use when not defined by the user.
49 * The strategy can be a valid PHP callback that takes the template
50 * name as an argument and returns the strategy to use.
52 * @param string|false|callable $defaultStrategy An escaping strategy
54 public function setDefaultStrategy($defaultStrategy)
57 if (true === $defaultStrategy) {
58 @trigger_error('Using "true" as the default strategy is deprecated since version 1.21. Use "html" instead.', E_USER_DEPRECATED);
60 $defaultStrategy = 'html';
63 if ('filename' === $defaultStrategy) {
64 @trigger_error('Using "filename" as the default strategy is deprecated since version 1.27. Use "name" instead.', E_USER_DEPRECATED);
66 $defaultStrategy = 'name';
69 if ('name' === $defaultStrategy) {
70 $defaultStrategy = array('Twig_FileExtensionEscapingStrategy', 'guess');
73 $this->defaultStrategy = $defaultStrategy;
77 * Gets the default strategy to use when not defined by the user.
79 * @param string $name The template name
81 * @return string|false The default strategy to use for the template
83 public function getDefaultStrategy($name)
85 // disable string callables to avoid calling a function named html or js,
86 // or any other upcoming escaping strategy
87 if (!is_string($this->defaultStrategy) && false !== $this->defaultStrategy) {
88 return call_user_func($this->defaultStrategy, $name);
91 return $this->defaultStrategy;
94 public function getName()
101 * Marks a variable as being safe.
103 * @param string $string A PHP variable
107 function twig_raw_filter($string)
112 class_alias('Twig_Extension_Escaper', 'Twig\Extension\EscaperExtension', false);