<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<style type="text/css">
#template {
display: none;
}
</style>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script
src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
</head>
<body>
<div id="template">
<div id="event">
<button><%= title %></button>
</div>
</div>
<button id="cmd_create_event" name="cmd_create_event" type="button">Create
a new `EventModel`</button>
<div id="div_event_list"></div>
<script type="text/javascript">
$(document).ready(function() {
$("#cmd_create_event").click(function() {
var template = $("div#template div#event").children().html();
var compiled = _.template(template);
$("#div_event_list").html(compiled({
title : "New Event Title"
}));
});
});
</script>
</body>
</html>
The above would work perfectly if the html() call wouldn't return this: <%= title %>
I want it to return exactly <button><%= title %></button>, as I wrote it in the HTML, so I can use it for templating with underscore.
How?
Alternatively, is there any better way? Without extra dependency (jquery/underscore only). Without (too much) extra code.
_.templateSettings()which willchange Underscore's template settings to use different symbols to set off interpolated code. However, I'm still interested in hearing your opinions.