Skip to content

Update twig_extension.rst #10593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions templating/twig_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, 'calculateTotal')),
);
}

public function calculateTotal(float $price, int $quantity)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry but earlier I forgot to mention that we should use another example for the Twig function. Some people will tell us that storing prices in float variables is a bad practice (and they are right!). Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, then what about just changing the typehint to int here?

BTW: I'm storing prices as float (Doctrine: decimal) too ;-) What would be a better way? Using integer to store € 4,00 as 400, and then somehow convert it in the entity's setter/getter?

Copy link
Member

@javiereguiluz javiereguiluz Oct 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. Money should be managed with a library like https://github.com/moneyphp/money to avoid rounding issues, storing in floats, etc.

So, let's find another example for a Twig function which doesn't deal with money. Do you have any idea? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the money hint!

I now committed some nonsense $with*$length example...

{
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
Expand Down