From 2d68c114645fd4331eef9c8696c9e81f8e42340c Mon Sep 17 00:00:00 2001 From: fabien Date: Mon, 14 Dec 2009 11:04:44 +0000 Subject: [PATCH] rearranged the doc git-svn-id: http://svn.twig-project.org/trunk@179 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- doc/04-Extending-Twig.markdown | 46 ++++++++++++++++++++++++++++++++++++++++ doc/06-Recipes.markdown | 46 ---------------------------------------- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/doc/04-Extending-Twig.markdown b/doc/04-Extending-Twig.markdown index 3dea0b3..a3da39f 100644 --- a/doc/04-Extending-Twig.markdown +++ b/doc/04-Extending-Twig.markdown @@ -142,6 +142,52 @@ function call: return $prefix.str_rot13($string); } +Overriding default Filters +-------------------------- + +If some default core filters do not suit your needs, you can easily override +them by creating your own core extension. Of course, you don't need to copy +and paste the whole core extension code of Twig. Instead, you can just extends +it and override the filter(s) by overriding the `getFilters()` method: + + [php] + class MyCoreExtension extends Twig_Extension_Core + { + public function getFilters() + { + return array_merge( + parent::getFilters(), + array( + 'date' => array('my_date_format_filter', false) + ) + ); + } + } + + function my_date_format_filter($timestamp, $format = 'F j, Y H:i') + { + return '...'.twig_date_format_filter($timestamp, $format); + } + +Here, we override the `date` filter with a custom one. Using this new core +extension is as simple as registering the `MyCoreExtension` extension by +calling the `addExtension()` method on the environment instance: + + [php] + $twig = new Twig_Environment($loader, array('debug' => true, 'cache' => false)); + $twig->addExtension(new MyCoreExtension()); + +But I can already hear some people wondering how it can work as the Core +extension is loaded by default. That's true, but the trick is that both +extensions share the same unique identifier (`core` - defined in the +`getName()` method). By registering an extension with the same name as an +existing one, you have actually overridden the default one, even if it is +already registered: + + [php] + $twig->addExtension(new Twig_Extension_Core()); + $twig->addExtension(new MyCoreExtension()); + Defining new Tags ----------------- diff --git a/doc/06-Recipes.markdown b/doc/06-Recipes.markdown index c2cdff1..07a4042 100644 --- a/doc/06-Recipes.markdown +++ b/doc/06-Recipes.markdown @@ -174,49 +174,3 @@ allow communication between your templates and your application: Now, you can use the setter to inject the context whenever you create a template, and use the getter from within your custom nodes. - -Overriding default Filters --------------------------- - -If some default core filters do not suit your needs, you can easily override -them by creating your own core extension. Of course, you don't need to copy -and paste the whole core extension code of Twig. Instead, you can just extends -it and override the filter(s) by overriding the `getFilters()` method: - - [php] - class MyCoreExtension extends Twig_Extension_Core - { - public function getFilters() - { - return array_merge( - parent::getFilters(), - array( - 'date' => array('my_date_format_filter', false) - ) - ); - } - } - - function my_date_format_filter($timestamp, $format = 'F j, Y H:i') - { - return '...'.twig_date_format_filter($timestamp, $format); - } - -Here, we override the `date` filter with a custom one. Using this new core -extension is as simple as registering the `MyCoreExtension` extension by -calling the `addExtension()` method on the environment instance: - - [php] - $twig = new Twig_Environment($loader, array('debug' => true, 'cache' => false)); - $twig->addExtension(new MyCoreExtension()); - -But I can already hear some people wondering how it can work as the Core -extension is loaded by default. That's true, but the trick is that both -extensions share the same unique identifier (`core` - defined in the -`getName()` method). By registering an extension with the same name as an -existing one, you have actually overridden the default one, even if it is -already registered: - - [php] - $twig->addExtension(new Twig_Extension_Core()); - $twig->addExtension(new MyCoreExtension()); -- 1.7.2.5