ãã³ãã¬ãŒãåç©åã®æ代ã¯éããä»ã§ã¯MVCæç«ãèµ°ãåã£ãŠãããçµã¿èŸŒã¿ã®ãã³ãã¬ãŒããšã³ãžã³ãšã³ã³ããŒãã³ããã«ããŒã䜿çšããŠããŸãã ããããKnockoutãšBackboneã®å€ãããŸã䟿å©ã§ã¯ãªããã³ãã¬ãŒããšã³ãžã³ã眮ãæããã«ã¯ãæã
å¿
èŠã«ãªããŸããåºæ¬çã«ã2014幎é ã®ã¬ãã«ã§éçºãåæ¢ããŸããã
ããã¯
DoT.jsã§çºçã
ãŸããã 2013幎ã«çŽ1幎éå·çè
ã«æåã«èŠæšãŠããã圌ã¯ãããŒãžã§ã³1.0.1ãã1.1.1ã«äžæããäžæçã«æ³šç®ãéããåã³èŠæšãŠãããŸããïŒãŸãã¯ãçç±ã«å¿ããŠå®å®ããŸããïŒã ãã®ç¹ã§ã2013幎ã«ã¯ïŒ
DoT.jsã®ã¯ããŒã³ãäœæããããã« ïŒå¿
èŠã§ããããä»ã§ã¯ãã
ãã¢ããã°ã¬ãŒãããå¿
èŠ
ããããŸããã
Underscore / Lodashã®
_.template()
ãšåããããé«éã§ããããã³ãã¬ãŒãã«JSãèšè¿°ããå¿
èŠæ§ãäžè¬çã§ã¯ãªãæ§æãæ¹åãããŠããŸãããUnderscorã§ã¯åžžã«å¿
èŠã§ãã 圌ãã¯ãã¹ã¯ãªããä»ãã®ãããã®æ¬åŒ§ã®ç¹å¥ãªçšèªã§ããjavascript encapulated sectionïŒJESïŒãæãã€ããåºæ¬çã«ããããåãé€ããŸããã
ããã«äœãåŸãããŸããïŒ
1.ãã³ãã¬ãŒããšã³ãžã³ã®æ§é
ãåèšèšããïŒ2013幎ãããããã®ãªã³ã¯ïŒ ãèªã¿ããããªãããã³ãŒãæ©èœã®æ°ãæžããŸããã
2.
ãã¹ãã§ã¯ ãå¹³åé床ãå€åããŠããªãããš
ã瀺ãããŠããŸãïŒãã©ã¡ãŒã¿ãŒã«ãã£ãŠå€å-3ïŒ
-+ 10ïŒ
ïŒã
3.ã¢ã¬ã€ã®äœæ¥ãšåæ§ã«ãæ§é ã®äœæ¥ããŒã ãè¿œå ãããŸããã
4. 4çªç®ã®ãã©ã¡ãŒã¿ãŒ-æ§é äœãŸãã¯é
åã®èŠçŽ ã®ãã£ã«ã¿ãŒã
5.äžéšã®å Žæã§ã¯ããã€ãã¹ãã°ã«ããã¹ããŒããŠã³ã¯ãã³ãŒããšæ£èŠè¡šçŸã®æé©åã«ãã£ãŠè£åãããŸãã
6.ã°ããŒãã«åãdoTãã¯ãèšå®ã§å¥ã®ååã«å€æŽã§ããŸãïŒå
ã®-ãããïŒã
7.ã°ããŒãã«é¢æ°ã®ããŒãžã§ã³çªå·ä»ããšããŒãžã§ã³ä»ãã®é åºencodeHTMLïŒïŒ-æé©åã®ããã«ããã§æ¡çšãããã€ã³ã¹ã¿ã³ã¹ãé
眮ãããŸããã
ããŒãžã§ã³çªå·ã®åç
§çš
npmã§ã¯ãpackage.jsonã®1察1ã®ããŒãžã§ã³1.1.1ã®ã³ããŒã®ååã¯1.1.2ã§ããããã¡ã€ã«å
ã§ã¯-çªå·1.1.1ãæ®ããŸãã ãªããžããª2.0ã®ãã©ã³ã-éæŽæ°çªå·1.1.1ã®åããã®ã§ã var rw = unescape(...)
éãã¯1ã€ã ãvar rw = unescape(...)
ã äžè¬ã«ãæ··ä¹±ã®ããã«ãã¹ãŠãè¡ãããŸãã ãããã£ãŠãææ°ããŒãžã§ã³ã¯1.1.1ã§ããã2.0ãã©ã³ããšã®éããèæ
®ã«å
¥ããŠããŸãã Branch 2.0ã¯ãã®ã¿ã€ãã«ã«å€ããŸããããšãã©ããããããã§ãã¯ããããã®ããŒã«ã䜿çšããŠãããã¥ã¡ã³ããäœæããã®ã䟿å©ã§ãïŒ
èè
ããã®ãã®ããããŸãïŒã ïŒWebã§èªããã®ã¯ä»¥äžã®ãªã³ã¯ã§ããïŒèŠããã«ïŒ
â¢åœŒã¯ãã¢ã³ããŒã¹ã³ã¢
_.template()
ã®ãå
ã®ãæ§æãä¿æããŸããããã®æ§æã§ã¯ããã£ãã
{{ ... }}
ãå
ã®ä»»æã®JSã³ãŒããèšè¿°ã§ããŸãã
â¢ãã©ã±ããã¯ãèšå®ã§ãã¹ãŠã®æ£èŠè¡šçŸãåå®çŸ©ããããšã§åå®çŸ©ã§ããŸãïŒéåžžã¯å¿
èŠãããŸããïŒã
â¢
'it'
æ§é ã®äžçŽèŠçŽ ã®ååãããã³4ã€ã®è«çåäœèšå®ãåå®çŸ©ã§ããŸãã
â¢AMDãcommonJSãããã³ãã®ã°ããŒãã«åïŒ
'doT'
ïŒã§ãµããŒããããŸãã
â¢åºæ¬çãªãŠãããŒãµã«æ§æã«å ããŠãMustache / Handelbarsã¹ã¿ã€ã«ã«äŒŒãã³ãã³ããå€æ°ãããŸãã
â¢ããããšåæ§ã«ã
_.template()
ã¯2段éã®ãã³ãã¬ãŒãïŒãã©ã¡ãŒã¿ãŒã®ã«ãªãŒåïŒããããŸã-é¢æ°ãžããããŠHTMLïŒãŸãã¯ä»ã®ïŒã³ãŒããžã
â¢HTMLã®äžã§å³å¯ã«ã·ã£ãŒãåãããã®ã§ã¯ãªããJSã«çµã³ä»ããããããããã®ç¯å²ã¯ãã©ãŠã¶ãŒãšNodeJSã§ãã
â¢ãœãŒã¹ã³ãŒã
_.template()
ãããããªã¥ãŒã ã倧ãããªã-å§çž®ãããŠããªãå§çž®åœ¢åŒã§
_.template()
ã
ïŒæ°ãããŸãã¯å€ããã³ãã¬ãŒããšã³ãžã³ã®ã³ãŒãã䜿çšããå®éšã«ã€ããŠã¯ã httpïŒ//jsfiddle.net/6KU9Y/2/ã®å€ãäŸã䜿çšã§ããŸãïŒãã ããjsãã£ãŒãã®æ¹ãåªããŠããŸããïŒå
±éã®ãã³ãã¬ãŒãããã³ããŒã¿ã§2ã€ã®ãšã³ãžã³ããã¹ãããããã®äŸ¿å©ãªããŒãžããªããžããªã«æ§ç¯ãããŸãtest/index.html
ãããã©ã«ãã§ã¯ããããã€ããã³çž®å°ãããããŒãžã§ã³ã®ãã¡ã€ã«ãšåãçµæãæ¯èŒãã2çªç®ã®ãšã³ãžã³ã¯'doT'
以å€'doT'
ã°ããŒãã«åãæã£ãŠããå¯èœæ§ããããããã¯ããŒã³ã§ã®ã¿æ©èœããŸããã¯ããŒã³ãæåã®å Žæãããšãã°ãå
ã®DoT.js 1.1.1ããã³è§£ââæã®éããåç
§ããŠãã ãããïŒ2çªç®ã®ãšã³ãžã³ã®ãã¡ã€ã«ã§ãããŒãžãéãåã«ãåå
globalName:'doTmin'
ãŸãã
ãã ããåãããšããªã³ã©ã€ã³ã§è¡ãã®ã¯é£ãããããŸããïŒ
https :
//jsfiddle.net/spmbt/v3yvpbsu/26/embedded/#ResultãŸãã¯ãã¬ãŒã ãšã³ãŒãç·šéã䜿çšãã倧ããªç»é¢ãæã£ãŠããå ŽåïŒ
https :
//jsfiddle.net/spmbt/ v3yvpbsu / 26 / ã ãã¡ã€ã«ãæ¥ç¶ããå¿
èŠã¯ãªããDoT.jsã®2ã€ã®ããŒãžã§ã³ã®å
容ãã³ããŒã¢ã³ãããŒã¹ãããã ãã§ãã2çªç®ã®æ¹æ³ã§ã¯ãã¯ããŒã³ã§ãããè¡ãæ¹ã䟿å©ã§ã
doT
åã
doTmin
ä¿®æ£ã
doTmin
ïŒæå°åãããŠããªããŠãïŒã ããã©ã«ãã§ã¯ãDoT.js 1.1.1ã¯ãªãªãžãã«ã«èšå®ãããDoT12.js 1.2.1ã¯ã¯ããŒã³ã§ãã
ãã³ãããŒã¯ïŒ3çªç®ã®ãã¿ã³ïŒã¯çæéã®ããã«èšèšãããŠããããã䞊åããã»ã¹ã«äŸåããåæã¢ã«ãŽãªãºã ã®é床ã¯ãå€æ°ã®æž¬å®å€ãå¹³åããããšã§å€æã§ããŸãïŒå¹³åã环ç©ããé床ããããã«å€æã§ããŸãïŒã ãããžã§ã¯ãã«ã¯ãã³ãããŒã¯ãçµã¿èŸŒãŸããŠããŸãããNodeãä»ããŠè€éã§ãããåãããšãè¡ããŸãïŒãã¹ããã¡ã€ã«ã®ã°ã«ãŒãã«å¯ŸããŠïŒã
èªåæŽæ°ãã§ãã¯ããã¯ã¹ãšãšã©ãŒãã©ããã¯ãã¹ãããŒãžã«çµ±åãããŠãããããæ£åžžãªããŒãžã§ã³ã§ãã¹ããå®è¡ã§ããŸãã ãããã£ãŠãé
åã§ã®
'~'
'@'
ã³ãã³ããš
'@'
ã³ãã³ãã®é床ãæ¯èŒããã®ã¯ç°¡åã§ãã 2çªç®ãæ©èœããŸãããã¯ããã«é
ã-10ã15ïŒ
ïŒ[ãã³ã]ãã¿ã³ã§ãã§ãã¯ïŒã ããã¯ã2çªç®ã®ã±ãŒã¹ã§ããé
ãfor-inã«ãŒãã䜿çšããå¿
èŠãããããã§ãã ããã§ãã
'@'
ã³ãã³ããæããªãå
ã®ããŒãžã§ã³ã®æ§é äœããé
åãæºåããå¿
èŠããªãããã«ãæ§é äœã®for-inãªãã§ã¯ã§ããŸããã
ããã«äŒçµ±çã«ã2åç®ã®èšç®ïŒ
ã³ã³ãã€ã«ããããã³ãã¬ãŒãã«ããèšç®ïŒã¯ãæ¯åã®
å®å
šãªã³ã³ãã€ã« ïŒæåã®æ°ã100åå°ãªã枬å®ïŒ
ããã 100å
é«éã§ãïŒçãåŒã®å Žå ïŒã ã¹ã¯ãªãŒã³ã·ã§ããã®ã
Comp1e3: 99.22ms
ãã¯ãã
Comp1e3: 99.22ms
éã«1000åã®å®å
šãªã³ã³ãã€ã«ãå®è¡ãããããšããæå³ããŸãã 5è¡ç®ã®ã
Run5e5: 69.08 ms
ãã¯ãããã³ãã¬ãŒãã«ãã5äžã®é«éHTMLçæãã1äžäžä»£ããšã«å¹³å
Run5e5: 69.08 ms
ããæå³ããŸãã
äŸã®ããããããŠã³ãªã¹ããè¿œå
ãããŸãããDoT.jsã³ãã³ãã䜿çšããå
žåçãªäŸãæœåºãã2ã€ã®ããŒãžã§ã³ã®ã¹ã¯ãªããã§ãã¹ãã§ããŸãã ãµã³ãã«ãžã®å€æŽã¯äžæçã«ä¿åããããã©ãŠã¶ããŒãžããªããŒããããŠããªãå Žåããµã³ãã«ã®åã®æ®µèœãåéžæããããšã§å
ã«æ»ãããšãã§ããŸãã
ããŒã ã«ã€ããŠ-詳现
段èœã®å
é ã®åŒçšç¬Šã§
DoT12.js
ããååã¯ããã®ãã³ãã¬ãŒãé¢æ°ïŒã³ãã³ãïŒãæ
åœãã
DoT12.js
ã³ãŒãå
ã®æ£èŠè¡šçŸã®ååã«ãªããŸãã
ã³ãã³ãã valEncEval ã
ãã®ååã®æ£èŠè¡šçŸã¯ãåæ§ã®æ§æãæã€3ã€ã®ä»¥åã®ããŒã ãçµã¿åããããã®ã§ãã
{{ JS }}
ã¢ã³ããŒã¹ã³ã¢ã®äžçéºç£ã ãã以äžã¯å¿
èŠãããŸããããã¹ãŠãèšè¿°ããããšã¯èªçµŠèªè¶³ã§ãïŒæ¬åŒ§éã®ã¹ããŒã¹ã¯ãªãã·ã§ã³ã§ãïŒã ããããèªãã«ã¯... 40è¡ä»¥äžãèªãããšã¯å³å¯ã«ã¯æšå¥šãããŸããã JSãªãŒãã³ãã©ã±ããã«ã¯ãåããã¿ãŒã³ã§éããããåããã³ãã¬ãŒããã¬ãŒã¹ãæ£åšããŠããŸãã ãããã®èåŸã«ã¯ãéããããŠããªãHTMLã¿ã°ããããŸãã ããã¯éåžžã®æ¹æ³ã§ãããæ£åžžã«åäœãããã·ã³ã¯ç解ããŠããŸãã åæã«ãMustache / Handlebarsã¯ãã§ã«èªã¿åãå¯èœã§ãã
{{= JS }}
åŒã®å€ã¯åšå²ã®HTMLã¹ããªãŒã ã«é
眮ãããHTMLã¿ã°ã¯ä¿æãããŸãããéå§ã¿ã°ã¯æ¬åŒ§ã«ãªããŸãã ãããã£ãŠãåŒã®å€ã«äžèŽãã
"<br>"
ã¯ããã©ãŠã¶ããŒãžã«ã¬ã€ã¢ãŠããããŠããå Žåãããã¹ããšããŠã§ã¯ãªãã¿ã°ãšããŠåäœããHTMLã«ãŒã«ã«åŸã£ãŠæ¹è¡ãçºçããŸãã
{{! JS }}
åãã§ãããHTMLã³ãŒãã®ãã»ãã¥ãªãã£ããšãšãã«åºåã«ããã¹ããè¿ããŸããhtmlã¿ã°ãšhtmlãšã³ã³ãŒãïŒ
&...;
ïŒæåã¯ããã¹ãã«å€ãããŸãã
ã³ãã³ãã æ¡ä»¶ä»ã ã
{{? if- }} then- {{?}}
ãã³ãã¬ãŒãã®æ¡ä»¶ä»ãã€ã³ã¯ã«ãŒãïŒæ¬åŒ§éã®ã¹ããŒã¹ã¯ãªãã·ã§ã³ã§ãïŒ;
{{? if- }} then- {{?? [if-else-]}} [if-]else- {{?}}
ããã¹ããã³ãã¬ãŒãã®åå²ããã³æ¡ä»¶ä»ããã§ãŒã³ã
åŒã¯ããã³ãã¬ãŒãã¢ãã¬ãŒã·ã§ã³äžã«ãã©ã¡ãŒã¿ãŒãæž¡ãããã°ããŒãã«å€æ°ãŸãã¯
'it'
ãšããåå
'it'
æ§é ãå«ãä»»æã®åŒã§ãã
if-elseã¯ãif-expressionãéã«ããããšã§ç°¡åã«ååŸã§ããŸãã æ§æã¯ãæããã«é«éåã®ããã«è€éã«ãªããŸããã§ããã
ã³ãã³ãã䜿çš
{{# , }}
ããªããã»ããµã®é¡äŒŒç©ïŒãã¯ãïŒ-æååã«ã¯ãæåã«ããã¢ã«ãªã£ãŠããªããã³ãã¬ãŒããã©ã±ãããå«ãããã¹ãã®ãã©ã°ã¡ã³ããæ¿å
¥ã§ããŸãã çµæãã³ã³ãã€ã«ãããŸãã ããšãã°ã
{{# ...}}
ã䜿çšããŠãå€æ°ãä»ããŠå¥ã®ãã³ãã¬ãŒãã®ããã¹ãããã³ãã¬ãŒãã«å
¥åã§ããŸãã ããããããŒã ã¯å®çŸ©ããŒã ãšå¯Ÿã«ãªã£ãŠãããåçŽã§å
·äœçãªã±ãŒã¹ãæ³å®ããŠèæ¡ãããŸããã
ã³ãã³ããå®çŸ©ãã
ããŒãžã§ã³1.0ããç»å Žã æåã¯ããã³ãã¬ãŒãã®ããã¹ãã§ã¯ãªããèšå®åŸã®ãã©ã¡ãŒã¿ãŒã®ãªã¹ãïŒdoT.templateã®3çªç®ã®ãã©ã¡ãŒã¿ãŒïŒãã³ãã¬ãŒããèšå®ããã©ã¡ãŒã¿ãŒïŒïŒã§æ±ºå®ãããŸããã
ãdefineãã³ãã³ãã§ãuseãã³ãã³ãã®å€æ°ãå®çŸ©ãã圢åŒã¯æ¬¡ã®ãšããã§ãã
{{## def.defin1 :___#}} {{## def.defin1 =___#}} - ()
å€æ°ã«ã¯ããããš$ã䜿çšã§ããŸããå€æ°ã«ã¯ä»»æã®è¡ãå®çŸ©ãããŸãã ããã€ãã®ããªãã¯ããããŸãã
ååã®ãããïŒèª°ãã®ã¹ã¿ã€ã«ãã³ããŒãããïŒã
æåã®4æåããdefããã®å Žåããããã¯åé€ãããŸãã
ã³ãã³ãä»ããå Žåãdef [code] = {argïŒparamãtextïŒv}ã®ãã¢ãæžã蟌ãŸããŸãã
å¹³çã«ãããé¢æ° 'def'ãå®çŸ©ãããŸãïŒäŸã®ãªã¹ãã«ãããã¹ãããŒãžãèŠããšäŸ¿å©ã§ãïŒã
1ã€ã®å Žæã§ãã¯ããå®çŸ©ããŠã2å以äžäœ¿çšã§ããŸãã ãã¹ãŠã®ãã¯ããšåæ§ã«ãã³ãŒãå質ã®ç¹ã§çãããã§ãã ãã¯ããå¿
èŠãªå Žåãã€ãŸãããããžã§ã¯ãã®èŠ³ç¹ãããã¯ããçž®å°ããæ¹æ³ãéã«åããªãã£ãããã誰ããèªåèªèº«ãã·ã£ããããŠã³ããŠãæ±ããæ¹æ³ã§ããã¹ãã®ãµã€ãºãçž®å°ããå¿
èŠããããŸããã ãŸããã¹ã¯ãªããã®ãªãœãŒã¹ã¯å€æ°ã®ä¿åã«è²»ããããŸãã
ããã€ãã®ãã©ã¹ã¯ãããã€ãã®use-useParamsã³ãã³ãã®ããã«ãç°å¢ãšã¹ã¯ãªããã®äœæ¥å Žæãåé¢ãããŠããããšã§ãã
ãŸãããã³ãã¬ãŒããããŒã¿ãšãªã³ã¯ããå®çŸ©ããŠäœ¿çšããç¬éã«ã¹ã¯ãªãããå®è¡ããããšããäºå®ã«ã¯æå³çãªéãããããŸãã ã€ãŸãããããã¯ãã¯ãã§ãã
ã³ãã³ãã defineParams ã
{{##foo="bar"#}}
ãã®ããããã©ã¡ãŒã¿ãŒã¯æ±ºå®ãããå
éšæ§é ã«èç©ããããã®åŸäœåºŠã䜿çšãããŸãã
ã³ãã³ãã useParams ã
以åã«å®çŸ©ããããã©ã¡ãŒã¿ãŒã䜿çšããŸãã
{{#def.foo}}
JSã§ãã©ã¡ãŒã¿ãŒãååŸããã³å®çŸ©ããããšã劚ãããã®ã¯ãããŸããã
{{ JS }}
ãã³ãã¬ãŒãç°å¢ãšæ®ãã®JSãæ··åšãããããªãããšãé€ããŸãã ãã®å ŽåãIDEããã³ãšãã£ã¿ãŒã§ã®æ§æã®åŒ·èª¿è¡šç€ºãšã³ãŒãåæã¯å€±ãããŸãã ãããã£ãŠããã³ãã¬ãŒãããã¹ãã«JSãèšè¿°ããªãçç±ã¯ä»ã«ããããŸãã
ããŒã ã å埩 ã
ç°ãªãé床ãšæ©èœãæã€2ã€ã®ããŒã ãçµã¿åãããŸãã while-pattern
{{~...}}
ã¯ãwhile whileã«ãŒãã®ã«ãŒãã§ãã ïŒæããã«ãç§ãã¡ã¯ãã¹ãŠããéžæãããã®æç¹ã§æéã®ãã©ãŠã¶ãŒãéžæããŸãããïŒããã¯ãã¢ã¬ã€ãä»ããŠå®è¡ã§ããfor-in-templateã§ã®ä»£æ¿
{{@...}}
ããã10-15ïŒ
é«éã«åäœããŸããæ§é ã«ãã£ãŠã 4çªç®ã®ãã©ã¡ãŒã¿ãŒ-åŒã«ããèŠçŽ ã®ãã£ã«ã¿ãªã³ã°ã å
ã®ããŒãžã§ã³ã§ã¯ãé
åã®ã¿ããµããŒãããããã£ã«ã¿ãªã³ã°ã¯è¡ãããŸããã ããªãã«ã¯åããŠããŸãã-åžžã«ã
{{ ... }}
ãããããŸãïŒPerlããã·ã³ã³ãŒãã®ããã«ãæžãã®ãèªãã®ã䟿å©ã§ãïŒã
{{~ it : value : index : filter-expression }} while- {{~}}
ãã
it
ãåèª
'it'
ïŒãŸãã¯å¥ã®ïŒãã€ãŸãæåã®åŒæ°ãé
åã®ã°ããŒãã«åããŸãã¯é
åãè¿ãåŒã§ãã
value
-åŒçšç¬Šãªãã®
'v'
ãŸãã¯
'value'
'v'
ãªã©
'value'
ä»»æã®ååãããã¯ãåŒ
{{= value+1}}
ãªã©ãé
åèŠçŽ ã®çœ®æå€ã®ä»£ããã«forãã³ãã¬ãŒãã§äœ¿çšãããŸãã
index
åæ§ã«ãé
åèŠçŽ ã®ã€ã³ããã¯ã¹ãå®çŸ©ããååã
ã¯ãããã©ã¡ãŒã¿ã¯ãover the topãïŒæåã®å€ã次ã«ã€ã³ããã¯ã¹ïŒã§ç€ºãããŸããããããªã£ãã®ã§ãããã§ã次ã®åæ§ã®ã³ãã³ãã§ãå€æŽããŸããã ããžãã¯ã¯ããã³ãã¬ãŒãã§äžèŠãªå Žåã¯ã³ãã³ãšãšãã«æåŸã®ïŒãããŠäžè¬çã«æåŸã®ïŒ
index
ãçç¥ã§ãããšããããšã§ãã
ã¯ããŒã³ã§ã¯ä»ã®ãã©ã¡ãŒã¿ãŒãçç¥ããŠãã³ãã³ãæ®ãããšãã§ããŸãã ãªãªãžãã«ã§ã¯-ããã¯äžå¯èœã§ããå°ãªããšãæçŽã§ãããããªãã¯æžãå¿
èŠããããŸãã æåã®ããã©ã«ãã®ãã©ã¡ãŒã¿ãŒã¯ 'it'ïŒãŸãã¯ããã
templateSettings.varname
ïŒã§ãæ®ãã«ãããã©ã«ãããããŸãããéåžžã«æè¡çã§ãèšé²ãããŠããŸããã
èŠããã«ããã³ãã¬ãŒãã®ã³ã³ãã€ã«ãšå®è¡ãå¯èœãªéãé«éã«ãªãããã«ããã¹ãŠãè¡ãããäžèŠãªã¢ã¯ã·ã§ã³ãçŸããããšã¯ãããŸããã ãããããããªãã¯ãŸã äœããæ¹åããããšãã§ããããã€ãã®ããã©ãŒãã³ã¹ã®æ¹åã¯ã³ã³ãã€ã«æéãæªåãããã®ã§ããã¹ãŠã«ãããŠåççãªãã©ã³ã¹ããããŸãã
{{@ it : value : index : expression }} for-in- {{@}}
æ§é ãæåã®ã¬ãã«ã§å®è¡ããŸãã ãã©ãŠã¶ã§ã¯ãé
åãããå®è¡é床ãé
ããªããŸãïŒ10ã15ïŒ
ïŒãããŒçªå·ããã®ä»ã®ããŒãçºè¡ãããé åºã®ç¹æ®æ§ãèæ
®ããå¿
èŠããããŸãïŒçªå·ãæåã«ãªãã次ã«ä»ã®ãã¹ãŠã®ããŒïŒ8çªç®ä»¥äžãIEãªã©ã®å€ãããŒãžã§ã³ãé€ãïŒãåãéå»åœ¢ã®å€ãFxãæ°åãä»ã®è¡ãšé£ç¶ããå ŽæãïŒ ãŸããé åºãä¿èšŒããæšæºã¯ãããŸããããä¿èšŒãããŠããŸãã 䜿çšãããããã§ãã¯ããããé
åã«äŸåãããã¯éçºè
次第ã§ãã æãè©å€ã®è¯ã人ã¯ã100ïŒ
ãä¿¡é Œããããšã¯ã§ãããæ£ãããšèšãã§ãããã PythonãšJSONã®äž¡æ¹ã§åã話ã
åæã«ãJSONãæ¥ãé åºãŸãã¯æ§é ãå®çŸ©ãããé åºã䜿çšãããšãããã·ãŒãžã£ã³ãŒãã®éãæžããŸãã 4çªç®ã®ãã©ã¡ãŒã¿ãŒã¯ãåŒïŒãã£ã«ã¿ãŒæ¡ä»¶ïŒãè¿œå ããŸãã falseã®å ŽåãèŠçŽ ã¯è¡šç€ºãããŸããã ïŒæ¡ä»¶ããªãå Žåãå¯çæ¡ä»¶
"if(1)"
ç¶æãããŸããïŒããã¯ãã©ã³ã¹æéã§ããïŒ
ãã©ã¡ãŒã¿ã®ç¥èªã®äŸ
{{@::i}} for-in- {{@}}
ããã¯ãæ§é ã®ãã¹ãŠã®èŠçŽ ãééããã ãã§ãã
äŸïŒ
{{@::i}} <i>{{=it[i]}}</i> {{@}}
ãšã«ãã
{{@:v}} <i>{{=v}}</i> {{@}}
ãã¿ãŒã³ã®ä»£ããã«ã
"@"
ã³ãã³ãã§é
åã䜿çšã§ããŸãã ããããå察ã«ã
"~"
-ã³ãã³ãïŒé
åã§ïŒ-æ§é -ãªãªãžãã«ãšã®äºææ§ã®ããã«äžå¯èœã§ãã
... 7幎åŸããããã¯ãã¹ãŠæ¶æ»
ããæ°ãã壮倧ãªãã¬ãŒã ã¯ãŒã¯ã®éã¿ã§æ¶æ»
ããŸããããã§ã¯ãåé¡ãç¬èªã®æ¹æ³ã§å¥ã®æ¹æ³ã§è§£æ±ºãããŸãã forEachãfilterãreduceãJSX-ãã³ãã¬ãŒããã©ãŒã¯ãã§ã«ãããå€ãè¶
ããŠããããã¢ã®ãã¡ãåŽã«ãããã1ãã£ãŒããããŸãã ãŸãããã³ãã³ãžãŒã§ãç»é¢ã¬ã€ã¢ãŠãã管çã§ããŸãã ãããŸã§ã®éãæŸèæããããããŸãããŠãã€ãã«K155LA3ã®ãããªç©èªã«ãªããŸãã
ã©ããªèšå®ãããããªãå¿
èŠãªã®ã
varname: 'it',
ãã®èšå®ã¯éåžžã«ç°¡åã§ãã ãã³ãã¬ãŒãåŒã§ã¯ãåŒæ°[0]ïŒäžéã³ã³ãã€ã«æžã¿é¢æ°ã®æåã®ãã©ã¡ãŒã¿ãŒïŒã®ä»£ããã«ååã䜿çšãããŸãã åŒæ°[0]ã¯ããã¹ããããé¢æ°ã®ããã«åžžã«é©çšå¯èœãšã¯ã»ã©é ãã競åããªãå Žåãååã¯ã»ãšãã©åžžã«ã§ãã çŸåšã競åãããå Žåãååã¯ã©ã€ãã©ãªãã³ãã¬ãŒããšã³ãžã³ã®ã³ãŒãã§çŽæ¥å€æŽã§ããã ãã§ãªããæªãããšã«å ããŠãçŸåšã®ã³ãã³ãã®ããŒã«ã«èšå®ã®2çªç®ã®ãã©ã¡ãŒã¿ãŒ
doT.template()
ã§ãå€æŽã§ããŸãã ïŒ
window.doT.templateSettings
ãä»ããŠ
window.doT.templateSettings
ããšã«ãããèšå®ããéçã«ãå€æŽããæ¹æ³ããããŸããïŒ
ãã®ãã³ãã¬ãŒããšã³ãžã³ã«ã¯ã競åãåŒãèµ·ããå¯èœæ§ã®ããä»ã®ç¹å®ã®ååããããŸãã ãã¹ãããŒãž
test/index.html
[é¢æ°ã衚瀺]ãã¿ã³ãã¯ãªãã¯ãããšããã¹ãããŒãžã§ç°¡åã«ç¢ºèªã§ããŸãã ãããã¯ååã§ãïŒ
out, arr1, arr2
ã...ãllïŒ2ã€ã®å°ããªLïŒãvãiïŒé
åãééãããšããåãå€éšåãã¹ã¯ãªãŒãã³ã°ããŸãããããã
it
ã¯ç¹å¥ãªã¢ã«ãŠã³ãã«ããããããªãã§ã¯ã©ãã«ããããŸãããèšå®ã§ã
strip: true,
äœåãªã¹ããŒã¹ãã¿ããæ¹è¡ãæšãŠãŸãã åºåããã¹ãã®ãã©ãŒããããéèŠã§ãªãå Žåã¯ãtrueã䜿çšããŸãã é¢æ°ãšãã³ãã¬ãŒãã¯çããªããã³ã³ãã€ã«é床ã¯å¥åŠã«æãããããããŸãããããããã«é
ããªããŸãïŒ1ã2ïŒ
ïŒã ããã©ãŒãã³ã¹-é床ã§åºå¥ã§ããŸããã ããªãã¡ ã³ã³ãã€ã«ãé«éåããã«ã¯ã
strip: false
èšå®ã
strip: false
ã
append: true,
HTMLã³ãŒããšããŒã¿ã®æçãoutå€æ°ã«è¿œå ããã¹ã¿ã€ã«ã¯ããã§ãŒã³ã§åèšããããæŒç®åãå²ãåœãŠãŸãã åŸè
ã¯é¢æ°ã®ããã¹ããé·ããããããç¹ã«å¿
èŠãªãå Žåã¯trueãéžæããŸãã ïŒã©ããããããã¯ãã€ãŠçåã§ãã-äœãéžæããäœãããéãåäœããããïŒ
log: true,
䜿çšãããŸããã åé€ããã®ãå¿ããããNodeJS-expressãªã©ã®è¿é£ã®ã¹ã¯ãªããã®ã©ãããå¿
èŠã§ãã
selfcontained: false,
æé©åã«ã€ããŠå°ã説æããŸãã
doT.template(...)
é¢æ°ã¯ã1ã€ã®å
±éç°å¢ïŒ
_globals
ïŒã§æºåããããã«
doT.template(...)(...)
ãšããŠå®è¡ããããšãã
doT.template(...)(...)
ã«å®è¡ããããšãã§ããŸãïŒajaxãŸãã¯ãã¡ã€ã«ããå®è¡ïŒ ã åŸè
ã®å ŽåãtrueïŒé¢æ°doT.templateïŒ...ïŒãæ¡åŒµããïŒãå¿
èŠã§ããéåžžãæåã®å Žåã¯falseã§ãã ãã®åŸãéå°ã«çæããå¿
èŠã¯ãªããã«ãŠã³ãã¯
_globals.doT.encodeHTMLSource()
ããçæããã
_globals._encodeHTML
ã«æ ŒçŽãã
_globals._encodeHTML
ãåžžã«ã§ã¯ãããŸãããã
{{! }}
{{! }}
ã
èšãæãããšã
selfcontained = true
ã¯ããã³ãã¬ãŒãé¢æ°
doT.template()
ã
doT.template()
ãšã¯å¥ã«äœ¿çšãããããšãæå³ããããããã³ãã¬ãŒãå®è¡ã®ããã®ãã¹ãŠãå«ããå¿
èŠããããŸãã ããã ãã§ã-
{{!}}
ã³ãã³ãã§HTMLæåããšã³ã³ãŒãããç¹å¥ãªå Žåã®ã¿ãæå³ããŸãã ååšããå Žåã¯ããšã³ã³ãŒãé¢æ°ã®å®çŸ©ãé¢æ°ã«å«ããå¿
èŠããããŸãâäœæ
doT.encHtmlStr
è¡ïŒããã¯ã¯ããŒã³1.2.1ã§è¡ããããªãªãžãã«ã§ã¯ã
encodeHTMLSource
é¢æ°
encodeHTMLSource
æååã«å€æãããŸãïŒã
ãªãªãžãã«ã®ããŒãžã§ã³1.1.1ã«ã¯æ¬ é¥ããããŸã
selfcontained = false
å Žåã§ããã¢ã«ãŽãªãºã ã¯åžžã«å§çž®ããã«é¢æ°ã³ãŒãããã³ãã¬ãŒãã«ãåºå®ãããŸããããã¯ä¿®æ£ããå¿
èŠããããŸããã ãã®é¢æ°ã¯ã
doNotSkipEncoded
ãã©ã¡ãŒã¿ãŒãåžžã«ãã€ã³ãããããšãåŠçããŸãããããã¯ãã³ãã¬ãŒãé¢æ°ãäœæããå Žåã«ã®ã¿å¿
èŠã§ãã
次ã«ãå
ã®ãšã³ãžã³ã§ã¯ãã°ããŒãã«ãªããžã§ã¯ãïŒãŠã£ã³ããŠãã°ããŒãã«ïŒã䜿çšããŠHTMLãšã³ã³ãŒãæ©èœã®äœ¿çšãæé©åãããããããŒãžã§ã³ã®ç«¶åã®åé¡ããããŸãã ã¯ããŒã³1.2.1ã§ã¯ããšã³ã³ãŒãé¢æ°ã®ã°ããŒãã«åããšã³ãžã³ã®ååãšããŒãžã§ã³ã«å¿ããŠéžæããããšããäºå®ã«ãã£ãŠè§£æ±ºãããŸããã 次ã®ãããªçµæã«ãªããŸããã
var encHt = '_'+dS.globalName + doT.version.replace(/\./g,''). ... encHtmlStr:'var encodeHTML=typeof '+ encHt +'!="undefined"?'+ encHt +':function(c){return((c||"")+"").replace(' + (dS.doNotSkipEncoded ?'/[&<>"\'\\/]/g':'/&(?!#?\\w+;)|[<>"\'/]/g') +',function(s){return{"&":"&","<":"<",">":">",\'"\':""","\'":"'","/":"/"}[s]||s})};'
ãã³ãã¬ãŒãé¢æ°ã«æ¿å
¥ããæååãååŸããŸããã
selfcontained = false
å Žåã¯
{{! }}
{{! }}
ããããã
encodeHTML()
ã䜿çšããããã«ãã°ããŒãã«ãªããžã§ã¯ãã§å®è¡ããããã«å¶éããŸãã
doNotSkipEncoded: false,
doT.encodeHTMLSource()
åŒæ°ã
{{! }}
{{! }}
-å®å
šãªïŒå®è¡å¯èœãªã¿ã°ãªãã®ïŒHTMLã³ãŒããçºè¡ããŸãã ç°å¢ãã³ãã¬ãŒãã«ããå Žåãå®å
šãªæåãçæãã
_globals._encodeHTML
é¢æ°ãåããŠå®çŸ©ãããç¹°ãè¿ãåŒã³åºããä¿åããŸãã ãã®ãããªãã°ã解決ããããã«äœãããŸããïŒ
github.com/olado/doT/issues/106 trueã®å Žåã
"&....;"
ãªã©ã®ãã¹ãŠã®ã³ãŒãã¯ãšã³ã³ãŒããããŸãã ãããã³äž»ãªçµæã¯ããã®ãããªåŒã®
'&'
ã¢ã³ããµã³ãã®éã³ãŒãã£ã³ã°ã§ãã
ãããã«
ã³ã³ãã€ã«é床ã®ããã«ãå¿
èŠãªã¢ã¯ã·ã§ã³ãå°ãªããã©ã¡ãŒã¿ãŒãæããã«å¿
èŠã§ãïŒå¯èœã§ããã°ã
selfcontained = false
ã
strip: false
ã
append: true
ã æ®ãã®doTããŒãã¯æé©åãããŠãããå¹³åããŠæéã®ãœãªã¥ãŒã·ã§ã³ãéžæãããŠããŸãã ããŒãžã§ã³ã®é床ã¯ãã³ãã¬ãŒãã®ç¹å®ã®ã¿ã€ãã«äŸåãããããé床ã«é¢ããã¹ããŒãã¡ã³ãã¯ã¿ã¹ã¯ã®ç¯å²ã§ã®ã¿å¹³ååã§ããŸãã
doNotSkipEncoded
ã¯ã
{{!...}}
ã³ãã³ãã®çµæã«åœ±é¿ãäžããŸã
true
&...;
ãšãã圢åŒã®ãšã³ã³ãŒããããæå
&...;
ã
äžè¬ã«ãããã€ãã®ãã°ã解決ããããã«å®è¡ããå¿
èŠã®ããã³ãŒãåæã®éãå¢å ãããããã¯ããŒã³ã¯ãã³ãã¬ãŒããé¢æ°ã«ã³ã³ãã€ã«ããŸãã ããšãã°ãunescapeïŒïŒé¢æ°ãæ¡åŒµãããŠããŸãã æåŸã®2ã€ã®çœ®æãåé€ãããšãé床ã¯3ïŒ
å¢å ããŸãïŒChrome v.61 CanaryïŒããããã€ãã®ãã°ããããŸãã
ããŒã»ã³ãåäœã«æ³šæãæããªãå ŽåãdoT.jsãã³ãã¬ãŒããšã³ãžã³ã¯æéã§ãããªããã³ã³ãã¯ãã§ãã ES6ã¯ãé
åã®æ°ããã¡ãœããã䜿çšããŸãã-ããã¯ééã£ãæ代ã«æžãããŸãããããã¯ããã¹ãŠã®ãã©ãŠã¶ãŒã§ãµããŒããããŠãããšããç¹ã§ãã©ã¹ã«ãªããŸãïŒIE8ã§ãåäœããã¯ãã§ãïŒã IE11ã§ãã¹ãæžã¿ããã¹ãããŒãžã§ã¯ãtest.index.htmlãperformance.now()
ããªãã£ã«ãããŠããŸããDoTã®ããã®ããŒãžã§ã³ããã¹ããã¡ã¢ãªã®ãã³ãããŒã¯ãæ¯èŒâ¢ - DoT12ïŒhttps://jsfiddle.net/spmbt/v3yvpbsu/26/embedded/#Result - ïŒDoT12â¢ã®DoTã®ããã®ããŒãžã§ã³ããã¹ããã¡ã¢ãªã®ãã³ãããŒã¯æ¯èŒã®httpsïŒ// jsFiddleã net / spmbt / v3yvpbsu / 26 / embedded /ïŒçµæãŸãã¯ãã¬ãŒã ãšã³ãŒãç·šéïŒhttps: //jsfiddle.net/spmbt/v3yvpbsu/26/â¢Github DoT12.jsïŒãªãªãžãã«ã®ã¯ããŒã³ïŒãDoT.jsã⢠JSFiddleã¯ããã³ãã¬ãŒããšã³ãžã³ïŒ2013ã³ãŒãïŒãšãã³ãã¬ãŒãïŒDotã¯ããŒã³ãæåã«å°å
¥ãããŸãããä»ã®é£æ¥ãããã£ãŒãçªå·ã§ã¯ãèªè
ã¯å®éšçµæãæ®ãããšãã§ããŸãã ãå®è¡ãïŒã⢠ããã©ãŒãã³ã¹ãã¹ããå«ã2013 DoT.jsã¯ããŒã³ã«é¢ããèšäºã* Closure Compiler -Advancedã¢ãŒãã§ã¯Uglifyãããå§çž®çãå°ãé«ããªããŸãã* doT.jsã®äœ¿çšïŒé©åãªäŸã2012幎ïŒ* doT.jsïŒdot.jså
ã® if-else ifã®ãã§ãŒã³if-elseãã§ãŒã³ã®äœææ¹æ³ã* èè
ã®ãµã³ãããã¯ã¹ã䜿çšãã詳现ãªããã¥ã¡ã³ããšããªã³ã¯äžã®èª¬æäŸã