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 🙏

© 2025 – Pkg Stats / Ryan Hefner

jstemplate-jonijnm

v1.3.4

Published

Simple JavaScript template engine

Downloads

5

Readme

jstemplate

Simple JavaScript template engine

Allowed statements: if, for, while, else, else if Additional statements: elseif, foreach {array|obj} as {var_name}

Full example

template.tpl

<h1>{name.toUpperCase()}</h1>
<div>
	My age is: <span>{age}</span>
	{if skills}
		{*I'm a comment*}
		<div>
			Skills: <span>{skills.length}</span>
		</div>
		<ul>
			{foreach skills as skill}
				<li>{skill}</li>
			{/foreach}
		</ul>
	{else}
		<div>No skills</div>
	{/if}

	<div>
		More data
	</div>
	<ul>
	{foreach moreData as value}
		<li>{_key}: {value}</li> {*_key is a special var, you can use _i too*}
	{/foreach}
	</ul>
</div>
<div style="color: {color}">Now: {(new Date()).toString()}</div>

index.html

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="../src/jstemplate.js"></script>
<script src="test.js"></script>

<div id="user"></div>

test.js

$.ajax({ //get html template
	url: 'template.tpl',
	dataType: 'text' //plain text format!
}).done(function(html) {
	'use strict';

	var template = jstemplate.parse(html); //get template renderer
	var user = { //define user
		name: 'Joni',
		color: 'red',
		age: 25,
		skills: [
			'JavaScript', 'HTML', 'CSS'
		],
		moreData: {
			param1: 'value1',
			param2: 'value2'
		}
	};

	var output = template.rende(user); //get html output
	$('#user').html(output); //show output
});

More examples

For

<ul>
	{for var i = 0; i < 6; i++}
		{if i % 2 != 0}
			{continue}
		{/if}
		<li>{i}</li>
	{/for}
</ul>

While

<ul>
	{var i=0}
	{maxShow = 3}
	{while i <= 5}
		<li>{i}</li>
		{if i+1 == maxShow}
			{break}
		{/if}
		{i++}
	{/while}
</ul>

Functions

<script>
var plus = function(n1, n2) {
	return n1 + n2;
};
</script>

<div>1+2 = {plus(1, 2)} </div>
<div>2+2 = {plus(2, 2)} </div>
<div>3+2 = {plus(3, 2)} </div>

Do not declare functions directly, store them in vars

//BAD CODE
function plus(n1, n2) {
	return n1 + n2;
}

Force return value

jstemplate tries to know if code should be displayed. You can force it.

{var i=0}
{i++} {*i=1 and the value 1 is not displayed*}
{=i++} {*Forced return, i=2 and the value 1 is displayed*}

You can also force code to be not displayed.

<script>
var miFunc = function() {
	console.log('Testing');
};
</script>

{miFunc()} {*Will show 'undefined'*}
{#miFunc()} {*Not displayed*}

Force return raw value

jstemplate encode the values, you can force it to use raw values

{var html = '<b>Hello!</b>'}
{html} {*Ouput: &lt;b&gt;Hello!&lt;/b&gt;*}
{.html} {*Ouput: <b>Hello!</b>*}

self

{foreach self as number}
	{number}
{/foreach}
var tpl = jstemplate.parse(html);
var output = tpl.rende([5,7,8,10]);

Debug

You can debug code simply adding a {debugger}, also when an error occurs the code created is in the Error Object

<ul>
{debugger} {*Debug here!*}
{foreach moreData as value}
	<li>{_key}: {value}</li>
{/foreach}
</ul>
try {
	var template = jstemplate.parse(html);
	var output = template.rende(user); //get html output
	$('#user').html(output); //show output
}
catch(e) {
	//if there is an error in jstemplate, e.source has the code to eval
	console.error(e);
	if (e.source) {
		console.log(e.source);
	}
	throw e;
}

Requirejs

define(function(require, exports, module) {
	'use strict';

	var userHTML = require('text!my_templates/user.tpl'),
		jstemplate = require('vendor/jstemplate');

	var userTemplate = jstemplate.parse(userHTML);
	var rended = userTemplate.rende({
		name: 'Joni'
	});
	console.log(rended);
});

Requirejs (with plugin)

define(function(require, exports, module) {
	'use strict';

	var userTemplate = require('tplparse!my_templates/user');
	var rended = userTemplate.rende({
		name: 'Joni'
	});
	console.log(rended);
});