|
1 | 1 | # Expressions for PostHTML |
2 | 2 |
|
3 | | -## Install |
4 | | -```sh |
5 | | -npm i posthtml-exp |
6 | | -``` |
| 3 | +Local variables, expressions, loops, and conditionals in your html |
7 | 4 |
|
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 |
11 | 6 |
|
12 | | -##### JSX: **'{'** |
| 7 | +First, install from npm with `npm i posthtml-exp --save`, then add it as a plugin to your posthtml pipeline: |
13 | 8 |
|
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') |
18 | 13 |
|
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) |
21 | 17 | ``` |
22 | | -##### Blaze: **'@'** |
23 | 18 |
|
24 | | -```html |
25 | | -<div id="@id" class="@name">@content</div> |
26 | | -``` |
| 19 | +## Usage |
27 | 20 |
|
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. |
29 | 22 |
|
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']` | |
35 | 32 |
|
36 | 33 | ### 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 | + |
44 | 37 | ```js |
45 | | -let exps = require('posthtml-exp')({ |
46 | | - locals: {/* 'path/to/file.(js|json) '*/} |
| 38 | +exp({ |
| 39 | + locals: { myClassName: 'introduction', myName: 'Jeff' } |
47 | 40 | }) |
48 | 41 | ``` |
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 | | -``` |
61 | 42 |
|
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 | + |
75 | 45 | ```html |
76 | | -<div> |
77 | | - <h2>{local.one.two.tree}</h2> |
| 46 | +<div class="{{ myClassName }}"> |
| 47 | + My name is {{ myName }} |
78 | 48 | </div> |
79 | 49 | ``` |
| 50 | + |
| 51 | +You would get this as your output: |
| 52 | + |
80 | 53 | ```html |
81 | | -<div> |
82 | | - <h2>PostHTML Expressions</h2> |
| 54 | +<div class="introduction"> |
| 55 | + My name is Jeff |
83 | 56 | </div> |
84 | 57 | ``` |
85 | 58 |
|
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 | + |
88 | 63 | ```js |
89 | | -{ |
90 | | - locals: { |
91 | | - fruits: ['Apple', 'Orange', 'Mango'] |
92 | | - } |
93 | | -} |
| 64 | +exp({ |
| 65 | + locals: { strongStatement: '<strong>wow!</strong>' } |
| 66 | +}) |
94 | 67 | ``` |
| 68 | + |
| 69 | +And you rendered it into a tag like this: |
| 70 | + |
95 | 71 | ```html |
96 | | -<ul> |
97 | | - <li>{...fruits}</li> |
98 | | -</ul> |
| 72 | +<p>The fox said, {{ strongStatement }}</p> |
99 | 73 | ``` |
| 74 | + |
| 75 | +You would see the following output: |
| 76 | + |
100 | 77 | ```html |
101 | | -<ul> |
102 | | - <li>Apple</li> |
103 | | - <li>Orange</li> |
104 | | - <li>Mango</li> |
105 | | -</ul> |
| 78 | +<p>The fox said, <strong>wow!<strong></p> |
106 | 79 | ``` |
107 | 80 |
|
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> |
116 | 85 | ``` |
| 86 | + |
| 87 | +In this case, your code would render as html: |
| 88 | + |
117 | 89 | ```html |
118 | | -<h1>{locals|firstname|lastname}</h1> |
| 90 | +<p>The fox said, <strong>wow!<strong></p> |
119 | 91 | ``` |
| 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 | + |
120 | 97 | ```html |
121 | | -<h1>PostHTML Expressions</h1> |
| 98 | +<p class="{{ env === 'production' ? 'active' : 'hidden' }}">in production!</p> |
122 | 99 | ``` |
123 | 100 |
|
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 | + |
125 | 103 | ```js |
126 | | -{ |
| 104 | +exp({ |
127 | 105 | locals: { |
128 | | - button: './includes/button.html' |
| 106 | + isProduction: (env) => { |
| 107 | + return env === 'production' ? 'active' : 'hidden' |
| 108 | + } |
129 | 109 | } |
130 | | -} |
131 | | -``` |
132 | | -```html |
133 | | -<div>{> button}</div> |
| 110 | +}) |
134 | 111 | ``` |
| 112 | + |
135 | 113 | ```html |
136 | | -<div> |
137 | | - <button>Click Me!</button> |
138 | | -</div> |
| 114 | +<p class="{{ isProduction(env) }}">in production!</p> |
139 | 115 | ``` |
140 | 116 |
|
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 |
148 | 118 |
|
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: |
150 | 120 |
|
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' } |
159 | 124 | }) |
| 125 | +``` |
160 | 126 |
|
161 | | -let file = fs.readFileSync('./index.html', 'utf-8') |
| 127 | +And the following html: |
162 | 128 |
|
163 | | -posthtml([ exp ]) |
164 | | - .process(file) |
165 | | - .then(result => console.log(result.html)) |
166 | | -``` |
167 | | -##### Input |
168 | 129 | ```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> |
172 | 139 | ``` |
173 | | -##### Output |
| 140 | + |
| 141 | +Your result would be only this: |
| 142 | + |
174 | 143 | ```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> |
178 | 145 | ``` |
179 | | -#### Custom |
180 | | -```js |
181 | | -'use strict' |
182 | 146 |
|
183 | | -const fs = require('fs') |
| 147 | +Anything in the `condition` attribute is evaluated directly as an expression. |
184 | 148 |
|
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 | +``` |
186 | 159 |
|
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({ |
189 | 166 | locals: { |
190 | | - id: 'title', |
191 | | - class: 'header', |
192 | | - content: 'PostHTML Exps Default' |
| 167 | + anArray: ['foo', 'bar'], |
| 168 | + anObject: { foo: 'bar' } |
193 | 169 | } |
194 | 170 | }) |
| 171 | +``` |
| 172 | + |
| 173 | +```html |
| 174 | +<each loop="item, index in anArray"> |
| 175 | + <p>{{ index }}: {{ item }}</p> |
| 176 | +</each> |
| 177 | +``` |
195 | 178 |
|
196 | | -let file = fs.readFileSync('./index.html', 'utf-8') |
| 179 | +Output: |
197 | 180 |
|
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> |
201 | 184 | ``` |
202 | | -##### Input |
| 185 | + |
| 186 | +And an example using an object: |
| 187 | + |
203 | 188 | ```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> |
207 | 192 | ``` |
208 | | -##### Output |
| 193 | + |
| 194 | +Output: |
| 195 | + |
209 | 196 | ```html |
210 | | -<div class="header"> |
211 | | - <h1 id="title">PostHTML Exps Custom</h1> |
212 | | -</div> |
| 197 | +<p>foo: bar</p> |
213 | 198 | ``` |
| 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. |
0 commit comments