问题描述
我在使用谷歌的引擎,但干涸了。
I have searched for a bit on a basic example on using the John Resig JavaScript Micro-Templating engine on Google but came out dry.
我决定把它带给基地人。任何人都可以帮助一个使用这个引擎的简单例子吗?我之前从未使用过客户端模板引擎。
I decided to bring it to base guys. Can anyone help with a simple example on using this engine? I have never used a client side template engine before.
更新:这是完整的HTML文档。感谢Will。
Update: This is the complete HTML document. Thanks to Will.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>John Resig JavaScript Micro-Templating engine</title>
<script src="jquery-1.3.2.js" type="text/javascript"></script>
<script src="MicroTemplates.js" type="text/javascript"></script>
<script type="text/javascript">
//Data
var data = { fname: "fred" };
function onloadFunction() {
var s = $("#biodata").html();
var s1 = tmpl(s, data);
$("#target").html(s1);
}
</script>
<script id="biodata" type="text/html">
<div><%= fname %></div>
</script>
</head>
<body onload="onloadFunction();">
<div id="target">
</div>
</body>
</html>
推荐答案
您提供的链接在引擎后立即有一个示例码。请阅读第2段。
The link you provided has an example immediately after the engine code. Read from the 2nd paragraph down.
CB,使用您的示例,这是我对引擎的看法,它在div标签之间输出fname的值。为了实现这一代,您可以执行以下操作:
CB, using your example, here is my take on the engine, which outputs the value of fname between div tags. To carry out the generation you would do something like:
var data = { fname : "fred" };
var generatedText = tmpl("biodata", data);
然后你必须输出它,例如。
Then you'd have to output it, eg.
document.write(generatedText);
或(假设页面中存在div,其id为'elemId')
Or (assuming a div exists in the page with the id of 'elemId')
var elem = document.getElementById("elemId");
elem.innerHTML = generatedText;
以上所有都是未经测试的,但希望是准确的。希望它有所帮助!
All the above is untested, but hopefully accurate. Hope it helps!
这篇关于如何使用John Resig JavaScript微模板引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!