https://github.com/reshape/expressions
plugin that adds the ability to use expressions, conditionals, and loops
https://github.com/reshape/expressions
Last synced: about 1 year ago
JSON representation
plugin that adds the ability to use expressions, conditionals, and loops
- Host: GitHub
- URL: https://github.com/reshape/expressions
- Owner: reshape
- License: mit
- Created: 2016-08-09T16:33:40.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2019-02-05T12:37:31.000Z (over 7 years ago)
- Last Synced: 2025-06-10T18:01:36.376Z (about 1 year ago)
- Language: JavaScript
- Size: 436 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Contributing: contributing.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Reshape Expressions
[](https://npmjs.com/package/reshape-expressions)
[](https://travis-ci.org/reshape/expressions?branch=master)
[](https://david-dm.org/reshape/expressions)
[](https://coveralls.io/r/reshape/expressions?branch=master)
Local variables, expressions, loops, and conditionals in your html.
## Installation
First, install from npm with `npm i reshape-exp --save`, then add it as a plugin to your reshape pipeline:
```js
const reshape = require('reshape')
const exp = require('reshape-expressions')
const {readFileSync} = require('fs')
reshape({ plugins: exp() })
.process(readFileSync('exampleFile.html', 'utf8'))
.then((res) => {
return res.output({ foo: 'bar' }) // => your html
})
```
## Usage
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.
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 `expressions` plugin are shown below:
| Option | Description | Default |
| ------ | ----------- | ------- |
| **delimiters** | Array containing beginning and ending delimiters for escaped locals. | `['{{', '}}']` |
| **unescapeDelimiters** | Array containing beginning and ending delimiters for inserting unescaped locals. | `['{{{', '}}}']` |
| **conditionalTags** | Array containing names for tags used for standard `if`/`else if`/`else` logic | `['if', 'elseif', 'else']` |
| **loopTags** | Array containing names for standard `for` loop logic | `['each']` |
### Locals
You can inject locals into any piece of content in your html templates, other than overwriting tag names. For example, if you had the following template:
```html
My name is {{ myName }}
```
And passed it through reshape like this:
```js
reshape({ plugins: exp() })
.process(template)
.then((res) => res.output({ myClassName: 'introduction', myName: 'Marlo' })))
```
You would get this as your output:
```html
My name is Marlo
```
### Unescaped Locals
By default, special characters will be escaped so that they show up as text, rather than html code. For example, the following template:
```html
The fox said, {{ strongStatement }}
```
Called as such:
```js
reshape({ plugins: exp() })
.process(template)
.then((res) => res.output({ strongStatement: 'wow!' }))
```
You would see the following output:
```html
The fox said, <strong>wow!<strong>
```
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:
```html
The fox said, {{{ strongStatement }}}
```
In this case, your code would render as html:
```html
The fox said, wow!
```
### Expressions
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:
```html
in production!
```
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:
```html
in production!
```
```js
reshape({ plugins: exp() })
.process(template)
.then((res) => {
return res.output({
production: true
isProduction: (env) => {
return env === 'production' ? 'active' : 'hidden'
}
})
})
```
```html
in production!
```
### Conditional Logic
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 template:
```html
Foo really is bar! Revolutionary!
Foo is wow, oh man.
Foo is probably just foo in the end.
```
And the following config:
```js
reshape({ plugins: exp() })
.process(template)
.then((res) => res.output({ foo: 'foo' }))
```
Your result would be only this:
```html
Foo is probably just foo in the end.
```
Anything in the `condition` attribute is evaluated directly as an expression.
It should be noted that this is slightly cleaner-looking if you are using the [SugarML parser](https://github.com/reshape/sugarml). But then again so is every other part of html.
```sml
if(condition="foo === 'bar'")
p Foo really is bar! Revolutionary!
elseif(condition="foo === 'wow'")
p Foo is wow, oh man.
else
p Foo is probably just foo in the end.
```
### Loops
You can use the `each` tag to build loops. It works with both arrays and objects. For example:
Input:
```html
{{ index }}: {{ item }}
```
Config:
```js
reshape({ plugins: exp() })
.process(template)
.then((res) => {
return res.output({
anArray: ['foo', 'bar'],
anObject: { foo: 'bar' }
})
})
```
Output:
```html
1: foo
2: bar
```
And an example using an object (note that it uses "in" rather than "of", in the same way that this would be handled with javascript natively):
```html
{{ key }}: {{ value }}
```
Output:
```html
foo: bar
```
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.
So this would also be fine:
```html
{{ item }}
```
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.
### License & Contributing
- Licensed under [MIT](LICENSE)
- See [guidelines for contribution](CONTRIBUTING.md)