JTemplate学習(四)
3784 ワード
注釈、カスタムメソッド、テンプレートネストサブテンプレート、異なるclassのループ出力
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<!-- saved from url=(0043)http://jtemplates.tpython.com/example4.html -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="Scripts/jTemplates/example/jquery.js"></script>
<script type="text/javascript" src="Scripts/jTemplates/jquery-jtemplates.js"></script>
<title>jTemplates</title>
<script type="text/javascript">
// data for testing
$(document).ready(function () {
var data = {
name: 'User list',
list_id: 4,
table: [
{ id: 1, name: 'Anne', age: 22, mail: '[email protected]' },
{ id: 2, name: 'Amelie', age: 24, mail: '[email protected]' },
{ id: 3, name: 'Polly', age: 18, mail: '[email protected]' },
{ id: 4, name: 'Alice', age: 26, mail: '[email protected]' },
{ id: 5, name: 'Martha', age: 25, mail: '[email protected]' }
],
toString: function () {
return this.name.bold() + ' (count: ' + this.table.length + ')';
}
};
// attach the template
$("#result1").setTemplateElement("template");
// process the template
$("#result1").processTemplate(data);
//{**}
//
});
</script>
<style type="text/css">
.jTemplatesTest {
background: #DDD;
border: 1px solid #000;
margin: 2em;
width: 480px;
}
.jTemplatesTest * {
padding: 4px;
margin: 2px auto;
}
.jTemplatesTest td, tr {
border: 1px solid black;
}
.bcEEC {
background: #EEC;
}
.bcCEE {
background: #CEE;
}
</style>
</head>
<body style="zoom: 1;">
<!-- Template content (Valid XHTML 1.1) -->
<p style="display: none">
<textarea id="template" rows="0" cols="0"><!--
{* *}
{#template MAIN}
{* this is a comment *}
{$T.toString()} {* $T == $T.toSource() *}
<table>
{#foreach $T.table as record}
{#include ROW root=$T.record}{* , *}
{#/for}
</table>
{#/template MAIN}
{* *}
{#template ROW}
<tr class="{#cycle values=['bcEEC','bcCEE']}">{* *}
<td>{$T.name}</td>
<td>{$T.mail}</td>
</tr>
{#/template ROW}
--></textarea>
</p>
<!-- Output elements -->
<div id="result1" class="jTemplatesTest">
</div>
</body>
</html>