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 1 commit
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
25 changes: 24 additions & 1 deletion templating/twig_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be:
Public function total(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't know what you mean exactly. See above line:

new TwigFunction('total', array($this, 'totalFunction')),

Do you mean it should be changed there too?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes please, sorry for not being that clear, but Function suffix is not necessary at all

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, sure. But since the corresponding filter function (above) also carries the useless "Filter" suffix (priceFilter()) at https://symfony.com/doc/2.8/templating/twig_extension.html#create-the-extension-class I figured to go along with it. In the Twig docs they're recommending neither: https://twig.symfony.com/doc/2.x/advanced.html#id2

So I'm fine to delete it, but in this case the "Filter" suffix should probably be deleted too, shouldn't it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, how shall we proceed @javiereguiluz ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we could do some renaming here:

priceFilter() -> formatPrice()
totalFunction() -> getTotal() or calculateTotal()

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 for formatPrice() and calculateTotal()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure :-)
#10593

{
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