直接上代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Jquery-templter的简单用法</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.tmpl.js"></script>
</head>
<body class="bg-primary">
<!-- 外部模板 -->
<script type="text/x-jquery-tmpl" id="myTemplter">
<h1>方法一</h1>
<div>标题:\${title}</div>
</script>
<!--
由于jsp中的EL表达式语法和jquery.tmpl十分类似,,所以单纯的使用${param},
数据是渲染不上tmpl的. SO.. 要加上转义: \${param}
-->
<script type="text/javascript">
$(function(){
/* 模拟数据 */
var json=[{"title":"xxx"}];
/* 方法一 外部声明模板 */
$("#myTemplter").tmpl(json).appendTo(".container");
/* 方法二 内部模板 */
var temp="<h1>方法二</h1> <div>标题:\${title}</div>";
$.template("myTemplter",temp);
$.tmpl("myTemplter",json).appendTo(".container");
});
</script>
<div class="container"> </div>
</body>
</html>