npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

templates.js

v0.3.11

Published

An ultralight and super fast templating framework

Downloads

348

Readme

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 :)