<p>{{ forms.textarea('comment') }}</p>
+### Debug
+
+Whenever a template does not work as expected, the debug tag can be used to
+output the content of the current context:
+
+ [twig]
+ {% debug %}
+
+You can also output a specific variable or an expression:
+
+ [twig]
+ {% debug items %}
+
+ {% debug post.body %}
+
+Note that this tag only works when the `debug` option of the environment is
+set to `true`.
+
Expressions
-----------
new Twig_TokenParser_Macro(),
new Twig_TokenParser_Import(),
new Twig_TokenParser_Set(),
+ new Twig_TokenParser_Debug(),
);
}
--- /dev/null
+<?php
+
+class Twig_Node_Debug extends Twig_Node
+{
+ protected $expr;
+
+ public function __construct(Twig_Node_Expression $expr = null, $lineno, $tag = null)
+ {
+ parent::__construct($lineno, $tag);
+
+ $this->expr = $expr;
+ }
+
+ public function compile($compiler)
+ {
+ $compiler->addDebugInfo($this);
+
+ $compiler
+ ->write("if (\$this->env->isDebug())\n", "{\n")
+ ->indent()
+ ->write('var_export(')
+ ;
+
+ if (null === $this->expr)
+ {
+ $compiler->raw('$context');
+ }
+ else
+ {
+ $compiler->subcompile($this->expr);
+ }
+
+ $compiler
+ ->raw(");\n")
+ ->outdent()
+ ->write("}\n")
+ ;
+ }
+}
--- /dev/null
+<?php
+
+class Twig_TokenParser_Debug extends Twig_TokenParser
+{
+ public function parse(Twig_Token $token)
+ {
+ $lineno = $token->getLine();
+
+ $expr = null;
+ if (!$this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE))
+ {
+ $expr = $this->parser->getExpressionParser()->parseExpression();
+ }
+ $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
+
+ return new Twig_Node_Debug($expr, $lineno, $this->getTag());
+ }
+
+ public function getTag()
+ {
+ return 'debug';
+ }
+}