-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
107 lines (94 loc) · 2.2 KB
/
doc.go
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
/*
Package stem implements a templating system.
Overview:
JSON + Template = Output
Inspiration:
http://google-ctemplate.googlecode.com
http://www.json.org
http://code.google.com/p/json-template
http://json-template.googlecode.com/svn/trunk/doc/On-Design-Minimalism.html
Features:
-Conditions.
-Includes.
-Lexical scope.
-Repeated sections.
-Change delimiter.
Design:
-Separate tags for "enter object" and "enter array" to be unambiguous.
-Support to change delimiters.
Tags:
{{!a}} Comment.
{{#a}}...{{/a}} Enter array.
{{$a}}...{{/a}} Enter object.
{{+a}}...{{/a}} Render section if defined.
{{-a}}...{{/a}} Render section if not defined.
{{>a}} Include template.
{{*a}} Print. To access element of array use "{{*}}".
{{=<ld> <rd>}} Change delimiters.
Print a symbol.
JSON:
{"a": "foo", "b": "bar"}
Template:
{{*a}}{{*b}}
Output:
foobar
Conditional output.
JSON:
{"a": ""}
Template:
{{!"+" outputs if symbol defined, "-" is the opposite.}}
{{+a}}foo{{/a}}
{{-a}}bar{{/a}}{{!"bar" is not output because a is defined.}}
Output:
foo
Enter array of documents.
JSON:
{"a": [{"b": "foo"}, {"b": "bar"}]}
Template:
{{!Evaluated once for every element.}}
{{#a}}{{*b}}{{/a}}
Output:
foobar
Enter array of non-documents.
JSON:
{"a": ["foo", "bar"]}
Template:
{{!Notice how we use a print tag without a name.}}
{{#a}}{{*}}{{/a}}
Output:
foobar
Enter object.
JSON:
{"a": {"b": "foo"}}
Template:
{{$a}}{{*b}}{{/a}}
Output:
foo
Scoped symbol lookup example 1.
JSON:
{"a": {"b": "foo"}, "c": "bar"}
Template:
{{!Notice how "c" is found in the outer scope.
Name lookup is from inner to outer scope, just like C.}}
{{$a}}{{*b}}{{*c}}{{/a}}
Output:
foobar
Scoped symbol lookup example 2.
JSON:
{"a": {"b": "foo", "c": "bar"}, "c": "baz"}
Template:
{{!Notice how the inner "c" shadows the outer "c".
This only happens when in the "a" scope.}}
{{$a}}{{*b}}{{*c}}{{/a}}{{*c}}
Output:
foobarbaz
Change delimiters.
This can be used when your document contains the default delimiters.
JSON:
{"a": "foo", "b": "bar", "c": "baz"}
Template:
{{*a}}{{=[[ ]]}}[[*b]][[=<< >>]]<<*c>>
Output:
foobarbaz
*/
package stem