Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gimenete/emmental

Template engine for Nodejs
https://github.com/gimenete/emmental

Last synced: about 1 month ago
JSON representation

Template engine for Nodejs

Awesome Lists containing this project

README

        

# Goal

Emmental is a simple server-side template engine with one goal in mind: to be able to use the same file as a mockup and as a template as well. Usually designers create HTML mockups and then developers copy those files to another directory and start to insert things like ${} <%=%>,… breaking the HTML syntax, and making it impossible to be edited for a designer.

The idea of emmental is to create a template engine that doesn't break the HTML. So the designer can keep using their tools, and the file is always capable to be rendered by a web browser.

# How?

The emmental purpose is to use non-standard HTML attributes that the web browser ignores but the template engine interprets.

# Example

For example you have the following markup:

There are 3 fruits in the basket:


  • fruit1

  • fruit2

  • fruit3

This is dummy text. And now you would like to fill this HTML with real data (maybe fetched from a database or any other data source). For simplicity think that we have this data:

var fruits = ['apple', 'pear', 'banana', 'pineapple']

With emmental is easy to convert the previous mackup in a real template capable to be filled with data.

## The simplest attribute: "put"

Use "put" to change the text contained within an HTML element. For example:

There are 3 fruits in the baket:

As you can see you can use any javascript expression in the "put" attribute.

## Loops: "loop" and "as" attributes

To loop over a collection you will use the "loop" and "as" attributes. Example:


  • foo

  • bar

  • baz

With the "loop" attribute you define which collection will be iterated. With the "as" attribute you define the variable name to access each item in the collection while iterating.

The previous code will generate just four \

  • foo
  • elements which is not very useful. The complete example could be:


    • foo

    • bar

    • baz

    Now you want to remove the last two

  • elements because they are dummy data. You can do the following


    • foo

    • bar

    • baz

    ## Conditional rendering

    You can use the "if" attribute to do conditional rendering. The expression result is a falsy value then the element is removed. For example:

    There are no fruits.

    There is no "else" support and not is planned.

    ## Attribute override

    You can override attributes with "put-*". For example, if you have this markup:

    more info

    You can override the href attribute with "put-href". Example:

    more info

    Again, you can use any javascript expression.

    # Usage

    Usage:

    var emmental = require('emmental')
    var html = fs.readFileSync('./example.html', 'utf8')
    var out = emmental.processTemplate(html, data, function(err, out) {
    console.log(out) // "out" is a string with the HTML output
    })

    You can find a complete example inside the "examples" folder in this repository.

    # LICENSE

    Copyright (c) 2011 Alberto Gimeno Brieba

    Permission is hereby granted, free of charge, to any
    person obtaining a copy of this software and associated
    documentation files (the "Software"), to deal in the
    Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the
    Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice
    shall be included in all copies or substantial portions of
    the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
    KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
    PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
    OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.