From 3494bc233a7ae12ad8d932bd36cb7988b63ac6d8 Mon Sep 17 00:00:00 2001 From: fabien Date: Thu, 22 Oct 2009 01:56:15 +0000 Subject: [PATCH] added a debug tag git-svn-id: http://svn.twig-project.org/trunk@90 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- doc/02-Twig-for-Template-Designers.markdown | 18 ++++++++++++ lib/Twig/Extension/Core.php | 1 + lib/Twig/Node/Debug.php | 39 +++++++++++++++++++++++++++ lib/Twig/TokenParser/Debug.php | 23 ++++++++++++++++ 4 files changed, 81 insertions(+), 0 deletions(-) create mode 100644 lib/Twig/Node/Debug.php create mode 100644 lib/Twig/TokenParser/Debug.php diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index 08da161..876d8eb 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -582,6 +582,24 @@ to use them, they still need to be imported:

{{ forms.textarea('comment') }}

+### 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 ----------- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index eab044e..b0b625a 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -40,6 +40,7 @@ class Twig_Extension_Core extends Twig_Extension new Twig_TokenParser_Macro(), new Twig_TokenParser_Import(), new Twig_TokenParser_Set(), + new Twig_TokenParser_Debug(), ); } diff --git a/lib/Twig/Node/Debug.php b/lib/Twig/Node/Debug.php new file mode 100644 index 0000000..c8d7b31 --- /dev/null +++ b/lib/Twig/Node/Debug.php @@ -0,0 +1,39 @@ +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") + ; + } +} diff --git a/lib/Twig/TokenParser/Debug.php b/lib/Twig/TokenParser/Debug.php new file mode 100644 index 0000000..879db6c --- /dev/null +++ b/lib/Twig/TokenParser/Debug.php @@ -0,0 +1,23 @@ +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'; + } +} -- 1.7.2.5