https://github.com/heineiuo/saxt
A streaming template engine based on sax-js, primary for server side render.
https://github.com/heineiuo/saxt
nodejs stream-api template-engine
Last synced: 18 days ago
JSON representation
A streaming template engine based on sax-js, primary for server side render.
- Host: GitHub
- URL: https://github.com/heineiuo/saxt
- Owner: heineiuo
- Created: 2018-03-07T09:16:53.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-09T06:58:39.000Z (almost 8 years ago)
- Last Synced: 2025-01-19T10:29:04.479Z (12 months ago)
- Topics: nodejs, stream-api, template-engine
- Language: HTML
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# saxt
SAX T(emplate)
A server-side template engine based on sax-js
## Install
```bash
npm i saxt
```
## Usage
saxt has a very simple syntax.
All `tagName` is html tag. The `attr` can be static value or wrapped with `{}`, then it will bind view props, and one special attr is `children`. If attr is `children`, the attrValue will be passed to the children element.
**Important**: If the bind value is not a string type, it will be stringify by `JSON.stringify` and encoded by `encodeURIComponent`
The `saxt()` will return a readable stream, like:
```jsx
const saxt = require('saxt')
const view = { foo: "bar" }
const stream = saxt(`
`, view, { /* some options */ })
let result = ''
stream.on('data', (data) => {
result += data
// or do stuff like `res.write(data)`
})
stream.on('end', () => {
assert(result === `
bar`)
// or do stuff like `res.end()`
})
```
## Example
```jsx
const saxt = require('saxt')
const view = {
post: `
hello world
`,
charset: 'utf8'
}
// 1. children example
saxt(`
`, view)
// result >>>
hello world
// 2. attr example
saxt(`
`, view)
// result >>>>
```
## License
MIT License