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.
13 * Marks a section of a template as untrusted code that must be evaluated in the sandbox mode.
17 * {% include 'user.html' %}
21 * @see https://twig.symfony.com/doc/api.html#sandbox-extension for details
25 class Twig_TokenParser_Sandbox extends Twig_TokenParser
27 public function parse(Twig_Token $token)
29 $stream = $this->parser->getStream();
30 $stream->expect(Twig_Token::BLOCK_END_TYPE);
31 $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
32 $stream->expect(Twig_Token::BLOCK_END_TYPE);
34 // in a sandbox tag, only include tags are allowed
35 if (!$body instanceof Twig_Node_Include) {
36 foreach ($body as $node) {
37 if ($node instanceof Twig_Node_Text && ctype_space($node->getAttribute('data'))) {
41 if (!$node instanceof Twig_Node_Include) {
42 throw new Twig_Error_Syntax('Only "include" tags are allowed within a "sandbox" section.', $node->getTemplateLine(), $stream->getSourceContext());
47 return new Twig_Node_Sandbox($body, $token->getLine(), $this->getTag());
50 public function decideBlockEnd(Twig_Token $token)
52 return $token->test('endsandbox');
55 public function getTag()
61 class_alias('Twig_TokenParser_Sandbox', 'Twig\TokenParser\SandboxTokenParser', false);