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

jaaulde-template-manager

v1.0.4

Published

A simple manager for loading and caching templates

Readme

template-manager

A simple manager for loading and caching templates.

Get templates by ID/URL from script tags in the DOM, or from the server via AJAX. Retrieved templates are cached for the next call. You can also inject a named template into the cache for retrieval later. This is handy for writing code that needs templates distributed with it.

Note that this is not a template parsing/rendering engine. It is merely for use in loading and caching templates from various sources.

dependencies

installation

bower

bower install jaaulde-template-manager

npm

npm install jaaulde-template-manager

html

Download the code, link it in your HTML file (after dependencies).

<script src="/path/to/jaaulde-template-manager.js"></script>

usage

Get a template from the cache, DOM, or server

template_manager.get()

signature
/**
 * Get a template from the Cache, DOM, or server
 *
 * @param  string id the ID or URL of the template
 *
 * @return string the tempalate
 */
get: function (id) {
example

If you have a template in the DOM like so:

<script type="text/template" id="hello_world">
	<p>Hello, World! I am {name}.</p>
</script>

Note the id attribute.

You can retrieve it like so:

var hello_world_template = template_manager.get('hello_world');

See how the id attribute of the DOM node is what we passed to get()?

If you have a template on the server at /templates/hello_world.html, you can retrieve it like so:

var hello_world_template = template_manager.get('/partials/hello_world.html');

Simple, right? We just told get() the URL in the same way we told it an ID earlier.

After the first successful fetch of any particular template, that template is cached for faster retrieval next time.

Inject a template into the cache for later retrieval

template_manager.cache()

signature
/**
 * Inject a template string into the cache by name
 *
 * @param  string id The ID you'll use when you want to get the
 *                   template from the manager
 * @param  string template The template to cache
 */
cache: function (id, template) {
example
template_manager.cache('hello_world', '<p>Hello, World! I am {name}.</p>');

Later, when you're ready, you can ask for the template via template_manager.get('hello_world').

Using with build systems

The ability to inject things into the cache may not seem all that handy, but consider a JavaScript package that also needs templates, and is intended for distribution over Bower or NPM. Figuring out where to put templates, and how to reference them from the script, and how to keep file paths and URLs synced up on any number of systems to which the package could be installed is very difficult.

By including this package in that package's dependencies, however, the author could simply inject templates into the cache in her code. Even better, the author could write her package with actual HTML files holding each template in her src, and have a build system compile those and write lines of JavaScript that inject them into the cache in the dist output. Her src would be nice and clean, and her dist works wherever it is installed!

Here's an example in GulpJS using gulp-angular-templatecache. Note that this plugin was intended for use with AngularJS' own template caching system for the same purpose, but provides enough option overrides that, out of the box, it can be used for this package, too.

gulp.task('templates', function () {
	return gulp
            .src('src/**/*.html')
            .pipe(plugins.htmlmin({
                collapseWhitespace: true,
                removeComments: true
            }))
            .pipe(plugins.angularTemplatecache({
                templateHeader: '\n\t',
                templateBody: 'template_manager.cache(\'<%= url %>\',\'<%= contents %>\');',
                templateFooter: '\n',
                moduleSystem: 'iife'
            }))
            .pipe(gulp.dest('dist/'));
});

Consider a package with a JS file, src/index.js and a template file, src/templates/hello.html, that looks like:

<p>
	Hello, World! I am {name}.
</p>

Our HTML is stored outside of our JS in the src, and is uncompressed and easy to view and edit. The src/index.js file would be written such that the template is loaded via template_manager.get('templates/hello.html'). (Notice that the id/url/path passed to .get() is relative to index.js within the src of the repo. This convention helps you find templates in your src when you're looking through your JS and see them referenced. It just so happens that the author of gulp-angular-templatecache thinks along the same lines and, by default, uses the path to the file as its identifier when injecting--see output example below.)

Running our example Gulp templates task in that package would produce the following output in dist/templates.js:

(function () {
	template_manager.cache('templates/hello.html', '<p>Hello, World! I am {name}.</p>');
}());

In your application or webpage, loading templates.js before index.js attempts to .get() that template will ensure it is available in the cache.