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

Conversation

ThomasLandauer
Copy link
Contributor

Added example for custom function

Added example for custom function
);
}

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

);
}

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...

@javiereguiluz javiereguiluz added this to the 2.8 milestone Oct 30, 2018
javiereguiluz added a commit that referenced this pull request Oct 30, 2018
This PR was squashed before being merged into the 2.8 branch (closes #10593).

Discussion
----------

Update twig_extension.rst

Added example for custom function

Commits
-------

32e3179 Update twig_extension.rst
@javiereguiluz
Copy link
Member

Thomas, thanks a lot for contributing these missing docs ... and for your patience during the long review process!

@ThomasLandauer
Copy link
Contributor Author

patience during the long review process

@javiereguiluz Never mind, three days is not long... ;-)

...at least compared to this one here: #10180
I'd really appreciate if you could accelerate that! :-)

@ThomasLandauer ThomasLandauer deleted the patch-5 branch October 30, 2018 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants