LICENSE-minstache.txt 1.6 KB
Newer Older
wu-sheng's avatar
wu-sheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

# minstache

  Mini mustache template engine.

## Installation

    $ npm install minstache
    $ component install visionmedia/minstache

## minstache(1)

  The `minstache(1)` executable can compile a file to a valid
  stand-alone commonjs module for you, there's no need to have minstache
  as a dependency:

  hello.mustache:

```
Hello {{name}}! {{^authenticated}}<a href="/login">login</a>{{/authenticated}}
```

  convert it:

```
$ minstache < hello.mustache > hello.js
```

  hello.js:

```js
module.exports = function anonymous(obj) {

  function escape(html) {
    return String(html)
      .replace(/&/g, '&amp;')
      .replace(/"/g, '&quot;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;');
  };

  function section(obj, prop, negate, str) {
    var val = obj[prop];
    if ('function' == typeof val) return val.call(obj, str);
    if (negate) val = !val;
    if (val) return str;
    return '';
  };

  return "Hello " + escape(obj.name) + "! " + section(obj, "authenticated", true, "<a href=\"/login\">login</a>") + "\n"
}
```

## API

### minstache(string, [obj])

  Compile and render the given mustache `string` with optional context `obj`.

### minstache.compile(string)

  Compile the mustache `string` to a stand-alone `Function` accepting a context `obj`.

## Divergence

  Partials are not supported, this lib is meant to be a small template engine solution for stand-alone [component](http://github.com/component) templates. If your template takes "partials" then pass other rendered strings to it. If you need a full-blown mustache solution Hogan.js is still great.

  Minstache uses `{{!name}}` for unescaped properties.

## License

  MIT