From 89edc52ae479576f162f3f3fc3558d94721b176f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 8 Jun 2010 16:47:55 +0200 Subject: [PATCH] [doc] updated documentation --- doc/04-Extending-Twig.markdown | 31 +++++++++++++++++++++++++------ 1 files changed, 25 insertions(+), 6 deletions(-) diff --git a/doc/04-Extending-Twig.markdown b/doc/04-Extending-Twig.markdown index bb4a287..a8ad8d3 100644 --- a/doc/04-Extending-Twig.markdown +++ b/doc/04-Extending-Twig.markdown @@ -2,7 +2,7 @@ Extending Twig ============== Twig supports extensions that can add extra tags, filters, or even extend the -parser itself with node transformer classes. The main motivation for writing +parser itself with node visitor classes. The main motivation for writing an extension is to move often used code into a reusable class like adding support for internationalization. @@ -13,6 +13,17 @@ to host all the specific tags and filters you want to add to Twig. >Before writing your own extensions, have a look at the Twig official extension >repository: http://github.com/fabpot/Twig-extensions. +Extending without an Extension (new in Twig 0.9.7) +-------------------------------------------------- + +If you just need to register a small amount of tags and/or filters, you can +register them without creating an extension: + + [php] + $twig = new Twig_Environment($loader); + $twig->addTokenParser(new CustomTokenParser()); + $twig->addFilter('upper', new Twig_Filter_Function('strtoupper')); + Anatomy of an Extension ----------------------- @@ -36,11 +47,11 @@ An extension is a class that implements the following interface: public function getTokenParsers(); /** - * Returns the node transformer instances to add to the existing list. + * Returns the node visitor instances to add to the existing list. * - * @return array An array of Twig_NodeTransformer instances + * @return array An array of Twig_NodeVisitorInterface instances */ - public function getNodeTransformers(); + public function getNodeVisitors(); /** * Returns a list of filters to add to the existing list. @@ -148,8 +159,16 @@ a string. Here is an example of its usage and the expected output: {# should displays Gjvt #} -Adding a filter in an extension means overriding the `getFilters()` method. -This method must return an array of filters to add to the Twig environment: +Adding a filter is as simple as calling the `addFilter` method of the +`Twig_Environment` instance (new in Twig 0.9.7): + + [php] + $twig = new Twig_Environment($loader); + $twig->addFilter('upper', new Twig_Filter_Function('strtoupper')); + +To add a filter to an extension, you need to override the `getFilters()` +method. This method must return an array of filters to add to the Twig +environment: [php] class Project_Twig_Extension extends Twig_Extension -- 1.7.2.5