0.3.11 • Published 7 years ago

templates.js v0.3.11

Weekly downloads
42
License
MIT
Repository
github
Last release
7 years ago

Build Status Code Climate Dependency Status

templates.js is an ultralight (1.72kb minified and gzipped) and super fast templating framework for JavaScript and node.js.

It has express support out-of-the-box.

API

Parse templates by passing in a template string and an object representing the data to be parsed.

var parsed = templates.parse(template, data);

templates.js client-side

<html>
<head>
	<script type="text/javascript" src="path/to/templates.js"></script>
</head>
<body>
	<div id="template">
		<p>{quote.text}</p>
		<strong>{quote.author}</strong>
	</div>

	<script type="text/javascript">
		var el = document.getElementById('template');

		el.innerHTML = templates.parse(el.innerHTML, {
			quote: {
				text: "Life is really simple, but we insist on making it complicated.",
				author: "Confucius"
			}
		});
	</script>
</body>
</html>

templates.js and express

var express = require('express'),
	app = express(),
	templates = require('templates.js'),
	data = {};

app.configure(function() {
	app.engine('tpl', templates.__express);
	app.set('view engine', 'tpl');
	app.set('views', 'path/to/templates');
});

app.render('myview', data, function(err, html) {
	console.log(html);
});

app.get('/myview', function(res, req, next) {
	res.render('myview', data);
});

Template Syntax

Sample data, see test cases for more:

{
	"animals": [
		{
			"name": "Cat",
			"species": "Felis silvestris catus",
			"isHuman": false,
		},
		{
			"name": "Dog",
			"species": "Canis lupus familiaris",
			"isHuman": false,
		},
		{
			"name": "Human",
			"species": "Homo sapiens",
			"isHuman": true
		}
	],
	"package": {
		"name": "templates.js",
		"author": "psychobunny",
		"url": "http://www.github.com/psychobunny/templates.js"
	},
	"website": "http://burnaftercompiling.com",
	"sayHello": true
}

Simple key/value

My blog URL is {website}. The URL for this library is {package.url}

Conditionals

<!-- IF sayHello -->
  Hello world!
<!-- ENDIF sayHello -->

<!-- IF !somethingFalse -->
  somethingFalse doesn't exist
<!-- ENDIF !somethingFalse -->

Arrays

Repeat blocks of HTML with an array. The two helpers @first and @last are also available as conditionals within an array.

<!-- BEGIN animals -->
  {animals.name} is from the species {animals.species}.
  <!-- IF !animals.isHuman -->
    - This could be a pet.
  <!-- ENDIF !animals.isHuman -->
<!-- END animals -->

prints out:

Cat is from the species Felis silvestris catus.
- This could be a pet.
Dog is from the Canis lupus familiaris.
- This could be a pet.
Human is from the species Homo sapiens.

Helpers

Helpers are JavaScript methods for advanced logic in templates. This example shows a really simple example of a function called print_is_human which will render text depending on the current block's data.

templates.registerHelper('print_is_human', function(data, iterator, numblocks) {
	return (data.isHuman) ? "Is human" : "Isn't human";
});

<!-- BEGIN animals -->
{function.print_is_human}
<!-- END animals -->

prints out:

Isn't human
Isn't human
Is human

Installation

npm install templates.js

Testing

npm install
npm test

Projects using templates.js

NodeBB

Add yours here by submitting a PR :)

0.3.11

7 years ago

0.3.10

7 years ago

0.3.9

7 years ago

0.3.8

7 years ago

0.3.7

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.16

9 years ago

0.2.15

9 years ago

0.2.14

9 years ago

0.2.13

9 years ago

0.2.12

9 years ago

0.2.11

9 years ago

0.2.10

9 years ago

0.2.9

9 years ago

0.2.8

9 years ago

0.2.7

9 years ago

0.2.6

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.33

9 years ago

0.1.32

9 years ago

0.1.31

9 years ago

0.1.30

9 years ago

0.1.29

9 years ago

0.1.28

9 years ago

0.1.27

9 years ago

0.1.26

9 years ago

0.1.25

9 years ago

0.1.24

9 years ago

0.1.23

9 years ago

0.1.22

9 years ago

0.1.21

9 years ago

0.1.20

9 years ago

0.1.19

9 years ago

0.1.18

9 years ago

0.1.17

9 years ago

0.1.16

9 years ago

0.1.15

9 years ago

0.1.14

9 years ago

0.1.13

9 years ago

0.1.12

9 years ago

0.1.11

9 years ago

0.1.10

9 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.15

10 years ago

0.0.14

10 years ago

0.0.13

10 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago