Reputation: 26202
Since I'm using underscore templates with ruby I want to change default <% %>
tags which both ruby and underscore template use, so I setup the underscore to use other tags like this:
<script>
_.templateSettings = {
interpolate: /\[\=(.+?)\]/g,
evaluate: /\[(.+?)\]/g
};
</script>
So my template isn't that much complicated :
<script type="text/html" id='table_list_rows_template'>
[
_.each(items,function(obj,key){
var median = obj.md.toFixed(4)
]
<tr>
<td>[= key + 1]</td>
<td>[= obj.name]</td>
<td>[= median]</td>
</tr>
[ }); ]
So when I try to render html from template like this (tuples
is list of objects and my table has id table_list
) :
<script>
$(document).ready(function(){
var template = $("#table_list_rows_template").html();
$("#table_list tbody").html(_.template(template,{items:tuples}));
});
</script>
</script>
So I get the following error :
Uncaught SyntaxError: Unexpected token )
And it should be on this line :
$("#table_list tbody").html(_.template(template,{items:tuples}));
But I can't spot the error. What is wrong with my code?
UPDATE :
I've tried to change from [] to <? ?>
like php style because first one reminds of javascript array so I figured that might cause problems. See below :
_.templateSettings = {
interpolate: /<\?\=(.+?)\?>/g,
evaluate: /<\?(.+?)\?>/g
};
<script type="text/html" id='table_list_rows_template'>
<?
_.each(items,function(obj,key, list){
?>
<tr id='response_[= obj.id]'>
<td></td>
<td><?= obj.name ?></td>
</tr>
<? }); ?>
</script>
Made no difference same error.
Upvotes: 3
Views: 272
Reputation: 6344
Change your template settings from "." to "[\s\S]":
_.templateSettings = {
interpolate: /\[\=([\s\S]+?)\]/g,
evaluate: /\[([\s\S]+?)\]/g
};
http://jsfiddle.net/zBTMw/4/ with the \s\S
http://jsfiddle.net/zBTMw/5/ with the . (has the error)
The latest version of underscore.js uses the [\s\S] construct, which is where I got the idea.
Upvotes: 1