saxt
A server-side template engine based on sax-js
Last updated 2 years ago by heineiuo .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install saxt 
SYNC missed versions from official npm registry.

saxt

SAX T(emplate)

A server-side template engine based on sax-js

Install

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:

const saxt = require('saxt')
const view = { foo: "bar" }
const stream = saxt(`<div children={foo}></div>`, view, { /* some options */ })

let result = ''
stream.on('data', (data) => {
  result += data
  // or do stuff like `res.write(data)`
})

stream.on('end', () => {
  assert(result === `<div>bar</div>`)
  // or do stuff like `res.end()`
})

Example

const saxt = require('saxt')
const view = {
  post: `<div>
          <p>hello world</p>
        </div>`,

  charset: 'utf8'
}

// 1. children example
saxt(`
  <div id="post" children={post} />
`, view)

// result >>>
<div id="post">
<div>
  <p>hello world</p>
</div>
</div>



// 2. attr example
saxt(`
  <meta charset={charset} />
`, view)

// result >>>>
<meta charset="utf8"></meta>

License

MIT License

Current Tags

  • 0.0.7                                ...           latest (2 years ago)

7 Versions

  • 0.0.7                                ...           2 years ago
  • 0.0.6                                ...           2 years ago
  • 0.0.5                                ...           2 years ago
  • 0.0.4                                ...           2 years ago
  • 0.0.3                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 3
Last Day 0
Last Week 1
Last Month 9
Dependencies (2)
Dev Dependencies (1)

Copyright 2014 - 2017 © taobao.org |