XF 2.2 Display a variable as a 'simple' number

Orit

Active member
Hi
How can I display a number variable as a 'simple' number in a template?

Example code:
HTML:
{{ $cost ? $cost : phrase('free') : }}
i.e. instead of displaying the $cost variable as 5.00 USD I'd like to simply display[COLOR=hsl(var(--xf-editorFocusColor))] [/COLOR]5.
Is there a built in function I can use, or only use JavaScript?
I found this post:
But can't figure out how to use the functions (and if they are any use to me...)

Thanks!
 
Solution
The |number filter is the correct one to use.

Using it as |number results in 0 decimal places.
Using it as |number(2) would result in 2 decimal places.

Try using it on something like {{ ($user.message_count/4.5)|number(2) }} with and without (2) to see how it works.

I'm not sure why it isn't working for your var.
This works for me - it displays as 5.

HTML:
<xf:set var="$cost" value="5.00" />
{{$cost ? $cost|number : 'Free' }}

Without the |number filter it displays as 5.00.
 
This works for me - it displays as 5.

HTML:
<xf:set var="$cost" value="5.00" />
{{$cost ? $cost|number : 'Free' }}

Without the |number filter it displays as 5.00.
I have a feeling that when you input "5.00" it is recognized as a string, and when you use the number filter you get the number itself.
my $cost variable is a number.
I just want to get the numbers before the decimal point. Preferably without using JavaScript...
 
The |number filter is the correct one to use.

Using it as |number results in 0 decimal places.
Using it as |number(2) would result in 2 decimal places.

Try using it on something like {{ ($user.message_count/4.5)|number(2) }} with and without (2) to see how it works.

I'm not sure why it isn't working for your var.
 
Solution
The |number filter is the correct one to use.

Using it as |number results in 0 decimal places.
Using it as |number(2) would result in 2 decimal places.

Try using it on something like {{ ($user.message_count/4.5)|number(2) }} with and without (2) to see how it works.

I'm not sure why it isn't working for your var.
You are right.
Thank you so much!

When dumping the variable I couldn't find it as $cost (I'm editing a template of an existing (not mine) add-on). I did quite a bit of digging in the relations and managed to find the correct variable.
I can confirm |number is indeed the way to go.

Thanks again!
 
Back
Top Bottom