HTML templating library for JS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mort 864d1d2975 explained why templish exists 4 years ago
test added tests 4 years ago
README.md explained why templish exists 4 years ago
index.js added tests 4 years ago
package.json added tests 4 years ago

README.md

Templish

A templating language for JavaScript, using JavaScript.

Why?

There are other templating languages out there. However, most of them don’t really let you have a lot of logic in your templates. While those languages have their uses, it’s often useful to have some logic in your templates.

Templish isn’t meant to be used like traditional bad PHP code, hvere you have view logic and business logic in the same file with little to no distinction; most people will want to have each template stored in its own file, and have all business logic handled outside of templates, like you would with any other node.js web server. An example of a case where you would want logic in your templates is a list template where you want to alternate the color of the entries. With templish, this could be achieved like this:

<ul>{{
	var str = "";
	rows.forEach(function(row, i) {
		var c = "type"+(i % 2);
		str += "<li class='"+c+"'>"+row+"</li>";
	});
	return str;
}}</ul>

With a templating language which does not allow arbitrary logic, things like that may have to be done in a more complicated way outside of templates.

Installation

Install with NPM:

npm install --save templish

Usage

A simple example:

//First, require the library
var Templish = require("templish");

//Create a templish template
var t = new Templish("<title>{{conf.title}}</title>", ["conf"]);

//Create an environment
var env = {
	conf: {
		title: "Hello, World!"
	}
}

//Execute the template
t.exec(env, function(err, res) {
	console.log(res); //<title>Hello, World!</title>
});

//Execute the template again, this time with a different environment
env = {
	conf: {
		title: "Good Morning, Earth!"
	}
}
t.exec(env, function(err, res) {
	console.log(res); //<title>Good Morning, Earth!</title>
});

We can do more complex things, even with asynchronous functions:

var fs = require("fs");
var Templish = require("templish");

//Create a templish template
var t = new Templish(
	"<title>{{fs.readFile('someFile', 'UTF8', cb)}}</title>",
	["fs"]
);

//Create an environment, this time with the `fs` method to let us access files
var env = {
	fs: fs
}

//Execute the template
t.exec(env, function(err, res) {
	console.log(res); //If we assume the `someFile` file contains "hi, world":
	                  //<title>hi, world</title>
});

We can have multiple statements:

var Templish = require("templish");

//Create a templish template
var t = new Templish("\
	<title>{{ \
		var str = 'Hello World'; \
		return str; \
	}}</title>",
	[]
);

//Execute the template
t.exec({}, function(err, res) {
	console.log(res) //<title>Hello World</title>
});

Notice that with multiple statements, you have to include the return statement yourself.

Of course, in practice, you would probably want your templates to be individual files, and not inline in the script file, not that Templish cares, as long as it receives a string.