Skip to content

Commit 0640dfa

Browse files
docs(); resolve merge conflicts
2 parents 7a29397 + f09535d commit 0640dfa

File tree

2 files changed

+166
-79
lines changed

2 files changed

+166
-79
lines changed

README.md

Lines changed: 165 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<div align="center">
88
<img width="220" height="150" title="PostHTML" src="http://posthtml.github.io/posthtml/logo.svg">
99
<h1>Expressions Plugin</h1>
10+
<p>Local variables, expressions, loops, conditionals and unicorns 👍</p>
1011
</div>
1112

1213
<h2 align="center">Install</h2>
@@ -17,154 +18,243 @@ npm i -D posthtml-exp
1718

1819
<h2 align="center">Usage</h2>
1920

21+
```js
22+
const { readFileSync } = require('fs')
2023

21-
```html
22-
<div id="{{id}}" class="{{class}}">{{content}}</div>
24+
const posthtml = require('posthtml')
25+
const exp = require('posthtml-exp')
26+
27+
posthtml(exp({ locals: { foo: 'bar' } }))
28+
.process(readFileSync('index.html', 'utf8'))
29+
.then((result) => console.log(result.html))
2330
```
2431

32+
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+
2544
### Locals
2645

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+
2748
```js
28-
const exp = require('posthtml-exp')({
29-
locals: {}
49+
exp({
50+
locals: { class: 'intro', name: 'Jeff' }
3051
})
3152
```
3253

54+
And compiled with the following template:
55+
56+
```html
57+
<div class="{{class}}">
58+
My name is {{name}}
59+
</div>
60+
```
61+
62+
You would get this as your output:
63+
64+
```html
65+
<div class="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+
3374
```js
34-
const exp = require('posthtml-exp')({
35-
locals: 'path/to/file.(js|json)'
75+
exp({
76+
locals: { statement: '<strong>wow!</strong>' }
3677
})
3778
```
3879

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:
4087

41-
Expression and Helper arguments can be at least nested 3 levels deep.
88+
```html
89+
<p>The fox said, &lt;strong&gt;wow!&lt;strong&gt;</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>
4296

43-
```js
44-
{
45-
obj: {
46-
1: {
47-
2: {
48-
3: 'PostHTML Expressions'
49-
}
50-
}
51-
}
52-
}
5397
```
98+
99+
In this case, your code would render as html:
100+
54101
```html
55-
<div>
56-
{{obj.1.2.3}}
57-
</div>
102+
<p>The fox said, <strong>wow!<strong></p>
58103
```
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:
108+
59109
```html
60-
<div>
61-
PostHTML Expressions
62-
</div>
110+
<p class="{{ env === 'production' ? 'active' : 'hidden' }}">in production!</p>
63111
```
64112

65-
### Helpers
66-
#### Each **{...}**
113+
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:
114+
115+
67116
```js
68-
{
117+
exp({
69118
locals: {
70-
fruits: ['Apple', 'Orange', 'Mango']
119+
isProduction: (env) => {
120+
return env === 'production' ? 'active' : 'hidden'
121+
}
71122
}
72-
}
123+
})
73124
```
125+
126+
```html
127+
<p class="{{ isProduction(env) }}">in production!</p>
128+
```
129+
130+
### Conditional Logic
131+
132+
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+
74142
```html
75-
<ul>
76-
<li>{...fruits}</li>
77-
</ul>
143+
<if condition="foo === 'bar'">
144+
<p>Foo really is bar! Revolutionary!</p>
145+
</if>
146+
<elseif condition="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>
78152
```
153+
154+
Your result would be only this:
155+
79156
```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.
85171
```
86172

87-
#### Pipe **{ | }**
173+
#### Loops
174+
175+
You can use the `each` tag to build loops. It works with both arrays and objects. For example:
88176

89177
```js
90-
{
178+
exp({
91179
locals: {
92-
firstname: 'PostHTML',
93-
lastname: 'Expressions'
180+
array: ['foo', 'bar'],
181+
object: { foo: 'bar' }
94182
}
95-
}
183+
})
96184
```
185+
97186
```html
98-
<h1>{firstname | lastname}</h1>
187+
<each loop="item, index in anArray">
188+
<p>{{ index }}: {{ item }}</p>
189+
</each>
99190
```
191+
192+
Output:
193+
100194
```html
101-
<h1>PostHTML Expressions</h1>
195+
<p>1: foo</p>
196+
<p>2: bar</p>
102197
```
103198

104-
#### Import **{> }**
105-
```js
106-
{
107-
locals: {
108-
button: './button.html'
109-
}
110-
}
199+
And an example using an object:
200+
201+
```html
202+
<each loop="key, value in anObject">
203+
<p>{{ key }}: {{ value }}</p>
204+
</each>
111205
```
206+
207+
Output:
208+
112209
```html
113-
<div>
114-
{> button}
115-
</div>
210+
<p>foo: bar</p>
116211
```
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+
117217
```html
118-
<div>
119-
<button>Click Me!</button>
120-
</div>
218+
<each loop="item in [1,2,3]">
219+
<p>{{ item }}</p>
220+
</each>
121221
```
122222

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.
224+
123225
<h2 align="center">Example</h2>
124226

125227
```js
126-
'use strict'
127-
128228
const { readFileSync } = require('fs')
129229

130230
const posthtml = require('posthtml')
131-
132-
const exp = require('posthtml-exp')({
133-
locals: {
134-
id: 'title',
135-
class: 'header',
136-
content: 'PostHTML Expressions'
137-
}
138-
})
231+
const exp = require('posthtml-exp')
139232

140233
const html = readFileSync('./index.html', 'utf8')
141234

142235
posthtml([ exp ])
143-
.process(file)
236+
.process(html)
144237
.then(result => console.log(result.html))
145238
```
146239

147240
###### Input
148241

149242
```html
150-
<div class={{class}}>
151-
<h1 id={{id}}>{{content}}</h1>
152-
</div>
243+
153244
```
154245

155246
###### Output
156247

157248
```html
158-
<div class="header">
159-
<h1 id="title">PostHTML Expressions</h1>
160-
</div>
249+
161250
```
162251

163252
<h2 align="center">LICENSE</h2>
164253

165254
> MIT License (MIT)
166255
167-
> Copyright (c) 2016 PostHTML Michael Ciniawsky <[email protected]>
256+
> Copyright (c) 2016 PostHTML Jeff Escalante
257+
Michael Ciniawsky <[email protected]>
168258
169259
> Permission is hereby granted, free of charge, to any person obtaining a copy
170260
of this software and associated documentation files (the "Software"), to deal

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
"name": "posthtml-exp",
33
"version": "0.8.1",
44
"description": "Expressions for PostHTML",
5-
"engines": {
6-
"npm": ">=3",
7-
"node": ">=4"
8-
},
5+
"engines": {"node": ">=6"},
96
"main": "index.js",
107
"scripts": {
118
"clean": "echo '=> Cleaning' && sudo rm -rf .nyc_output coverage",

0 commit comments

Comments
 (0)