Skip to content

Commit 3d04646

Browse files
author
Jeff Escalante
committed
new readme spec
1 parent d96c0b6 commit 3d04646

File tree

2 files changed

+146
-149
lines changed

2 files changed

+146
-149
lines changed

README.md

Lines changed: 145 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,213 +1,210 @@
11
# Expressions for PostHTML
22

3-
## Install
4-
```sh
5-
npm i posthtml-exp
6-
```
3+
Local variables, expressions, loops, and conditionals in your html
74

8-
## Options
9-
### Style
10-
Choose one of the following expression syntaxes. If **options.style** is unset, the default syntax (JSX) is used.
5+
## Installation
116

12-
##### JSX: **'{'**
7+
First, install from npm with `npm i posthtml-exp --save`, then add it as a plugin to your posthtml pipeline:
138

14-
```html
15-
<div id="{id}" class="{class}">{content}</div>
16-
```
17-
##### HBS: **'{{'**
9+
```js
10+
const posthtml = require('posthtml')
11+
const exp = require('posthtml-exp')
12+
const {readFileSync} = require('fs')
1813

19-
```html
20-
<div id="{{id}}" class="{{class}}">{{content}}</div>
14+
posthtml(exp({ locals: { foo: 'bar' } }))
15+
.process(readFileSync('exampleFile.html', 'utf8'))
16+
.then(console.log)
2117
```
22-
##### Blaze: **'@'**
2318

24-
```html
25-
<div id="@id" class="@name">@content</div>
26-
```
19+
## Usage
2720

28-
For example, to change to handlebars-style delimiters:
21+
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.
2922

30-
```js
31-
let exps = require('posthtml-exp')({
32-
style: '{{'
33-
})
34-
```
23+
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:
24+
25+
| Option | Description | Default |
26+
| ------ | ----------- | ------- |
27+
| **delimiters** | Array containing beginning and ending delimiters for escaped locals. | `['{{', '}}']` |
28+
| **unescapeDelimiters** | Array containing beginning and ending delimiters for inserting unescaped locals. | `['{{{', '}}}']` |
29+
| **locals** | Object containing any local variables you want to be available inside your expressions. |
30+
| **conditionalTags** | Array containing names for tags used for standard `if`/`else if`/`else` logic | `['if', 'elseif', 'else']` |
31+
| **loopTags** | Array containing names for standard `for` loop logic | `['each']` |
3532

3633
### Locals
37-
#### Set locals directly as arguments
38-
```js
39-
let exps = require('posthtml-exp')({
40-
locals: {/* locals */}
41-
})
42-
```
43-
#### Load locals from an external file
34+
35+
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:
36+
4437
```js
45-
let exps = require('posthtml-exp')({
46-
locals: {/* 'path/to/file.(js|json) '*/}
38+
exp({
39+
locals: { myClassName: 'introduction', myName: 'Jeff' }
4740
})
4841
```
49-
```js
50-
exports = module.exports = {/* locals */}
51-
```
52-
```json
53-
{
54-
"name": "PostHTML Exps",
55-
"repo": {
56-
"name": "posthtml-exp",
57-
"url": "https://github.com/michael-ciniawsky/posthtml-exp"
58-
}
59-
}
60-
```
6142

62-
## Paths
63-
Expression and Helper arguments can be expressed with dot notation syntax. The current limit for nesting is set to 3.
64-
```js
65-
{
66-
local: {
67-
key1: {
68-
key2: {
69-
key3: 'PostHtML Expressions'
70-
}
71-
}
72-
}
73-
}
74-
```
43+
And compiled with the following template:
44+
7545
```html
76-
<div>
77-
<h2>{local.one.two.tree}</h2>
46+
<div class="{{ myClassName }}">
47+
My name is {{ myName }}
7848
</div>
7949
```
50+
51+
You would get this as your output:
52+
8053
```html
81-
<div>
82-
<h2>PostHTML Expressions</h2>
54+
<div class="introduction">
55+
My name is Jeff
8356
</div>
8457
```
8558

86-
## Helpers
87-
### Each **{...}**
59+
### Unescaped Locals
60+
61+
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:
62+
8863
```js
89-
{
90-
locals: {
91-
fruits: ['Apple', 'Orange', 'Mango']
92-
}
93-
}
64+
exp({
65+
locals: { strongStatement: '<strong>wow!</strong>' }
66+
})
9467
```
68+
69+
And you rendered it into a tag like this:
70+
9571
```html
96-
<ul>
97-
<li>{...fruits}</li>
98-
</ul>
72+
<p>The fox said, {{ strongStatement }}</p>
9973
```
74+
75+
You would see the following output:
76+
10077
```html
101-
<ul>
102-
<li>Apple</li>
103-
<li>Orange</li>
104-
<li>Mango</li>
105-
</ul>
78+
<p>The fox said, &lt;strong&gt;wow!&lt;strong&gt;</p>
10679
```
10780

108-
### Pipe **{ | }**
109-
```js
110-
{
111-
locals: {
112-
firstname: 'PostHTML',
113-
lastname: 'Expressions'
114-
}
115-
}
81+
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:
82+
83+
```html
84+
<p>The fox said, {{{ strongStatement }}}</p>
11685
```
86+
87+
In this case, your code would render as html:
88+
11789
```html
118-
<h1>{locals|firstname|lastname}</h1>
90+
<p>The fox said, <strong>wow!<strong></p>
11991
```
92+
93+
### Expressions
94+
95+
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:
96+
12097
```html
121-
<h1>PostHTML Expressions</h1>
98+
<p class="{{ env === 'production' ? 'active' : 'hidden' }}">in production!</p>
12299
```
123100

124-
### Partial **{> }**
101+
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:
102+
125103
```js
126-
{
104+
exp({
127105
locals: {
128-
button: './includes/button.html'
106+
isProduction: (env) => {
107+
return env === 'production' ? 'active' : 'hidden'
108+
}
129109
}
130-
}
131-
```
132-
```html
133-
<div>{> button}</div>
110+
})
134111
```
112+
135113
```html
136-
<div>
137-
<button>Click Me!</button>
138-
</div>
114+
<p class="{{ isProduction(env) }}">in production!</p>
139115
```
140116

141-
## Usage
142-
For general usage and build process integration see [PostHTML Docs](https://github.com/posthtml/posthtml#usage)
143-
144-
### Example using Node API
145-
#### Default
146-
```js
147-
'use strict'
117+
### Conditional Logic
148118

149-
const fs = require('fs')
119+
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:
150120

151-
const posthtml = require('posthtml')
152-
153-
const exp = require('posthtml-exp')({
154-
locals: {
155-
id: 'title',
156-
class: 'header',
157-
content: 'PostHTML Exps Default'
158-
}
121+
```js
122+
exp({
123+
locals: { foo: 'foo' }
159124
})
125+
```
160126

161-
let file = fs.readFileSync('./index.html', 'utf-8')
127+
And the following html:
162128

163-
posthtml([ exp ])
164-
.process(file)
165-
.then(result => console.log(result.html))
166-
```
167-
##### Input
168129
```html
169-
<div class={class}>
170-
<h1 id={id}>{content}</h1>
171-
</div>
130+
<if condition="foo === 'bar'">
131+
<p>Foo really is bar! Revolutionary!</p>
132+
</if>
133+
<elseif condition="foo === 'wow'">
134+
<p>Foo is wow, oh man.</p>
135+
</elseif>
136+
<else>
137+
<p>Foo is probably just foo in the end.</p>
138+
</else>
172139
```
173-
##### Output
140+
141+
Your result would be only this:
142+
174143
```html
175-
<div class="header">
176-
<h1 id="title">PostHTML Exps</h1>
177-
</div>
144+
<p>Foo is probably just foo in the end.</p>
178145
```
179-
#### Custom
180-
```js
181-
'use strict'
182146

183-
const fs = require('fs')
147+
Anything in the `condition` attribute is evaluated directly as an expression.
184148

185-
const posthtml = require('posthtml')
149+
It should be noted that this is slightly cleaner-looking if you are using the [SugarML parser](https://github.com/posthtml/sugarml). But then again so is every other part of html.
150+
151+
```sml
152+
if(condition="'foo' === 'bar'")
153+
p Foo really is bar! Revolutionary!
154+
elseif(condition="'foo' === 'wow'")
155+
p Foo is wow, oh man.
156+
else
157+
p Foo is probably just foo in the end.
158+
```
186159

187-
const exp = require('posthtml-exp')({
188-
style: '{{',
160+
#### Loops
161+
162+
You can use the `each` tag to build loops. It works with both arrays and objects. For example:
163+
164+
```js
165+
exp({
189166
locals: {
190-
id: 'title',
191-
class: 'header',
192-
content: 'PostHTML Exps Default'
167+
anArray: ['foo', 'bar'],
168+
anObject: { foo: 'bar' }
193169
}
194170
})
171+
```
172+
173+
```html
174+
<each loop="item, index in anArray">
175+
<p>{{ index }}: {{ item }}</p>
176+
</each>
177+
```
195178

196-
let file = fs.readFileSync('./index.html', 'utf-8')
179+
Output:
197180

198-
posthtml([ exp ])
199-
.process(file)
200-
.then(result => console.log(result.html))
181+
```html
182+
<p>1: foo</p>
183+
<p>2: bar</p>
201184
```
202-
##### Input
185+
186+
And an example using an object:
187+
203188
```html
204-
<div class={{class}}>
205-
<h1 id={{id}}>{{content}}</h1>
206-
</div>
189+
<each loop="key, value in anObject">
190+
<p>{{ key }}: {{ value }}</p>
191+
</each>
207192
```
208-
##### Output
193+
194+
Output:
195+
209196
```html
210-
<div class="header">
211-
<h1 id="title">PostHTML Exps Custom</h1>
212-
</div>
197+
<p>foo: bar</p>
213198
```
199+
200+
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.
201+
202+
So this would also be fine:
203+
204+
```html
205+
<each loop="item in [1,2,3]">
206+
<p>{{ item }}</p>
207+
</each>
208+
```
209+
210+
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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.8.1",
44
"description": "Template Expressions for PostHTML",
55
"engines": {
6-
"node": ">=4"
6+
"node": ">=6"
77
},
88
"main": "index.js",
99
"dependencies": {},

0 commit comments

Comments
 (0)