1.1.28 • Published 8 years ago

brown v1.1.28

Weekly downloads
14
License
BSD-2-Clause
Repository
github
Last release
8 years ago

Mustache-ish but clean template engine on steroids in 846 gzipped kilobytes

Usage

$ npm install brown

or in the browser:

<script type='text/javascript' src='brown.min.js'></script>

Simple

brown.render( "hello {{foo.bar}}", { 
  foo: {
    bar: "world" 
  } 
});

outputs:

hello world

Functions

Create a fullfledged template engine by adding functions:

brown.encode = function(key,type) {
  var html = this[key] || '';
  return  type == "html" ?
          String(html).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;') :
          html
};

brown.render( 'a href="{{href}}" onclick="{{encode:label:html}}"': "{{label}}", {
  href="/", 
  label:"my \"label\""} 
});

outputs:

<div>
  <ul>
    <li>
      <a href="/" onclick="my &quot;label&quot;">my "label"</a>
    </li>
  </ul>
</div>

If / Else / Looping

Need more? See brown-ext-basic for if/foreach/filter/loop-functionality.

Generate xml/html from jsonportable JADE

ϐrown can be monkeypatched, to automatically produce xml-trees from json (like JADE), see this coffeescript example:

json = 
  ul: 
    li: 'a href="{{foo}}": "Click me"

brown.render json,{ foo: "/" }

outputs:

<ul>
  <li>
    <a href="/">Click me </a>
  </li>
</ul>

How? Simple, by just monkeypatching ϐrown with json-dsl. See coffeescript / JS examples.

Commandline util

Use as a commandline generator (install using npm install -g ) :

  $ brown
    Usage: brown <string|file> [jsonstring|jsonfile]

  examples:
          $ brown 'foo {{foo}}' '{"foo":"world"}'
          $ brown foo.html data.json

Goals / Philosophy

  • lightweight and extendable
  • monkeypatchable syntax
  • mustache without the noise and weight (mustache is a whopping 97k and pretty slow)

I got a lot of projects using mustache. The mustache syntax can easily get really noisy, and annoying because of absense of function arguments. Brown is a way to slowly abandon the noisy syntax, and replace it with readable functions.

Hence the brown-ext-basic extension to easily convert it to brown syntax:

mustachebrown
array iteration{{#array}} {{.}} {{/array}{{foreach:items:itemtemplate:"no items found"}}
if/else{{#foo}}{{foo}}{{/foo}}{{^foo}} no foo {{/foo}}{{if:foo:foo:"no foo"}}
object iterationn/a{{foreach:items:itemtemplate:"no items found"}}
global functionsn/abrown.foo = (arg1,arg2) -> return arg1+arg2
partial{{ > templatename }}{{include:templatename}}
chaining functionsn/a{{foo:htmlencode:uppercase}}template functions suite
filesize97k846 bytes (gzipped)

With ϐrown as a fundament you can literally extend and overload anything.

Extentions

Roadmap

  • stability and peace
1.1.28

8 years ago

1.1.27

8 years ago

1.1.26

8 years ago

1.1.25

8 years ago

1.1.24

8 years ago

1.1.23

8 years ago

1.1.22

8 years ago

1.1.21

8 years ago

1.1.2

8 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.2

9 years ago