Revision 1852
Added by darkviper over 11 years ago
Escaper.php | ||
---|---|---|
10 | 10 |
*/ |
11 | 11 |
class Twig_Extension_Escaper extends Twig_Extension |
12 | 12 |
{ |
13 |
protected $autoescape;
|
|
13 |
protected $defaultStrategy;
|
|
14 | 14 |
|
15 |
public function __construct($autoescape = true)
|
|
15 |
public function __construct($defaultStrategy = 'html')
|
|
16 | 16 |
{ |
17 |
$this->autoescape = $autoescape;
|
|
17 |
$this->setDefaultStrategy($defaultStrategy);
|
|
18 | 18 |
} |
19 | 19 |
|
20 | 20 |
/** |
... | ... | |
49 | 49 |
); |
50 | 50 |
} |
51 | 51 |
|
52 |
public function isGlobal() |
|
52 |
/** |
|
53 |
* Sets the default strategy to use when not defined by the user. |
|
54 |
* |
|
55 |
* The strategy can be a valid PHP callback that takes the template |
|
56 |
* "filename" as an argument and returns the strategy to use. |
|
57 |
* |
|
58 |
* @param mixed $defaultStrategy An escaping strategy |
|
59 |
*/ |
|
60 |
public function setDefaultStrategy($defaultStrategy) |
|
53 | 61 |
{ |
54 |
return $this->autoescape; |
|
62 |
// for BC |
|
63 |
if (true === $defaultStrategy) { |
|
64 |
$defaultStrategy = 'html'; |
|
65 |
} |
|
66 |
|
|
67 |
$this->defaultStrategy = $defaultStrategy; |
|
55 | 68 |
} |
56 | 69 |
|
57 | 70 |
/** |
71 |
* Gets the default strategy to use when not defined by the user. |
|
72 |
* |
|
73 |
* @param string $filename The template "filename" |
|
74 |
* |
|
75 |
* @return string The default strategy to use for the template |
|
76 |
*/ |
|
77 |
public function getDefaultStrategy($filename) |
|
78 |
{ |
|
79 |
// disable string callables to avoid calling a function named html or js, |
|
80 |
// or any other upcoming escaping strategy |
|
81 |
if (!is_string($this->defaultStrategy) && is_callable($this->defaultStrategy)) { |
|
82 |
return call_user_func($this->defaultStrategy, $filename); |
|
83 |
} |
|
84 |
|
|
85 |
return $this->defaultStrategy; |
|
86 |
} |
|
87 |
|
|
88 |
/** |
|
58 | 89 |
* Returns the name of the extension. |
59 | 90 |
* |
60 | 91 |
* @return string The extension name |
... | ... | |
74 | 105 |
{ |
75 | 106 |
return $string; |
76 | 107 |
} |
77 |
|
Also available in: Unified diff
updated Twig template engine to stable version 1.11.1 step2