From db1b03b13f00f760039e18fe74aa30fc5e8b639e Mon Sep 17 00:00:00 2001 From: Thomas Landauer Date: Sat, 27 Oct 2018 23:14:29 +0200 Subject: [PATCH 1/3] Update twig_extension.rst Added example for custom function --- templating/twig_extension.rst | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/templating/twig_extension.rst b/templating/twig_extension.rst index e9a58badd5d..1f863bf022b 100644 --- a/templating/twig_extension.rst +++ b/templating/twig_extension.rst @@ -62,9 +62,32 @@ As an example you'll create a price filter to format a given number into price:: versions before 1.26, include this method which is omitted in the example above. +Here's how to create a custom **function**:: + + // src/AppBundle/Twig/AppExtension.php + namespace AppBundle\Twig; + + use Twig\Extension\AbstractExtension; + use Twig\TwigFunction; + + class AppExtension extends AbstractExtension + { + public function getFunctions() + { + return array( + new TwigFunction('total', array($this, 'totalFunction')), + ); + } + + public function totalFunction(float $price, int $quantity) + { + return $price * $quantity; + } + } + .. tip:: - Along with custom filters, you can also add custom `functions`_ and register + Along with custom filters and functions, you can also register `global variables`_. Register an Extension as a Service From 23ef8d63f1fd464e1ef217009a8a55e8ccffbf8c Mon Sep 17 00:00:00 2001 From: Thomas Landauer Date: Mon, 29 Oct 2018 12:05:27 +0100 Subject: [PATCH 2/3] Update twig_extension.rst As requested :-) https://github.com/symfony/symfony-docs/pull/10593 --- templating/twig_extension.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/templating/twig_extension.rst b/templating/twig_extension.rst index 1f863bf022b..4ee6330d771 100644 --- a/templating/twig_extension.rst +++ b/templating/twig_extension.rst @@ -41,11 +41,11 @@ As an example you'll create a price filter to format a given number into price:: public function getFilters() { return array( - new TwigFilter('price', array($this, 'priceFilter')), + new TwigFilter('price', array($this, 'formatPrice')), ); } - public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',') + public function formatPrice($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',') { $price = number_format($number, $decimals, $decPoint, $thousandsSep); $price = '$'.$price; @@ -75,11 +75,11 @@ Here's how to create a custom **function**:: public function getFunctions() { return array( - new TwigFunction('total', array($this, 'totalFunction')), + new TwigFunction('total', array($this, 'calculateTotal')), ); } - public function totalFunction(float $price, int $quantity) + public function calculateTotal(float $price, int $quantity) { return $price * $quantity; } From 04e7e45c60b22b84405fa690c4ecac77fa30c607 Mon Sep 17 00:00:00 2001 From: Thomas Landauer Date: Tue, 30 Oct 2018 13:15:56 +0100 Subject: [PATCH 3/3] Update twig_extension.rst See https://github.com/symfony/symfony-docs/pull/10593#discussion_r229209283 --- templating/twig_extension.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templating/twig_extension.rst b/templating/twig_extension.rst index 4ee6330d771..e1b5350ab78 100644 --- a/templating/twig_extension.rst +++ b/templating/twig_extension.rst @@ -75,13 +75,13 @@ Here's how to create a custom **function**:: public function getFunctions() { return array( - new TwigFunction('total', array($this, 'calculateTotal')), + new TwigFunction('total', array($this, 'calculateArea')), ); } - public function calculateTotal(float $price, int $quantity) + public function calculateArea(int $width, int $length) { - return $price * $quantity; + return $width * $length; } }