MTã®ç®¡çç»é¢ã§MTæ¨æºã®JSãã³ãã¬ã¼ãã¨ã³ã¸ã³ã使ã
管çç»é¢ã®ã«ã¹ã¿ãã¤ãºããã¦ãã¦ãJSã§DOMãå¼ã£ã¦HTMLãåºåãããã¨æã£ãæã«ãã³ã¼ãã®è¦éãã確ä¿ããããã«JSãã³ãã¬ã¼ãã¨ã³ã¸ã³ã欲ãããªã¨æãã¾ããã
ãã ããã®ããã«ã©ã¤ãã©ãªèªã¿è¾¼ãã®ãå¾®å¦ã ããæ£è¦è¡¨ç¾ã§é å¼µããã¨æã£ãã¨ããã管çç»é¢ã§JSãã³ãã¬ã¼ãã¨ã³ã¸ã³ã使ããã¦ããã¨ãæãåºãã¾ããã
MTã®ç®¡çç»é¢ã§ä½¿ããã¦ãããJSãã³ãã¬ã¼ãã¨ã³ã¸ã³ã®ã©ã¤ãã©ãªã¯ã/path/to/mt-static/js/common/Template.js
ã«ãã£ã¦ãå®è£
ã確èªããã¨ãããã·ã³ãã«ã§æ±ç¨çã«ä½¿ããå°ããªã©ã¤ãã©ãªã£ã½ãã®ã§ãä¸è¨ã®éãæ©è½ãã¾ã¨ãã¾ããã
ã¡ãªã¿ã«ãã®ã©ã¤ãã©ãªã¯ãæ°å¹´åããæ´æ°ãæ¢ã¾ã£ã¦ãã¦ãGithub ãªãã¸ããªã®å½è©²ãã¡ã¤ã«ã®ã³ãããå±¥æ´ãããCommon JavaScript ã©ã¤ãã©ãªããªãã¸ããªã«å ¥ãã¾ãããçãªãã°ãä¸ã¤ããã ãã®ã¬ãã«ã§ãã
ã¡ã³ããæ¢ã¾ã£ã¦ãã¦ããã¤å½¹ç®ãçµãããããããªãæãã®åå¨æãªã®ã§ããã®ç¹ã¯ãçæãã ããã
ãã®è¨äºã®è³å³æéãããã¾ã§ã§ãã
使ãæ¹
ä¸è¨ã®ãããªæãã§ä½¿ãã¾ãã
var tmpl = new Template('Hello, [#= name #]!'); var context = new Template.Context({ name: 'taiju' }); tmpl.process(context); // => "Hello, taiju!" /* ä¸è¨ã§ä½¿ãå¤æ°tmpl_textã«ã¯ä¸è¨ã®ãã³ãã¬ã¼ãæååãä¿åããã¦ããã¨ä»®å®ãã <ul> [# jQuery.each(vars, function (i, v) { #] <li>[#|h|u v#]</li> [# }) #] </ul> */ tmpl.compile(tmpl_text); context.vars = { vars: ["<script>alert('foo');</script>", "ãã¼", "baz"] }; tmpl.process(context); /* => "<ul> <li><script>alert('foo');<%2Fscript></li> <li>%E3%83%90%E3%83%BC</li> <li>baz</li> </ul>"*/
Underscore.jsã®ãã³ãã¬ã¼ãæ©è½ã®ããã«ãæ®éã«è¨èªã®æ©è½ããã®ã¾ã¾ä½¿ããã¿ã¤ãã®ã·ã³ãã«ãªãã³ãã¬ã¼ãã¨ã³ã¸ã³ã§ãããã®æã®ãã³ãã¬ã¼ãã¯å¦ç¿ã³ã¹ããä½ããè¨èªã®ãã¯ã¼ããã®ã¾ã¾ä½¿ããã®ã§ãå人çã«å¥½ãã¿ã¤ãã§ãã
管çç»é¢ã¯jQueryããã¼ãããã¦ããã®ã§ãjQueryã®ã¦ã¼ãã£ãªãã£ãçµã¿åããã¦ãã³ãã¬ã¼ããæ¸ãã®ãè¯ãããããã¾ããã
ã©ã¤ãã©ãªã®æ©è½
Template.jsã«ãã£ã¦ãTemplate, Template.Context, Template.Filterã¨ããã¯ã©ã¹ãæä¾ããã¾ãã
Template.jsã¯ãCore.jsã«ä¾åãã¦ããã®ã§ãå©ç¨ããã«ã¯Core.jsçã®ä¾åã©ã¤ãã©ãªããããããèªã¿è¾¼ã¾ãã¦ããå¿ è¦ãããã¾ãã
主ã«ä½¿ãã®ã¯Templateã¯ã©ã¹ã«ãªãããã§ãããå ã®ãµã³ãã«ã®ããã«Templateã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãä½ã£ã¦å¦çããä»ãTemplateã¯ã©ã¹ã®ã¯ã©ã¹ã¡ã½ããã使ã£ã¦ãJSãã³ãã¬ã¼ãããã«ããããã¨ãã§ãã¾ãã
var templates = { foo: '[#= foo #]' }; var vars = { foo: 'FOO' }; Template.process('foo', vars, templates); // => "FOO"
ã¯ã©ã¹ã¡ã½ããã®æ¹ãæ軽ã«ä½¿ããããããã¾ããã
ãã³ãã¬ã¼ãã®è¨æ³
[#
ãã#]
ã§å²ã¾ããé¨åã JS ãã³ãã¬ã¼ãã®è¨è¿°é¨åã«ãªãã¾ãã
JavaScript ã®ä»»æã®å¼ãæãè¨è¿°ã§ããå¼ã®çµæãåºåããããåºåçµæã«ãã£ã«ã¿ãããããã§ãã¾ããã¾ããTemplate.Contextã®ã¤ã³ã¹ã¿ã³ã¹ãTemplateã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ï¼compileæ¸ã¿ï¼ã®æã¤ãprocessã¡ã½ããã«æ¸¡ããã¨ã§ããã³ãã¬ã¼ãã«ä»»æã®å¤æ°ã渡ããã¨ãã§ãã¾ãã
[#
ã¨#]
ã¨ãããã³ãã¬ã¼ãã®éå§ä½ç½®ãçµäºä½ç½®ãæå®ããããã®æååã¯ãTemplateã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã®beginToken
ããããã£ã¨endToken
ããããã£ãå¤æ´ãããã¨ã§å¤æ´å¯è½ã§ãã
é »ç¹ã«ä½¿ããã¨ã«ãªãã®ã¯ã[# ... #] 㨠[#= ... #] ã§ãããããã³ãã¬ã¼ãã¨ã³ã¸ã³ã§ããè¦ãããå½¢å¼ã ã¨æãã¾ãã
ãã³ãã¬ã¼ãã®ãµã³ãã«
[# ... #]
[# if (false) { #] ããã¯åºåãããªã [# } #]
[#
ãã#]
ã§å²ã¾ããç¯å²ã§ã¯ãä»»æã®JavaScriptã®å¼ãæãè©ä¾¡ãããã¨ãã§ãã¾ããè©ä¾¡å
容ã¯åºåããã¾ããã
[#-- ... --#]
[#-- ä¸è¨ã§ã´ãã§ã´ãã§ãã --#] ã´ãã§ã´ãã§
[#--
ãã--#]
ã§å²ã¾ããç¯å²ã¯ã³ã¡ã³ãã«ãªãã¾ããã³ã¡ã³ãä¸ã®å¼ãæã¯å®è¡ããããåºåãããã¾ããã
[#= ... #]
[#= true ? 1 : 0 #]
[#=
ãã#]
ã§å²ã¾ããç¯å²ã¯ãä»»æã®JavaScriptã®å¼ãè¨è¿°ã§ããè©ä¾¡ãããå¤ãåºåããã¾ãã
[#* ... #]
[#= foo #]
[#* return #]
[#= baz #]<!-- ãã®å¤æ°ã®å¤ã¯åºåãããªã -->
[#*
ãã#]
ã§å²ã¾ããç¯å²ã¯ãå®ç¾©ãããç¹å®ã®ã³ãã³ããè¨è¿°ã§ãã¾ãã
ã¨ã¯è¨ããå®éã«ã¯ãreturnããå®ç¾©ããã¦ããããreturnã³ãã³ããè¨è¿°ããã¨ããã®è¨è¿°ä»¥éã®ãã³ãã¬ã¼ãã¯è©ä¾¡ããã¾ããã
[#| ... #]
[#|i "foo ${bar} baz" #]<!-- ${foo} ã $foo ã¨ããè¨è¿°ãå¤æ°å±éããã --> [#|h "<script>alert(1);</script>" #]<!-- HTMLãã¨ã¹ã±ã¼ããã --> [#|H "<script>alert(1);</script>" #]<!-- HTMLãã¢ã³ã¨ã¹ã±ã¼ããã --> [#|u "ãã¹ã" #]<!-- URLã¨ã³ã³ã¼ããã --> [#|U "%E3%83%86%E3%82%B9%E3%83%88" #]<!-- URLãã³ã¼ããã --> [#|lc "ABC" #]<!-- å°æåã«ãã --> [#|uc "abc" #]<!-- 大æåã«ãã --> [#|substr(0, 3) "abcdefg" #]<!-- æå®ããä½ç½®ããæå®ããä½ç½®ã¾ã§æåãåãåºã --> [#|ws " foo " #]<!-- åå¾ã®ç©ºç½ãé¤å»ãã --> [#|trim(3) "abcdefg" #]<!-- å¼æ°ã§æå®ããæå以éãâ¦(U+2026)ã§çç¥ãã --> [#|date "2014-06-05T00:00:00+09:00" #]<!-- ISOå½¢å¼ã®æ¥ä»æååãISOå½¢å¼ã®å¹´ææ¥æååã«å¤æãã --> [#|localeDate "2014-06-05T00:00:00+09:00" #]<!-- ISOå½¢å¼ã®æ¥ä»æååããã±ã¼ã«ã«åããã表è¨ã«å¤æãã --> [#|rt "<script>alert(1);</script>" #]<!-- ã¿ã°ãåé¤ããï¼å 容ã¯æ®ãï¼ --> [#|rp(/foo/g,"bar") "foobarfoobar" #]<!-- å¼æ°ã«æå®ããæåãå¼æ°ã«æå®ããæåã«ç½®æãã -->
[#|
ãã#]
ã§å²ã¾ããç¯å²ã¯ãè©ä¾¡ãããå¼ã®å¤ã«ãå®ç¾©ããããã£ã«ã¿ãé©ç¨ããä¸ã§å¤ãåºåãã¾ãã
ä¸è¨ã®ä¸ã§ããiãã£ã«ã¿ããdateãã£ã«ã¿ã¯ãå é¨ã§ä½¿ã£ã¦ããé¢æ°ãCore.jsã«ä¾åãã¦ãã¾ãã
MTã®JSã©ã¤ãã©ãªãã¾ã調ã¹ããã¨ãªãã£ãã®ã§æèãã¦ããªãã£ããã§ãããçµæ§ã°ãã¼ãã«ãªãã¸ã§ã¯ããæ¡å¼µãã¦ãããã§ãã
rpãã£ã«ã¿ã¯ãã¾ãåãã¦ããªãã£ãã®ã§ãPRãã¾ããã
ã¾ãããã£ã«ã¿ã¯Unixã®ãã¤ãã®ãããªæãã§ãè¤æ°çµã¿åããããã¨ãã§ãã¾ãã
[#|ws|rt|uc " <span>abcd</span> " #]<!-- åå¾ã®ç©ºç½ãé¤å»ãã¦ãããã¿ã°ãé¤å»ãã¦ãå 容ã大æåã«ãã -->
Template.Context, Template.Filter ã«ã¤ãã¦
Template.Contextã«ã¯ãããããã¡ã½ãããç¨æããã¦ãã¾ãããTemplateã¯ã©ã¹ãéãã¦ééçã«å©ç¨ãããã¨ãã»ã¨ãã©ãªæ°ãããã®ã§ãç´¹ä»ã¯å²æãã¾ãããã£ã«ã¿ã«é¢ãã¦ããåºæ¬ã¯ãã³ãã¬ã¼ãå ã«è¨è¿°ããã±ã¼ã¹ãã»ã¨ãã©ã§ãããã
Template.Contextã®includeã¡ã½ããã¯åä½ã§ã使ãããããã¾ããã
var templates = { header: 'Header!!', body: '[#= context.include("header") #] Body!! [#= context.include("footer") #]', footer: 'Footer!!' }; Template.process('body', {}, templates); // => "Header!! Body!! Footer!!"
ã¾ã¨ã
ãã使ãæ©è½ã«é¢ãã¦ã¯ãä¸è¨ã§ç´¹ä»ããéãã§ãããã¨ã¯ã½ã¼ã¹èªãã§ç¢ºèªãã¦ãã ããã
æã¯ããããã¨MTæ¨æºã®JSã©ã¤ãã©ãªã«ã¤ãã¦ããã¬ãã¸ãå ±æããã¦ããã®ããããã¾ããããä»ã¯ãã°ã°ã£ã¦ããã¾ãæ å ±ãè¦ã¤ãããªãæãããã¾ãã
Template.jsã«éããæ±ç¨çã«ä½¿ããããªã©ã¤ãã©ãªãããããã§ãããããã¥ã¡ã³ããªãããèªåã®ãããªæ°åè ã¯ã½ã¼ã¹èªããããªãæãã§ããå ¬å¼ããã¥ã¡ã³ããå¾ ãããã¨ããã
Template.jsã¯ãæ±ç¨çã«ä½¿ãããã³ãã¬ã¼ãã¨ã³ã¸ã³ã ã¨æãã®ã§ãMTã®ç®¡çç»é¢ã®ã«ã¹ã¿ãã¤ãºç¨éçã§ãç¹ã«ä»ã®ã©ã¤ãã©ãªãèªã¿è¾¼ãäºå®ãä»ã«ãªãæã¯ããã®JSãã³ãã¬ã¼ãã¨ã³ã¸ã³ã使ããããªã¨æã£ã¦ã¾ãã
ã»ãªã®ã