-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Update twig_extension.rst #10593
Conversation
Added example for custom function
templating/twig_extension.rst
Outdated
); | ||
} | ||
|
||
public function totalFunction(float $price, int $quantity) |
There was a problem hiding this comment.
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(...)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for formatPrice()
and calculateTotal()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure :-)
#10593
As requested :-) symfony#10593
templating/twig_extension.rst
Outdated
); | ||
} | ||
|
||
public function calculateTotal(float $price, int $quantity) |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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...
Thomas, thanks a lot for contributing these missing docs ... and for your patience during the long review process! |
@javiereguiluz Never mind, three days is not long... ;-) ...at least compared to this one here: #10180 |
Added example for custom function