rein

A fast and easy-to-use template language for every langauge (XML, HTML, SQL, CSS)

Rein.js

One of the fastest and easy-to-use template language for every langauge (XML, HTML, SQL, CSS)

Why the weird name?

Rein is german/dutch for clean, neat and that was exactly the goal of this project, create a clean and neat template language.

What is it?

It's another template langauge! It originally was a template langauge for SQL. but after I ported it to JavaScript, it became suitable for HTML too! and after some tweaking it became one of the fastest template engines there is

When should I use it?

When you need a simple, lightweight and superfast template engine, that can be used server side but also client side

How do I use it

for NodeJS you first need to install it with the following command and then you can go wild!

npm i rein

and for the web you need add Rein(.min).js to your page, (note that compiling a template takes more time then just executing it, f.x. a different template langauge may be faster in compiling the template but is most likely not faster in executing it, so its recommend to compile the templates serverside)

Examples

Server.js

var fs = require('fs');
var rein = require('rein');
var templates = {
    list:rein(fs.readFileSync('./list.rein')),
    friends:rein(fs.readFileSync('./friends.rein'))
};
// createBundle will make a little javascript script 
//  with the compiled templates ready to execute client side 
//  via the ReinBundle function, so if you send jsCode 
//  to your client and execute it there 
//  you can fully use that as described in Client.js 
var jsCode = rein.createBundle(templates);

Client.js

// ReinBundle takes 2 arguments, id and args 
//  the id is from the object which is used to generate 
//  this bundle. 
var html = ReinBundle('list',{ items:["soap","shampoo","wig"] });
// The client side version is very simple  
//  with Rein you build your template 
//  and with Template.parse you (compile and) execute it 
var tpl = Rein("Your are customer <strong>{{nr}}</strong>");
var html = tpl.parse({nr:425});

Template syntax

The template syntax is simple, easy to use and fluid. at least, I tried to make it that way.

variables just work as you expect them to work, you can also execute functions and all the things you would normally do between a ( and )

I'm {{name}} and I'm {{age}} old, I'm a {{gender.name}}, and {{this()}} is my weapon!

if's are structured like {{ expression : MOAR TEMPLATE }}, well that's simple isn't it?!

There is currently no else :( I will work on that!

{{gotKids && doIReallyGotKids():
    I got like {{numKids}} kids! You can have {{ numKids - ((numKids + hisNumKids)/2) }} kids from me if you want?
}}

for loops are almost like if's {[ variable : TEMPLATE INCEPTION ]}, it won't get much harder!

Although there are some things you need to know!

first off we reset the scope of the for to the current object we are looping on, so be aware of that! we also add the following variables to the for scope, I mean, these could be usefull, sometimes, like when you need it.

__i: current index
__amount: length of current array
__first: returns true if this is the first element
__last: returns true if this is the last element

{[arr:
    Im {{__first: the first }} looping: {{this}}
    {{__last: bye bye!}}
]}

Well. that's it! now create your own neat adventures! and be sure to give some (supportive) feedback.

API

Rein(tpl) alias for Rein.build(tpl)
Rein(tpl,vars) alias for Rein.build(tpl).parse(vars)

  • .createBundle(tplobj) returns code for the client with a bundle of template code
  • .build(tpl) build template and return Rein.Template object
  • .Template template object
    • .parse(vars) parses template and returns a string with the parsed template
    • .compile() compiles template to 1 function, which works like .parse(vars)

License

Rein.js is licensed under the MIT License (see LICENSE)