-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadme.html
More file actions
659 lines (598 loc) · 21.8 KB
/
readme.html
File metadata and controls
659 lines (598 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
<html>
<head>
<meta charset="utf-8" />
<script src="3party/jquery-1.11.3.js"></script>
<script src="3party/json2.js"></script>
<script src="3party/jsplus.js"></script>
<script src="jst-core/jst.js"></script>
<script src="jst-core/observe.js"></script>
<style>
.sample {
border: 1px solid gray;
padding: 0;
margin: 0;
margin-top:10px;
}
.title {
background-color: black;
color: white;
padding:0;
margin:0;
}
br {
margin:0
}
fieldset{
margin: 10px 20px 10px 20px;
border: 1px solid darkgray;
}
h1 {
text-indent: 20px;
}
pre{
tab-size:2;
}
</style>
</head>
<body>
<div class="sample">
<h1 class="title">Guide</h1>
<pre>
What is JST:
JST is a small, fast template utility in replacing for AngularJS / Vue.
It can be used in conjunction with almost all other frameworks, such as jquery, dojo.
Also you can use JST to make your own framework as well.
Usage:
Html:
<div id="template">
<input type="text" value="{{name}}">
<div>Your age is {{age}}.</div">
</div>
<div id="output">
</div>
Js:
var template = document.getElementById("template"); //template
var target = document.getElementById("output"); //render target
var j = new jst(template, target); //create and compile template
j.render({name: 'joe', age: 10}); //render
Output html:
<div id="template">
<input type="text" value="{{name}}">
<div>Your age is {{age}}.</div">
</div>
<div id="output">
<input type="text" value="joe">
<div>Your age is 10.</div">
</div>
About MVVM:
MVVM support is implemented by using Object.definedProperty.
To set a value in array. use ary.item(index, value);
value changes using the following method will not be detected.
delete property/element:
delete ary[i] => use ary.splice(i, 1)
delete obj.prop => use obj.$delete(name) (under construction)
add property:
obj.newProperty = 1 => use obj.$prop(name, value) (under construction)
obj[newProperty] = 1 => use obj.$prop(name, value) (under construction)
Example:
Html:
...
<input type="text" value="{{count}}" jst-bind="@count" >
<div>The count is {{count}}.</div">
...
Js:
var j = new jst("template", "output", false);
var data = {count: 100};
j.render(data); //render
j.watch(data); //add watch to the rendered data
//add event listener. use jquery for convenience.
$(target).on("input", ":text, textarea", function(){
var dom = event.target, obj = jst.get_bind_obj(dom); //get the object
if(obj){
var key = jst.get_bind_key(dom); //get the key
obj[key] = dom.value; //set the value to the binded object
}
});
** the value of jst-bind must in the form of obj@key.
** @count equals to $data.count
** tags@name equals to $data.tags.name
** tags@{{i}} equals to $data.tags[i]
** tags@5 equals to $data.tags[5]
** the key can be an string expression. eg. tags@col_{i}
API document:
constructor:
jst(name, dom, options);
name: unique name of the template
dom: dom object / id of dom object / template string
options:
preserveWhiteSpace: trim textnode. default: false.
wrapText: wrap textnode of {{...}} with span tag. default: false
instance method:
render(data, refresh);
data: the data used for template
dom: the output target
refresh: true: refresh mode, false: patch mode. default: false.
watch(data);
data: the data object to watch
class method:
jst.set_data(node, name, value) //store data to node
jst.get_data(node, name) //get dom's stored data
jst.stock_functions //stock functions. change with care
jst.get_bind_obj //get the binded object
jst.get_bind_key //get the key of the binded object
jst.prefix //prefix of jst attr name. default is "jst-"
jst.special_attributes //special attributes definition. do not change!
jst.directives //directives management
insert(directive, name) //insert directive before the directive specfied by the name
remove(name) //remove the directive by name
indexOf(name) //return the compile order of the directive
find(name) //find the directive by name
List of template directives in compile order:
jst-skip : Skip the processing of this node. Used when the node contains a large third party user control(eg. google map),
*The processing of the following directions is also skipped.
jst-begin : Call javascript functions before processing
jst-if : Output the node when the expression is valued true
jst-repeat : Repeat the node itself
jst-each : Repeat the node itself, using forEach function.
jst-filter : Filter condition for jst-repeat
jst-item-begin : Call javascript functions when output the node. used with jst-repeat only.
jst-var-* : Safely set a variable to a var. To avoid undefined error.see tree demo.
jst-html : Set the innerHTML property value to the result of the expression
jst-text : Set the innerText property value to the result of the expression
jst-bind : Store a data binding (obj,key) to the node.
jst-set-* : Store data to dom object, using name after 'jst-set-'.
: *Using $ctl["@" + key] = value, can be replaced by using jquery.data.
jst-prepend-* : Prepend value to the value named after 'jst-set-'.
jst-append-* : Append value to the value named after 'jst-set-'.
jst-* or * : other attributes. eg. jst-value, jst-style, jst-class, jst-data-property, etc.
jst-on* : Bind event handler.(see detail on how to)
jst-call : Execute javascript functions before processing it's child nodes
jst-purge : Clean child nodes when the expression is valued true
jst-recursive : Skip the processing of the child nodes when the expression is valued false
jst-include : include other template and render into the content of the node. see tree demo for recursive include.
jst-item-end : Call javascript functions after output the node and it's child nodes. used with jst-repeat only
jst-end : Call javascript functions after processing the node and it's child nodes
*jst-html is superior to jst-text
Change the way of data binding.
the jst-bind and jst-data-* use $ctl[name] = value to store the data to dom
to change the way of it, eg, using jquery, override the get_data and set_data functions before render.
jst.set_data = function(node, name, value){
$(node).data(name, value);
}
jst.get_data = function(node, name){
return $(node).data(name);
}
About jst-on*
In normal html dom, the value of onXX is something like onclick="alert('hello world')"
The value of jst-on* should be a full function expression. eg. jst-onclick="function(){alert('hello world'}"
But using a variable defined in the repeat, call, begin, end will cause unpredictable result.
eg.
<button jst-repeat="var i=0; i < 10; ++i" jst-onclick="function(){alert('hello world:'+i)}" >test{{i}}</button>
each button click will alert the same message of 'hello world:10' .
In order to solve this problem, you should use an anonymous inline function.
<button jst-repeat="var i=0; i < 10; ++i"
jst-onclick="(function(i){return function(){alert('hello world:'+i)}})(i)" >test{{i}}</button>
an other way to solve this problem, store the value of i to the node using jst-set-idx="i"
jst-onclick="function(){alert('hello world:'+jst.get_data(this, 'idx'))}"
see the 'event sample' for more other solutions.
Pre-defined varibles for template expression:
$jst : the jst class object
$target : the render target
$data : refer to the render data
$ctl : the current node on processing
**for advanced usage only**
Notice:
changing dom node with care.
How to write template:
Template language is pure javascript expression. See the following sample.
No additional rules or syntax!
Advanced usage:
for more advanced usage, see the samples in the samples folder.(under construction)
1) user defined directive.
2) use with jquery-ui
3) use with dojo
*this sample uses jquery and jsplus(a js prototype extension for old browser.)
</pre>
</div>
<div name="test" class="sample">
<h1 class="title">simple text output</h1>
<script type="text/template"name="html">
<div>simple:{{text}}</div>
</script>
<script type="text/template" name="data">
{
text: "john smith is <b>tall</b>.\r\nThis is second line.\nThis is thrid line.\rThis is forth line."
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">text output</h1>
<script type="text/template" name="html">
<div>jst-text: <span jst-text="{{text}}"></span></div>
</script>
<script type="text/template" name="data">
{
text: "john smith is <b>tall</b>.\r\nThis is second line.\nThis is thrid line.\rThis is forth line."
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">html output</h1>
<script type="text/template" name="html">
<div>jst-html:<span jst-html="{{text}}"></span></div>
</script>
<script type="text/template" name="data">
{
text: "john smith is <b>tall</b>.\r\nThis is second line.\nThis is thrid line.\rThis is forth line."
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">complex text output</h1>
<script type="text/template" name="html">
<div>mix with static text:<span jst-text="==={{text.toUpperCase() + ' plus alpha'}}==="></span></div>
</script>
<script type="text/template" name="data">
{
text: "john smith is <b>tall</b>.\r\nThis is second line.\nThis is thrid line.\rThis is forth line."
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">textbox</h1>
<script type="text/template" name="html">
<div>name:<input type="text" jst-value="{{name}}"></div>
<div>name:<input readonly type="text" jst-value="{{name}}"></div>
<div>name:<input disabled type="text" jst-value="{{name}}"></div>
</script>
<script type="text/template" name="data">
{
name: "john smith"
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">select</h1>
<script type="text/template" name="html">
<div>
<select>
<option value="1" jst-selected="{{$ctl.value==value}}">apple</option>
<option value="2" jst-selected="{{$ctl.value==value}}">banana</option>
<option value="3" jst-selected="{{$ctl.value==value}}">orange</option>
</select>
</div>
</script>
<script type="text/template" name="data">
{
value: 2
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">radio</h1>
<script type="text/template" name="html">
<div>
<input type="radio" name="gender" value="0" jst-checked="{{$ctl.value==gender}}">male
<input type="radio" name="gender" value="1" jst-checked="{{$ctl.value==gender}}">female
<input type="radio" name="gender" value="2" jst-checked="{{$ctl.value==gender}}">bi-sex
</div>
</script>
<script type="text/template" name="data">
{
gender: 1
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">checkbox</h1>
<script type="text/template" name="html">
<div>
<input type="checkbox" name="fish" value="1" jst-checked="{{meal.fish==1}}">fish
<input type="checkbox" name="pork" value="1" jst-checked="{{meal.pork==1}}">pork
<input type="checkbox" name="beef" value="1" jst-checked="{{meal.beef==1}}">beef
</div>
</script>
<script type="text/template" name="data">
{
meal: {
fish: 0,
pork: 1,
beef: 1
}
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">expression & others</h1>
<script type="text/template" name="html">
<div>
<input type="text" jst-value="{{text.toUpperCase()}}">
</div>
<div>
<div jst-style="width:80px;border:{{width}}px solid black;margin:{{width}};padding:{{width}}px;background-color:{{color}}">test</div>
</div>
<div>
<div style="border:5px solid black;" jst-prepend-style="width:80px;border:{{width}}px solid black;margin:{{width}};padding:{{width}}px;background-color:{{color}};">test</div>
</div>
<div>
<div style="border:5px solid black;font-size: 46px;" jst-append-style="width:80px;border:{{width}}px solid black;margin:{{width}};padding:{{width}}px;background-color:{{color}}">test</div>
</div>
attributes other than predefined directives, which starts with 'jst-' will be outputed without 'jst-'.
used for img's src attribute to avoid network error.
</script>
<script type="text/template" name="data">
{
text: "Hello World!",
width: 2,
color: "#ff7788"
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">condition</h1>
<script type="text/template" name="html">
<div>
<span jst-if="value1 >= 0" style="color:black">{{value1}}</span>
<span jst-if="value1 < 0" style="color:red">{{value1}}</span>
</div>
<div>
<span jst-if="value2 >= 0" style="color:black">{{value2}}</span>
<span jst-if="value2 < 0" style="color:red">{{value2}}</span>
</div>
<div>
<span jst-style="color:{{(value2 < 0)? 'red':'black'}}">{{value2}}</span>
</div>
</script>
<script type="text/template" name="data">
{
value1: 7,
value2: -10
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">condition</h1>
<script type="text/template" name="html">
<div>
<span jst-purge="value1 >= 0" style="color:black">{{value1}}</span>
<span jst-purge="value1 < 0" style="color:red">{{value1}}</span>
</div>
<div>
<span jst-purge="value2 >= 0" style="color:black">{{value2}}</span>
<span jst-purge="value2 < 0" style="color:red">{{value2}}</span>
</div>
</script>
<script type="text/template" name="data">
{
value1: 7,
value2: -10
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">repeat</h1>
<script type="text/template" name="html">
<div>
<ul>
<li jst-repeat="var idx=0; idx < list.length; ++ idx">{{idx}}:{{list[idx]}}</li>
</ul>
</div>
the jst-repeat is the same as [for] expression
</script>
<script type="text/template" name="data">
{
list: ['a', 'b', 'c', 'd']
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">each</h1>
<script type="text/template" name="html">
<div>
<ul>
<li jst-each="(n, i, a):list"><button jst-onclick="alert(i+':'+n)">{{i}}:{{n}}</button></li>
</ul>
</div>
</script>
<script type="text/template" name="data">
{
list: ['a', 'b', 'c', 'd']
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">repeat with output</h1>
<script type="text/template" name="html">
<div>
<ul>
<li jst-repeat="var idx=0; idx < list.length; ++ idx" jst-text="{{idx}}:{{list[idx]}}"></li>
</ul>
</div>
use jst-text in repeat.
</script>
<script type="text/template" name="data">
{
list: ['a', 'b', 'c', 'd']
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">combination</h1>
<script type="text/template" name="html">
<div jst-skip>
add some large widget here. like google map. {{value}} is not parsed.
</div>
<div class="test"
jst-begin="console.log('begin')"
jst-if="show"
jst-repeat="var i=0; i < count; ++ i"
jst-filter="i %2 == 0"
jst-item-begin="console.log('item-begin')"
jst-style="background:{{(i % 4== 0)? '#e9e9e9' : '#e9e980'}}"
others="tag:{{i}}"
jst-call="console.log('call')"
jst-recursive="true"
jst-item-end="console.log('item-end')"
jst-end="console.log('end')">
<span jst-text="row:{{i}}"></span>
</div>
see console output.
</script>
<script type="text/template" name="data">
{
count: 10,
show: true
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">event sample</h1>
<script type="text/template" name="html">
<button jst-onclick="function(){alert('ok')}">ok</button>
click the button to see the result<br><br>
simple:
<button jst-repeat="var i=0; i < 3; ++ i"
jst-onclick="function(){alert('hello:'+ name + i)}"
jst-item-begin="name = 'joe' + i">showme{{i}}</button>(always the same message!)<br><br>
using set data/attr:
<button jst-repeat="var i=0; i < 3; ++ i"
jst-set-idx="i"
name = "{{name}}"
jst-onclick="function(){alert('hello:'+ name + ',' + this.getAttribute('name') + jst.get_data(this, 'idx'))}"
jst-item-begin="name = 'joe' + i">showme{{i}}</button>(using name will show the last value)<br><br>
using anonimous function:
<button jst-repeat="var i=0; i < 3; ++ i"
jst-onclick="(function(i){return function(){alert('hello:'+ name + i)}})(i)"
jst-item-begin="name = 'joe' + i">showme{{i}}</button>(name is missing, always show the last value of name)<br><br>
using anonimous function(add 'name'):
<button jst-repeat="var i=0; i < 3; ++ i"
jst-onclick="(function(name, i){return function(){alert('hello:'+ name + i)}})(name, i)"
jst-item-begin="name = 'joe' + i">showme{{i}}</button><br><br>
recomended:
<button jst-repeat="var i=0; i < 3; ++ i"
data-idx="{{i}}"
data-name = "{{name}}"
jst-onclick="showme"
jst-item-begin="name = 'joe' + i">showme{{i}}</button><br><br>
conclusion: declare all the variables used in the event handler as the parameter of anonimous function.
or use jst-each directive instead.
</script>
<script type="text/template" name="data">
{
name: "Joe",
showme: function(){
var name = this.getAttribute("data-name");
var idx = this.getAttribute("data-idx");
alert(name + "," + idx);
}
};
</script>
</div>
<div name="test" class="sample">
<h1 class="title">Bidirectional binding binding(IE9+)</h1>
<script type="text/template" name="html">
name: <input type="text" value="{{name}}" jst-bind="@name" jst-oninput="(function(d){return function(){d.name=this.value;console.log(d)}})($data)"><br> hello {{name}}
<div jst-begin="$jst.watch($data);">only for sample. use with js.</div>
</script>
<script type="text/template" name="data">
{
name: "Joe"
};
</script>
</div>
<div name="test" class="sample" id="t">
<h1 class="title">Tree Demo</h1>
<script type="text/template" name="html">
<ul>
<li jst-include="'treenode'" jst-include-data="tree"></li>
</ul>
</script>
<script type="text/template" name="data">
{
tree: {
name: 'aa',
children: [
{
name: "11"
},
{
name: "22",
children: [
{
name: "22-3",
children: [
{
name: "22-4"
}
]
},
{
name: "22-32",
children: [
{
name: "22-42"
}
]
}
]
}
]
}
}
</script>
</div>
<div id="treenode" style="display:none">
<a href="#" jst-var-subs="children">{{name}}</a>
<ul jst-if="subs && subs.length > 0" >
<li jst-repeat="var i =0; i < children.length; ++ i" jst-include="'treenode'" jst-include-data="children[i]">
</li>
</ul>
</div>
</body>
<script>
function trim(t){
t = t.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
var s = t.split(/\n/g);
for(var i = s.length -1;i >=0; -- i){
if(s[i].replace(/^\s*/g, "") != ""){
s[i] = s[i].substr(4);
}else{
s.splice(i,1);
}
}
return s.join("\n");
}
function html_encode(str)
{
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/ /g, " ").replace(/\t/g, " ").replace(/\'/g, "'").replace(/\"/g, """).replace(/\n/g, "<br>");
}
$(document).ready(function(){
var idx = 0;
$("[name=test]").each(function(){
$(this).append("<fieldset><legend>HTML</legend><div name='html'></div></fieldset>");
$(this).append("<fieldset><legend>Data</legend><div name='data'></div></fieldset>");
$(this).append("<fieldset><legend>Output</legend><div name='output'></div></fieldset>");
$(this).append("<fieldset><legend>Display</legend><div name='display'></div></fieldset>");
var o;
o = $(this).find("script[name=html]");
var html = trim(o.html());
$(this).find("div[name=html]").html(html_encode(html));
var o = $(this).find("script[name=data]");
var data = trim(o.html());
$(this).find("div[name=data]").html(html_encode(data));
var d = (new Function('return ' + data.replace(/^\n/g, "")+";"))();
var o = $(this).find("[name=display]");
var t = new jst(html, o[0]);
t.render(d);
var o = $(this).find("[name=output]");
o.html(html_encode($(this).find("[name=display]").html()));
});
var p = $("<fieldset></fieldset>");
var t = $("<pre></pre>");
t.text($("#treenode").prop("outerHTML"));
p.append("<legend>treenode</legend>");
p.append(t);
$("#t").append(p);
});
</script>
</html>