Skip to content Skip to sidebar Skip to footer

Call Javascript Function From Jsrender

I have a JsRender template. When template is rendering I want to call a JavaScript function to manipulate some data. Here is my JsRender template.

Solution 1:

You can do this by using a helper function. When you call render, you can define and pass a helper function 'into' your template:

$("#tmpl").render(data, {
    getSize: function(size) {
        var megabytes = size / (1024 * 1024);
        return megabytes.toFixed(2) + "MB";
    }
});

Then, in your template, you call the helper function like this:

<tr class="template-download">
   <td class="size">
        <span>{{:size}} {{:~getSize(size)}} </span>
   </td>
</tr>

I've made a couple of assumptions about how your template is being used, but this should get you going.

This article has a section related to helper functions, and @BorisMoore has a number of good examples on his jsViews site.


Solution 2:

I believe you must use helper functions. See the two references below. The 2nd is a fully flushed out example.

http://borismoore.github.io/jsrender/demos/step-by-step/09_helper-functions.html


Post a Comment for "Call Javascript Function From Jsrender"