How to use values from Handlebars helpers with math ? | Community
Skip to main content
December 4, 2023
Solved

How to use values from Handlebars helpers with math ?

  • December 4, 2023
  • 1 reply
  • 1343 views

Hi !

I created a loop to extract all the product prices.

I need to use those values in math calculations.

What is the right syntax to achieve that ?

 

{{#each profile.productsInCart}} This price = {{this.price}} <!-- This price = 22.88 (Working) --> This Price + TVA = {{ 1.7*this.price }} <!-- Not working --> This Price + TVA = {%= 1.7*this.price %} <!-- Not working --> This Price + TVA = {%= 1.7*{{this.price}} %} <!-- Not working --> {{/each}}

 

I can see the value for {{this.price}} , but I cannot use in other context (eg helper functions).

Thank you!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by AdrianCi

I have found the issue, posting it here for others.

 

the query result It needs to be assigned to a variable (don't relay on default "this"), only then it can be referenced on functions:

 

{{#each profile.productsInCart as |product|}} This price = {{product.price}} <!-- This price = 100 (Working) --> Sum = {%= 100 + 100 %} <!-- Sum = 200 (Working) --> Sum = {%= product.price + product.price %} <!-- Sum = 200 (Working) --> {{/each}}

 

1 reply

SatheeskannaK
Community Advisor
Community Advisor
December 4, 2023

@adrianci, Did you try these functions mentioned in the documentation?

Thanks, Sathees
AdrianCiAuthor
December 5, 2023

Hi SatheeskannaK

yes I have used those functions, they work with static values but not with dynamic values, coming from the Handlebars loop:

 

{{#each profile.productsInCart}} This price = {{this.price}} <!-- This price = 100 (Working) --> Sum = {%= 100 + 100 %} <!-- Sum = 200 (Working) --> Sum = {%= {{this.price}} + {{this.price}} %} <!-- Not Working --> {{/each}}

 

 

 

AdrianCiAuthorAccepted solution
December 5, 2023

I have found the issue, posting it here for others.

 

the query result It needs to be assigned to a variable (don't relay on default "this"), only then it can be referenced on functions:

 

{{#each profile.productsInCart as |product|}} This price = {{product.price}} <!-- This price = 100 (Working) --> Sum = {%= 100 + 100 %} <!-- Sum = 200 (Working) --> Sum = {%= product.price + product.price %} <!-- Sum = 200 (Working) --> {{/each}}