我正在使用JSRender来渲染模板。
我正在使用演示中的第一个示例 -
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery.js" type="text/javascript"></script>
<script src="../../jsrender.js" type="text/javascript"></script>
<link href="../resources/demos.css" rel="stylesheet" type="text/css" />
</head>
<body>
<a href="../demos.html">JsRender Demos</a><br />
<h3>Render template against local data</h3>
<script id="movieTemplate" type="text/x-jsrender">
<!-- This thing i want to change dynamically -->
<div>
{{:#index+1}}:<b>{{:Close}}</b> ({{:Open}})
</div>
</script>
<div id="movieList"></div>
<script type="text/javascript">
var movies = [
{ name: "The Red Violin", releaseYear: "1998" },
{ name: "Eyes Wide Shut", releaseYear: "1999" },
{ name: "The Inheritance", releaseYear: "1976" }
];
// Render the template with the movies data and insert
// the rendered HTML under the "movieList" element
$( "#movieList" ).html(
$( "#movieTemplate" ).render( movies )
);
</script>
</body>
</html>
我需要的是在运行时更改模板。这是<div>
<script id=movieTemplate">
内容
任何人都可以帮我解决这个问题。
感谢分享你的智慧。
答案 0 :(得分:2)
您可以通过$ .getJSON下载eample的模板,然后执行类似
的操作$.getJSON('http://YourServer.com/yourTemplate.txt', function(res) {
$.templates({ tmpl: res });
$("#movieList").html($.render.tmpl(movies ));
});
HTH