ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.31k stars 382 forks source link

How to get SUM in {{ ? }} #186

Closed joviqiao closed 8 years ago

joviqiao commented 8 years ago

data sometime need add , subtract , multiply and divide, How to do it? if i create helper, numerous functions...: data: [{name:user1,buytimes:2},{name:user2,buytimes:4},...] now, i need get all of buytimes, How to do it in page?

hguillermo commented 8 years ago

You need a helper. Check here: https://github.com/assemble/handlebars-helpers

If it doesn't fit your needs then you can write your own. It is pretty simple.

joviqiao commented 8 years ago

@hguillermo Thx, it work, but {{sum [user.tj1num,user.tj2num,user.tj3num] }} {{sum "[{{user.tj1num}},{{user.tj2num}},{{user.tj3num}}]" }} How to create array can get sum ?

hguillermo commented 8 years ago

You are trying to use a helper inside a helper. The syntax is different. Check this: http://stackoverflow.com/questions/14781916/is-it-possible-to-nest-helpers-inside-the-options-hash-with-handlebars

If not you can create your own helper and pass the array and return the sum or whatever you need.

joviqiao commented 8 years ago

Thx

joviqiao commented 8 years ago
{{ user }}
<tr>
    <td>{{user.Phone}}</td>
    <td>{{user.name}}</td>
    <td>
        <select class="trendSet">
            <option {{if user.trend eq 0}}selected="selected"{{/if}} value="0">No trend</option>
            <option {{if user.trend eq 1}}selected="selected"{{/if}} value="1">trend</option>
            <option {{if user.trend eq 2}}selected="selected"{{/if}} value="2">wait</option>
            <option {{if user.trend eq 3}}selected="selected"{{/if}} value="3">want</option>
        </select>
    </td>
</tr>

Hi @hguillermo above, my problem is: user.trend ( backend data ) judge eq number, like ==, + - * / , there have backend data. Thx

shone83 commented 6 years ago

@hguillermo How to write my own sum array helper function and use it in {{sum array}} handlebars without install handlebars-helpers? Can you help?

hguillermo commented 6 years ago

@shone83 I would like to help you but it is been many years without handlebars. I barely remember it. I am fully working on React the last 3 years. I am sure there are still some tutorials on the internet.

shone83 commented 6 years ago

Ok, you said it's pretty simple so I thought that could do it.