HTML templating library for JS
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
mort 9a1eb3038e added git repo to package.json vor 8 Jahren
README.md added README file vor 8 Jahren
index.js added git repo to package.json vor 8 Jahren
package.json added git repo to package.json vor 8 Jahren

README.md

Templish

A templating language for JavaScript, using JavaScript.

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.