IT戦記

プログラミング、起業などについて書いているプログラマーのブログです😚

JsonML を使う高速テンプレートエンジン「JsonML.Template」

というのを作ってみた

JsonML とは

JSON を使ったマークアップ言語です。
http://jsonml.org/

使いかた
// テンプレートの作成
// <div class="section"><h2>タイトル</h2><p>本文本文本文</p></div>
var t = $T(['div',
  {class: 'section'},
  ['h2', 'タイトル'],
  ['p', '本文本文本文本文']
]);

// 要素の生成
document.body.appendChild(t()); // 呼び出すだけ
// テンプレートの作成
var t = $T(['div',
  {class: 'section'},
  ['h3', function(c){return c.getVar('title')}], // タイトルを動的に生成
  ['p', function(c){return c.getVar('content')}] // コンテンツを動的に生成
]);

button.onclick = function(title, content) {
  document.body.appendChild(t({title: title, content: content})); // 変数付きで呼び出す
};
詳しくは、このリンクを参照してください。

http://usrb.in/amachang/wiki/JsonML.Template