npm

Need private packages and team management tools?Check out npm Orgs. »

mustache-cli

2.3.0 • Public • Published

mustache-cli

Description

Mustache's CLI interface.

Usage

$ npm install mustache-cli --global
$ mustache-cli -h

./tpl/layout.mustache

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>{{title}}</title>
  </head>
  <body>
    <div>
      {{>tpl}}
    </div>
  </body>
</html>

./tpl/page.mustache

<h1>{{title}}</h1>
{{>content}}

./conf/index.json

{
    "__root": "layout.mustache",
    "__tpl": "page.mustache",
    "_content": "<strong>Hello World</strong>",
    "title": "Home"
}

./conf/multi.js

const output = require('mustache-cli').output
 
module.exports = function(opts){
  return {
    __file: 'multi/index.html',
    __root: 'layout.mustache',
    _tpl: '{{{html}}}',
    title: 'Multi',
    html: function(){
      const page1 = output({
        __root: 'page.mustache',
        _content: '<p>page1</p>',
        title: this.title,
      }, opts)
      const page2 = output({
        __root: 'page.mustache',
        _content: '<p>page2</p>',
        title: this.title,
      }, opts)
      return page1 + page2
    }
  }
}
$ mustache-cli -p --color ./

./out/index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Home</title>
  </head>
  <body>
    <div>
      <h1>Home</h1>
      <strong>Hello World</strong>
    </div>
  </body>
</html>

./out/multi/index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Multi</title>
  </head>
  <body>
    <div>
      <h1>Multi</h1>
      <p>page1</p>
      <h1>Multi</h1>
      <p>page2</p>
    </div>
  </body>
</html>

Node

import express from 'express'
import {renderFile} from 'mustache-cli/lib/express'
 
const app = express()
 
app.set('views', 'src/mustache/tpl')
app.set('view engine', 'mustache')
app.engine('mustache', renderFile({
  baseDir: 'src/mustache',
}))
 
app.use((req, res) => {
  res.render('layout', {
    title: 'Home',
  })
})
 
app.listen(3000)

API

output(config[, options])

options:
  • baseDir: (Default: .)
  • confDir: (Default: ./conf)
  • tplDir: (Default: ./tpl)
  • outDir: (Default: ./out)
  • rootTpl: (Default: __root)
  • outFile: (Default: __file, Since: 2.2.0+)
  • forceMinify: (Default: __minify, Since: 2.3.0+)
  • forcePretty: (Default: __pretty, Since: 2.3.0+)
  • tplPrefix: (Default: __)
  • partialPrefix: (Default: _)
  • ext: (Default: html)
  • render: (Default: mustache.render)
  • print
  • onError
  • color: (Default: false)
  • minify: (Default: false)
  • pretty: (Default: false)
  • watch: (Default: false)
  • config

setGlobalData(data)

getGlobalData

License

MIT

install

npm i mustache-cli

Downloadsweekly downloads

40

version

2.3.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability