1313</ head >
1414< body >
1515 < h1 > micro-template.js Demo</ h1 >
16+ < label for ="template-select "> テンプレート例</ label >
17+ < select id ="template-select ">
18+ < option value ="normal "> 通常(正常終了)</ option >
19+ < option value ="error "> 3行目でエラー</ option >
20+ < option value ="wrapper "> wrapper() 使用例</ option >
21+ </ select >
22+
1623 < label for ="template "> Template</ label >
1724 < textarea id ="template " placeholder ="<h1><%= title %></h1>\n<p><%= message %></p> "> </ textarea >
1825
@@ -26,12 +33,37 @@ <h1>micro-template.js Demo</h1>
2633 < pre id ="output "> </ pre >
2734
2835 < script type ="module ">
29- // micro-template.js を ESMとしてimport
30- import { template } from "../lib/micro-template.js" ;
31-
36+ import { extended as template } from "../lib/micro-template.js" ;
3237 window . template = template ;
3338
39+ template . get = function ( name ) {
40+ return {
41+ wrapper : `foo: <%= foo %>\nThis is wrapper header\n<%=raw content %>\nThis is wrapper footer`
42+ } [ name ] || '' ;
43+ } ;
44+
45+ // テンプレート例
46+ const templates = {
47+ normal : `<h1><%= title %></h1>\n<p><%= message %></p>` ,
48+ error : `<h1><%= title %></h1>\n<p><%= message %></p>\n<%= notfound %>` ,
49+ wrapper : `<% wrapper('wrapper', function () { %>\n<h1>Hello, World!</h1>\n<% }) %>`
50+ } ;
51+ const stashes = {
52+ normal : {
53+ title : "Hello" ,
54+ message : "こんにちは micro-template.js!"
55+ } ,
56+ error : {
57+ title : "Hello" ,
58+ message : "これはエラー例です"
59+ } ,
60+ wrapper : {
61+ foo : "タイトル(wrapper用)"
62+ }
63+ } ;
64+
3465 // UI
66+ const templateSelect = document . getElementById ( 'template-select' ) ;
3567 const templateEl = document . getElementById ( 'template' ) ;
3668 const stashEl = document . getElementById ( 'stash' ) ;
3769 const outputEl = document . getElementById ( 'output' ) ;
@@ -55,20 +87,27 @@ <h1>micro-template.js Demo</h1>
5587 statusEl . textContent = 'OK' ;
5688 statusEl . className = '' ;
5789 } catch ( e ) {
58- console . log ( e )
59- console . error ( e ) ;
90+ console . log ( e )
91+ console . error ( e ) ;
6092 outputEl . textContent = '' ;
6193 statusEl . textContent = 'Template error: ' + String ( e ) ;
6294 statusEl . className = 'error' ;
6395 }
6496 }
6597 renderBtn . addEventListener ( 'click' , render ) ;
98+
99+ // テンプレート切り替え
100+ templateSelect . addEventListener ( 'change' , ( ) => {
101+ const key = templateSelect . value ;
102+ templateEl . value = templates [ key ] ;
103+ stashEl . value = JSON . stringify ( stashes [ key ] , null , 2 ) ;
104+ render ( ) ;
105+ } ) ;
106+
66107 // デフォルト値
67- templateEl . value = `<h1><%= title %></h1>\n<p><%= message %></p>` ;
68- stashEl . value = JSON . stringify ( {
69- title : "Hello" ,
70- message : "こんにちは micro-template.js!"
71- } , null , 2 ) ;
108+ templateSelect . value = 'normal' ;
109+ templateEl . value = templates . normal ;
110+ stashEl . value = JSON . stringify ( stashes . normal , null , 2 ) ;
72111 render ( ) ;
73112 </ script >
74113</ body >
0 commit comments