GithubHelp home page GithubHelp logo

michalkozminski / node-template Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gjritter/template.node.js

1.0 0.0 0.0 91 KB

A page templating system for node.js based on John Resig's microtemplate function.

node-template's Introduction

node-template

A templating system for node.js based on John Resig's JavaScript Micro-Templating.

Unlike that version (and Chad Etzel's) this one preserves whitespace allowing you to use blocks of (and inline) javascript and css in the page. It also has no problem with single quotes.

Templates

Templates are just files with special <% %> tags (like PHP or Ruby tags) which will be replaced with passed-in data. Templates can also contain javascript code to be expanded.

Example Template

<html>
<body>
 Hello, <%=name>.
</body>
</html>

Example Template with javascript

<html>
<body>
<% for (var i = 0; i < arr.length; i++) { %>
    The value of arr[<%=i%>] is <%=arr[i]%> <br/>
<% } %>
</body>
</html>

Usage

There are plenty of ways to use this:

Simple Usage with a String

sys.puts(template.create("Hello <%=word%>!", {word:"world"}));

Save the Template for Later

var t = template.create("Hello <%=word%>!");
sys.puts(t({word:"planet"}));

Callbacks

template.create("Hello <%=word%>!", {word:kitty"}, function(t) {
    sys.puts(t);
});

Todo

  • Callbacks with non-processed templates.
  • Cache by filename (not string).
  • Auto-cache, reloads the cached file if a change is detected in the file. (This can be turned off on production.)

Other Info

You do not need to restart the node app to see the changes take effect. The reason is because it caches strings and not files. This has the downside of filling memory with useless templates until you restart the app. This should be fixed in a future version together with auto-cache.

node-template's People

Contributors

gjritter avatar greut avatar jazzychad avatar nmondon avatar

Stargazers

 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.