How to do simple math in a meteor pattern?

Let's say I have a simple collection of product_orders in Meteor (_id, user_id, user_name, product_name, price_unit, quantity), and I want to show all the orders of one user in the table, where each row should contain:

user_name, product_name, quantity, price_unit, quantity, price_total (price_unit * quantity) 

In addition, I would like to show the total amount for all custom orders.

I see no easy way to do this in the Handlebars.js template, because Handlebars does not support simple math operations. I can easily return the product_order cursor to my template, but I don’t see a simple way to calculate price_total and the total in the template.

I am thinking of creating a template helper, but not sure if this is the right direction. This problem looks as if it should have a simple and elegant solution.

+6
source share
1 answer

Yes, you should write an assistant. Pens do not support the use of logic in patterns (which is good practice as it forces you to apply problem pattern splitting).

The template helper looks like this:

 Handlebars.registerHelper("grandTotal", function(user_name) { var grandTotal = some_magic_to_calc_total(user_name); return grandTotal; }); 

Then you can call a helper like this from your template:

 <template name="foo"> {{grandTotal user_name}} </template> 

You can read more about helpers in the Handlebars.js Docs .

+13
source

All Articles