After you compile a template, you find yourself with a function to call, and you pass in some arguments. So just pass your function as argument:
var tpl = _.template( tplString );
tpl({ someFunction: function( val ) { /* do something */ } });
Although, I think you're probably better if you only pass value inside your template data. Template are actually way easier to debug and maintain if they're almost logic less. So, instead, I'd go like this:
tpl({ someValue: someFunction( aValue ) });
In other javascript templating engine (like Handlebars), you can actually register helpers functions who'll be mostly available globally to execute action on your template data. If you really need to use the same function inside multiple templates, I'd think about switching template engine. Underscore provide a micro-templating engine, and as so, is somehow limited around helpers functions - although being able to contain way too much logic...
In my opinion, underscore template works well for small project and should be use carefully. Keep them as simple as possible: if/else
, printing data, and that should be it. If you need more, go for a more complete template engine.
But even there, all logic you do inside a template is hard to debug.
So! Keep it simple.