Git Product home page Git Product logo

mustache-pro's Introduction

Mustache.js+

Make mustache.js be easier to use by developers.

Supports

  • array index: first/last/index/middle of Array
  • if {{#if(a==1||b==3&&c==3)}} blabla {{/if(a==1||b==3&&c==3)}}
  • if {{#if(a!=1)}} blabla {{/if(a!=1)}}
  • filter {{value | filter1 | filter2(3)}}
  • render {{render_value}} (if register render)
  • include {{#include-sub-tmpl}}
  • define sub tmpl {{#sub-tmpl-id}}{{/sub-tmpl-id}}

API

  • Mustache.registerRender(obj)
  • Mustache.registerFilter(obj)
  • Mustache.to_html(tmpl, data)

Demo

if

tmpl

{{#if(a==1&&b==2||c!=3)}}123{{/if(a==1&&b==2||c!=3)}}
{{#if(a==1)}}a is 1{{/if(a==1)}}

data

{
  a: 1,
  b: 2,
  c: 4
}

==>

123
a is 1

array_index

tmpl

{{#list}}
	{{__index__}}({{a}})
	{{^__last__}}、{{/__last__}}
{{/list}}

data

{
  list: [{
     a: 'value1'
  }, {
     a: 'value2'
  }]
}

==>

0(value1)、1(value2)

renderer

register renderer:

var mapper = {
  "a": 'this is A',
  "b": 'this is B'
};

Mustache.registerRenderer({
  list: {
    desc: function() {
      return mapper[this.name]
    }
  }
});

tmpl:

{{#list}}
  {{list_desc}}'
{{/list}}

data:

{
  list: [{
    name: 'a'
  }, {
    name: 'b'
  }]
}

==>

this is A
this is B

filter

register filter:

var mapper = {
  "a": 'this is A',
  "b": 'this is B'
};

Mustache.registerFilter({
  desc: function(name) {
    return mapper[name]
  },
  omg: function(value) {
    return 'oh my god! ' + value
  }
}
});

tmpl:

{{#list}}
  {{name | desc | omg}}'
{{/list}}

data:

{
  list: [{
    name: 'a'
  }, {
    name: 'b'
  }]
}

==>

oh my god! this is A
oh my god! this is B

sub tmpl

tmpl:

{{#list}}
    {{#if(a==1)}}
        {{#include-list}}
    {{/if(a==1)}}
{{/list}}

{{#sub-tmpl-list}}
    {{a}}
{{/sub-tmpl-list}}

data:

{
  list: [{
    a: 1
  }]
}

==>

1

more...

in /test/cases dir

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.