From e8fa22a43b2c881642a7e7fe22e7ae33fdbc2cc1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 11 Mar 2011 13:29:40 +0100 Subject: [PATCH] added a recipe --- doc/recipes.rst | 20 ++++++++++++++++++-- 1 files changed, 18 insertions(+), 2 deletions(-) diff --git a/doc/recipes.rst b/doc/recipes.rst index d913920..d948cfa 100644 --- a/doc/recipes.rst +++ b/doc/recipes.rst @@ -209,8 +209,8 @@ In the inner loop, the ``loop.parent`` variable is used to access the outer context. So, the index of the current ``topic`` defined in the outer for loop is accessible via the ``loop.parent.loop.index`` variable. -Define undefined Functions and Filters on the Fly -------------------------------------------------- +Defining undefined Functions and Filters on the Fly +--------------------------------------------------- When a function (or a filter) is not defined, Twig defaults to throw a ``Twig_Error_Syntax`` exception. However, it can also call a `callback`_ (any @@ -240,4 +240,20 @@ does not return ``false``. As the resolution of functions and filters is done during compilation, there is no overhead when registering these callbacks. +Validating the Template Syntax +------------------------------ + +When template code is providing by a third-party (through a web interface for +instance), it might be interesting to validate the template syntax before +saving it. If the template code is stored in a `$template` variable, here is +how you can do it: + +try { + $twig->parse($twig->tokenize($template)); + + // the $template is valid +} catch (\Twig_Error_Syntax $e) { + // $template contains one or more syntax errors +} + .. _callback: http://www.php.net/manual/en/function.is-callable.php -- 1.7.2.5