diff --git a/book/templating.rst b/book/templating.rst
index 3b005642ea5..d6f184e94f8 100644
--- a/book/templating.rst
+++ b/book/templating.rst
@@ -1334,6 +1334,7 @@ Learn more from the Cookbook
* :doc:`/cookbook/templating/PHP`
* :doc:`/cookbook/controller/error_pages`
+* :doc:`/cookbook/templating/twig_extension`
.. _`Twig`: http://twig.sensiolabs.org
.. _`KnpBundles.com`: http://knpbundles.com
diff --git a/cookbook/index.rst b/cookbook/index.rst
index 7e2da2c062f..7e84001f325 100644
--- a/cookbook/index.rst
+++ b/cookbook/index.rst
@@ -71,6 +71,7 @@ Cookbook
templating/global_variables
templating/PHP
+ templating/twig_extension
logging/monolog
logging/monolog_email
diff --git a/cookbook/map.rst.inc b/cookbook/map.rst.inc
index 301ad1ea01f..f65a331dbcd 100644
--- a/cookbook/map.rst.inc
+++ b/cookbook/map.rst.inc
@@ -92,6 +92,7 @@
* :doc:`/cookbook/templating/global_variables`
* :doc:`/cookbook/templating/PHP`
+ * :doc:`/cookbook/templating/twig_extension`
* **Logging**
diff --git a/cookbook/templating/twig_extension.rst b/cookbook/templating/twig_extension.rst
new file mode 100644
index 00000000000..6f105f4dfbc
--- /dev/null
+++ b/cookbook/templating/twig_extension.rst
@@ -0,0 +1,128 @@
+.. index::
+ single: Twig extensions
+
+How to write a custom Twig Extension
+====================================
+
+The main motivation for writing an extension is to move often used code
+into a reusable class like adding support for internationalization.
+An extension can define tags, filters, tests, operators, global variables,
+functions, and node visitors.
+
+Creating an extension also makes for a better separation of code that is
+executed at compilation time and code needed at runtime. As such, it makes
+your code faster.
+
+.. tip::
+
+ Before writing your own extensions, have a look at the `Twig official extension repository`_.
+
+Create the Extension Class
+--------------------------
+
+To get your custom functionality you must first create a Twig Extension class.
+As an example we will create a price filter to format a given number into price::
+
+ // src/Acme/DemoBundle/Twig/AcmeExtension.php
+
+ namespace Acme\DemoBundle\Twig;
+
+ use Twig_Extension;
+ use Twig_Filter_Method;
+ use Twig_Function_Method;
+
+ class AcmeExtension extends Twig_Extension
+ {
+ public function getFilters()
+ {
+ return array(
+ 'price' => new Twig_Filter_Method($this, 'priceFilter'),
+ );
+ }
+
+ public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
+ {
+ $price = number_format($number, $decimals, $decPoint, $thousandsSep);
+ $price = '$' . $price;
+
+ return $price;
+ }
+
+ public function getName()
+ {
+ return 'acme_extension';
+ }
+ }
+
+.. tip::
+
+ Along with custom filters, you can also add custom `functions` and register `global variables`.
+
+Register an Extension as a Service
+-------------------------------
+
+Now you must let Service Container know about your newly created Twig Extension:
+
+.. configuration-block::
+
+ .. code-block:: xml
+
+
+
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: