使用するには、プラグインファイルを読み込みます。
jquery v1.4.2以上が必要です。
ファイルの読み込み
Microsoft CDNから読み込んでみる場合
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.5.min.js"></script> <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js">
参考サイト
- github - jQuery Templates plugin v1.0.0pre.
- jQuery APIドキュメント the jQuery Templates plugin (jquery-tmpl)
- idocsq.net [jQuery] jQueryの公式プラグイン - テンプレート(Template)
使用メモ
繰り返しタグ{{each}}{{/each}}の2重構造でも動いた
{{each 配列名}}でjsonデータに同名の配列があり、lengthが2以上だったら、自動的にループするのですが、配列の中に配列がある構造でも問題なく動作(ループ)しました。すばらしい。
//tmpl
{{each categories}}
<h2>${categoryName}</h2>
<ul>
{{each lists}}
<li><a href="#${url}">${name}</a></li>
{{/each}}
</ul>
{{/each}}
//json
{
"categories" : [
{ "categoryName" : "category1", "lists" : [
{ "name" : "sample1", "url" : "http://url1.sample/"},
{ "name" : "sample2", "url" : "http://url2.sample/"}
]},
{ "categoryName" : "category2", "lists" : [
{ "name" : "sample3", "url" : "http://url3.sample/"},
{ "name" : "sample4", "url" : "http://url4.sample/"}
]}
]
}