rearranged the doc
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Mon, 14 Dec 2009 11:04:44 +0000 (11:04 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Mon, 14 Dec 2009 11:04:44 +0000 (11:04 +0000)
git-svn-id: http://svn.twig-project.org/trunk@179 93ef8e89-cb99-4229-a87c-7fa0fa45744b

doc/04-Extending-Twig.markdown
doc/06-Recipes.markdown

index 3dea0b3..a3da39f 100644 (file)
@@ -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
 -----------------
 
index c2cdff1..07a4042 100644 (file)
@@ -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());