Code Monkey home page Code Monkey logo

mustache.js's Introduction

mustache.js with added dot notation and filters

For the original mustache.js implementation visit http://github.com/janl/mustache.js.

Why use this version?

In my opinion the original moustache.js implementation lacks two features: convenient access to value's properties (the dot notation) and value filters (which are available for instance in Django template engine or Smarty).

And I am not convinced about reasons for not doing this, which were discussed in http://github.com/defunkt/mustache/issues/6.

The dot notation

Tag in dot notation consists of a tag's name and optionally its properties separated by dots, followed by filters. Filters are separated from the properties and each other by vertical line characters (|). Filters are essentially one-parameter functions declared in the global scope.

Long story short, tag name name1.name2.name3 | filter1 | filter2 will be processed the following way:

  1. search in template context for value named name1
  2. try to access name2 property in the value from (1), if the property is not found return an empty string
  3. do the same as above but search for name3 property in value from (2)
  4. apply filter filter1 to the value from (3)
  5. apply filter filter2 to the value from (4)
  6. return the value from (5), so it may be used in a template

Filters are only applied if value specified by names is found within context.

Example

For example:

// declare filter
function uc(str) {
  return ('' + str).toUpperCase();
}

var view = {
  user: {
    first_name: 'John',
    last_name: 'Smith'
  }
}

var template = "Hello {{ user.first_name }} {{ user.last_name | uc }}!";

var html = Mustache.to_html(template, view);

After execution the above code the value of html variable will be:

Hello John SMITH!

About the modifications

This version differs from the original one only in the implementation of Renderer.prototype.find function.

mustache.js's People

Contributors

janl avatar langalex avatar jhs avatar defunkt avatar xemlock avatar agibralter avatar jchris avatar jpf avatar pandark avatar davisp avatar maritz avatar jacksontian avatar thegrandpoobah avatar vackraord avatar endor avatar nrstott avatar

Watchers

James Cloos avatar  avatar

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.