An open API service indexing awesome lists of open source software.

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.

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