1

Twig docs include a format_number filter, as of ver 2.12

My install shows Twig at ver 2.12.5, so should work, but throws a Twig Syntax Error: Unknown "format_percent_number" filter.

This started with not being able to use the Craft "percentage" filter because I was doing this:

{{ var1 / var2 | percentage }}

That filter fails because 'var1 / var2' returns a number without a leading zero and Craft does not like that.

So, two issues:

  1. the Twig filter format_number not work
  2. any thoughts on how to get around the Craft percentage filter issue

Thanks!

2

You're getting the error because of Twig's operator precedence. Filters take priority over math.

So basically Twig was trying change var2 to a percentage first instead of using the results of the math expression first, ie:

{{ var1 / (var2 | percentage) }}

Generally this behavior is usually what you want but sometimes there's edge cases where it doesn't work out. As you figured out, putting parenthesis around expressions to group them is never a bad idea especially when filters are involved.

2
  • Thanks for the explanation! – Roi Agneta Feb 18 at 16:31
  • @RoiAgneta feel free to accept this one (or yours for matter) as the accepted answer. – RitterKnight Feb 20 at 19:31
1

I got the Craft percentage filter to work by enclosing the calculation in parens, like this:

{{ (var1 / var2) | percentage }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.