Code Monkey home page Code Monkey logo

templater.js's Introduction

templater.js

Travis Build GitHub License

Dead-simple JavaScript templating with a tiny1 footprint. View the website for extended details & documentation.

Inspired by http://jsforallof.us/2014/12/01/the-anatomy-of-a-simple-templating-engine/ and http://krasimirtsonev.com/blog/article/Javascript-template-engine-in-just-20-line.

1 < 1KB minified + gzipped.

Quick Start

  1. Install the script

    The script can be installed with Bower, npm, or by grabbing the latest release from GitHub.

    bower install templaterjs # For Bower
    npm install templater.js # For npm
  2. Load in the script

    If in a browser environment, with a <script> tag

    <script src="path/to/templater.min.js"></script>

    Or, if in a Node.js environment with a require()

    var templater = require("templater.js");
  3. Create the template

    <!-- by giving it a non-standard `type` the browser will ignore it -->
    <script id="template" type="text/template">
    <h1>Hi, I'm {{name}}.</h1>
    </script>
  4. Use template and render it against some data

    var template = templater(document.getElementById("template").innerHTML),
        context = {
          name: "Paul"
        };
    
    console.log(template(context)); // <h1>Hi, I'm Paul.</h1>

Documentation

For further documentation and resources, visit the templater.js website.

Contributing

If you'd like to fork the repo and submit pull request, please feel free.

Check out CONTRIBUTING.md for specifics.

templater.js's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

templater.js's Issues

Support for defaults

If would be nice if a default value could be specified in the template string, such that it would be used if the object containing the replacement values was missing some, they were undefined, etc.

For example:

const data = {
  first_name: null,
  email: '[email protected]'
};

const template = templater('Welcome, {{ first_name | "visitor" }}!');

console.log(template(data));  // "Welcome, visitor!"

Loops

What's about loop support in templates?

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.