You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This plugin provides a syntax for including local variables and expressions in your templates, and also extends custom tags to act as helpers for conditionals and looping.
33
+
34
+
You have full control over the delimiters used for injecting locals, as well as the tag names for the conditional and loop helpers, if you need them. All options that can be passed to the `exp` plugin are shown below:
35
+
36
+
| Option | Description | Default |
37
+
| ------ | ----------- | ------- |
38
+
|**delimiters**| Array containing beginning and ending delimiters for escaped locals. |`['{{', '}}']`|
39
+
|**unescapeDelimiters**| Array containing beginning and ending delimiters for inserting unescaped locals. |`['{{{', '}}}']`|
40
+
|**locals**| Object containing any local variables you want to be available inside your expressions. |
41
+
|**conditionalTags**| Array containing names for tags used for standard `if`/`else if`/`else` logic |`['if', 'elseif', 'else']`|
42
+
|**loopTags**| Array containing names for standard `for` loop logic |`['each']`|
43
+
25
44
### Locals
26
45
46
+
You can inject locals into any piece of content in your html templates, other than overwriting tag names. For example, if you passed the following config to the exp plugin:
47
+
27
48
```js
28
-
constexp=require('posthtml-exp')({
29
-
locals: {}
49
+
exp({
50
+
locals: { class:'intro', name:'Jeff'}
30
51
})
31
52
```
32
53
54
+
And compiled with the following template:
55
+
56
+
```html
57
+
<divclass="{{class}}">
58
+
My name is {{name}}
59
+
</div>
60
+
```
61
+
62
+
You would get this as your output:
63
+
64
+
```html
65
+
<divclass="intro">
66
+
My name is Jeff
67
+
</div>
68
+
```
69
+
70
+
### Unescaped Locals
71
+
72
+
By default, special characters will be escaped so that they show up as text, rather than html code. For example, if you had a local containing valid html as such:
73
+
33
74
```js
34
-
constexp=require('posthtml-exp')({
35
-
locals:'path/to/file.(js|json)'
75
+
exp({
76
+
locals:{ statement:'<strong>wow!</strong>' }
36
77
})
37
78
```
38
79
39
-
### Paths
80
+
And you rendered it into a tag like this:
81
+
82
+
```html
83
+
<p>The fox said, {{ statement }}</p>
84
+
```
85
+
86
+
You would see the following output:
40
87
41
-
Expression and Helper arguments can be at least nested 3 levels deep.
88
+
```html
89
+
<p>The fox said, <strong>wow!<strong></p>
90
+
```
91
+
92
+
In your browser, you would see the angle brackets, and it would appear as intended. However, if you wanted it instead to be parsed as html, you would need to use the `unescapeDelimiters`, which by default are three curly brackets, like this:
93
+
94
+
```html
95
+
<p>The fox said, {{{ statement }}}</p>
42
96
43
-
```js
44
-
{
45
-
obj: {
46
-
1: {
47
-
2: {
48
-
3:'PostHTML Expressions'
49
-
}
50
-
}
51
-
}
52
-
}
53
97
```
98
+
99
+
In this case, your code would render as html:
100
+
54
101
```html
55
-
<div>
56
-
{{obj.1.2.3}}
57
-
</div>
102
+
<p>The fox said, <strong>wow!<strong></p>
58
103
```
104
+
105
+
### Expressions
106
+
107
+
You are not limited to just directly rendering local variables either, you can include any type of javascript expression and it will be evaluated, with the result rendered. For example:
With this in mind, it is strongly recommended to limit the number and complexity of expressions that are run directly in your template. You can always move the logic back to your config file and provide a function to the locals object for a smoother and easier result. For example:
Conditional logic uses normal html tags, and modifies/replaces them with the results of the logic. If there is any chance of a conflict with other custom tag names, you are welcome to change the tag names this plugin looks for in the options. For example, given the following config:
133
+
134
+
```js
135
+
exp({
136
+
locals: { foo:'foo' }
137
+
})
138
+
```
139
+
140
+
And the following html:
141
+
74
142
```html
75
-
<ul>
76
-
<li>{...fruits}</li>
77
-
</ul>
143
+
<ifcondition="foo === 'bar'">
144
+
<p>Foo really is bar! Revolutionary!</p>
145
+
</if>
146
+
<elseifcondition="foo === 'wow'">
147
+
<p>Foo is wow, oh man.</p>
148
+
</elseif>
149
+
<else>
150
+
<p>Foo is probably just foo in the end.</p>
151
+
</else>
78
152
```
153
+
154
+
Your result would be only this:
155
+
79
156
```html
80
-
<ul>
81
-
<li>Apple</li>
82
-
<li>Orange</li>
83
-
<li>Mango</li>
84
-
</ul>
157
+
<p>Foo is probably just foo in the end.</p>
158
+
```
159
+
160
+
Anything in the `condition` attribute is evaluated directly as an expression.
161
+
162
+
It should be noted that this is slightly cleaner-looking if you are using the [SugarML](https://github.com/posthtml/sugarml). But then again so is every other part of html.
163
+
164
+
```sml
165
+
if(condition="foo === 'bar'")
166
+
p Foo really is bar! Revolutionary!
167
+
elseif(condition="foo === 'wow'")
168
+
p Foo is wow, oh man.
169
+
else
170
+
p Foo is probably just foo in the end.
85
171
```
86
172
87
-
#### Pipe **{ | }**
173
+
#### Loops
174
+
175
+
You can use the `each` tag to build loops. It works with both arrays and objects. For example:
88
176
89
177
```js
90
-
{
178
+
exp({
91
179
locals: {
92
-
firstname:'PostHTML',
93
-
lastname:'Expressions'
180
+
array: ['foo', 'bar'],
181
+
object: { foo:'bar' }
94
182
}
95
-
}
183
+
})
96
184
```
185
+
97
186
```html
98
-
<h1>{firstname | lastname}</h1>
187
+
<eachloop="item, index in anArray">
188
+
<p>{{ index }}: {{ item }}</p>
189
+
</each>
99
190
```
191
+
192
+
Output:
193
+
100
194
```html
101
-
<h1>PostHTML Expressions</h1>
195
+
<p>1: foo</p>
196
+
<p>2: bar</p>
102
197
```
103
198
104
-
#### Import **{> }**
105
-
```js
106
-
{
107
-
locals: {
108
-
button:'./button.html'
109
-
}
110
-
}
199
+
And an example using an object:
200
+
201
+
```html
202
+
<eachloop="key, value in anObject">
203
+
<p>{{ key }}: {{ value }}</p>
204
+
</each>
111
205
```
206
+
207
+
Output:
208
+
112
209
```html
113
-
<div>
114
-
{> button}
115
-
</div>
210
+
<p>foo: bar</p>
116
211
```
212
+
213
+
The value of the `loop` attribute is not a pure expression evaluation, and it does have a tiny and simple custom parser. Essentially, it starts with one or more variable declarations, comma-separated, followed by the word `in`, followed by an expression.
214
+
215
+
So this would also be fine:
216
+
117
217
```html
118
-
<div>
119
-
<button>Click Me!</button>
120
-
</div>
218
+
<eachloop="item in [1,2,3]">
219
+
<p>{{ item }}</p>
220
+
</each>
121
221
```
122
222
223
+
So you don't need to declare all the available variables (in this case, the index is skipped), and the expression after `in` doesn't need to be a local variable, it can be any expression.
0 commit comments