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 🙏

© 2026 – Pkg Stats / Ryan Hefner

tpl2module

v0.1.4

Published

Compile templates into JS module

Readme

tpl2module

A command line tool that compiles templates into JSON or JS module.

Getting Started

In modern AJAX applications, it is a very common to use client side template rendering for html generation. There are a bunch of great JS templating engines, like mustache.js, underscore.js template utility, etc.

However, there are no good way to mention how to load templates. We could definately embed templates in the code, but that compromise readibility of the code, especially when the template is very large.

You could save your template file as html / txt file, and get them using AJAX one by one. Nonetheless, this solution would ended up in-efficient if there are many template files.

Or, use tpl2module. It allows you to compile all your template files into one JSON file or JS module ( AMDJS / require.js module or your customized module format). So you can get all your templates in one AJAX call, which could reduce the network overhead and loading time.

Installation

Install the module with: npm install tpl2module

Example - Compile templates into JSON file.

For example you have following folder structure.

[templates]
   |- tpl1.html
   |- tpl2.html
   |- [a]
       |- tpl3.html
       |-[b]
          |- tpl4.html

Execute the following command.

$> tpl2module -s templates --json
writing to templates.json

It would compile into templates.json with following content.

{
    "tpl1": "<!-- tpl1 -->\n<p>{{tpl1}}</p>",
    "tpl2": "<!-- tpl2-->\n<p>{{tpl2}}</p>",
    "a.tpl3": "<!-- tpl3 -->\n<p>{{tpl3}}</p>",
    "a.b.tpl4": "<!-- tpl4 -->\n<p>{{tpl4}}</p>"
}

tpl2module will search through the folder recursively, and collect the content of every single file and compile them to one single JSON file.

tpl3.html resides in folder a, and it is stored with key a.tpl3. tpl4.html resides in folder a/b, and it is stored with key a.b.tpl4. In order to keep the key neat, the prefix of the file is omitted.

Example - Compile templates into require.js module.

When using require.js, it is very common to use text plugin to load your template.

tpl2module just provides a alternative way, probably a better way to handle templates.

If you run tpl2module with '-a / --amdjs', it would compile tempaltes into AMDJS / require.js module. By doing so, you do not need text plugin anymore, just load your templates as a normal require.js module.

$> tpl2module -s templates --amdjs
writing (with amdjs format) to templates.js

templates.js

//generated by tpl2js - http://github.com/tly1980/tpl2modue
define({"tpl1":"<!-- tpl1 -->\n<p>{{tpl1}}</p>","tpl2":"<!-- tpl2-->\n<p>{{tpl2}}</p>","a.tpl3":"<!-- tpl3 -->\n<p>{{tpl3}}</p>","a.b.tpl4":"<!-- tpl4 -->\n<p>{{tpl4}}</p>"});

And you can just get your templates as a normal require.js module.

var templates = require('apps/templates');
console.log(templates['a.b.tpl4']);

Here is the output:

"<!-- tpl4 -->
<p>{{tpl4}}</p>"

Most of the time you would just run tpl2module from command line. It is possible to call it from API as a node.js lib, please checkout the source code in unit testcases if you are interested.

License

Copyright (c) 2013 Tom Tang
Licensed under the MIT license.