Giter VIP home page Giter VIP logo

node-jstemplate's Introduction

JSTemplate - A pure Javascript templating engine for node.js

JSTemplate is a simple and pure-Javascript templating engine for node.js

Installing

npm install jstemplate

A basic template

<%
   var
        title = "Mr";
-%>
Hello <%= title %> <%= args.sir -%>

Processing a template

var
    JSTemplate = require('jstemplate'),
	jst = new JSTemplate({viewDir: 'views'});

jst.process("basic.jst",{sir:process.env.LOGNAME},function(err,output){
    if ( err )
        throw err;
    console.log(output);
});

Settings

The JSTemplate object supports the following settings:

  • viewDir : The directory containing the templates (or views)

  • statInterval : The interval of time (in ms) that a template file is stat()'ed and the modify time is watched to see if the template file was changed. Default to 5000

  • useLayout : Defines if the layout.jst will be used to encapsulate the result of the processed template. Defaults to: true

The syntax

  • <% CODE %> : Everything between <% and %> will be evaluated as javascript. Example: <% var x = 666; %>

  • <%= VARIABLE|STATEMENT %> or <%? VARIABLE|STATEMENT %> : Prints the result of a variable or statement

  • <& file.jst, {a:1,b:2} | p &> : Includes template file.jst and pass {a:1,b:2} as arguments (args variable); Optional | p flag can be passed to just pre-process the template so it can be included with jst.include()

Spaces and indentation

Optionally all tags can finish with a - (example <% args.somevar -%>) telling that the next return's should be discarded;

Accessible variables

These variable are accessible by the template code:

  • args : The object containing the template arguments

  • jst : The global JSTemplate object

API

The JSTemplate object exposes the following API:

  • print(str) : Prints a string

  • dump(obj) : Prints the dump of an object

  • include(file,args) : Includes a template and call it with the specified arguments

  • next() : Calls the next template on the execution stack (ninjas only)

  • html(str) : Converts some data on something that is safe to use on a HTML page

  • url(str) : Converts some data on something linkable (escape)

  • global variable : A place where you can store some global data

Questions and suggestions

Mail me.

node-jstemplate's People

Contributors

davidfoliveira avatar erosdavid avatar

Stargazers

Stuart Macgregor 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.