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

requirejs-undertemplate

v0.0.4

Published

Require.js loader plugin for underscore.js templates

Downloads

4

Readme

requirejs-underscore-tpl

This is an AMD loader for Underscore.js micro-templates. It's a better maintained fork of jfparadis/requirejs-tpl and can be used as a drop-in replacement to ZeeAgency/requirejs-tpl.

Overview

  • Uses the _.template() engine provided by Underscore.js.
  • Uses the official text loader plugin provided by Require.js.
  • You don't have to specify the template file extension (.html is assumed, but this is configurable).

Notes:

  • Both libraries can be removed at build-time using r.js.
  • The extension .html is assumed, and this makes loading templates similar to loading JavaScript files with Require.js (all extensions are assumed).

Installation

To install it via NPM.js:

npm install requirejs-undertemplate

Then configure require.js:

require.config({
  paths: {
    underscore: 'node_modules/underscore/underscore',
    text: 'node_modules/requirejs-text/text'
    tpl: 'node_modules/requirejs-undertemplate/tpl'
  },
  shim: {
    'underscore': {
      exports: '_'
    }
  }
});

Usage

Specify the plugin using tpl! followed by the template file:

require(['backbone', 'tpl!template'], function (Backbone, template) {
  return Backbone.View.extend({
    initialize: function(){
      this.render();
    },
    render: function(){
      this.$el.html(template({message: 'hello'}));
  });
});

Customization

You can specify the template file extension in your main.js:

require.config({

  // some paths and shims

  tpl: {
    extension: '.tpl' // default = '.html'
  }
});

Underscore allows you to configure the style of templating (more specifically, the syntax for how variables are interpolated, conditional statements and comments). Refer to the templateSettings variable.

Similarly to setting the template file extension, you can set templateSettings in your main.js:

require.config({

    // Use Mustache style syntax for variable interpolation

    templateSettings: {
        evaluate : /\{\[([\s\S]+?)\]\}/g,
        interpolate : /\{\{([\s\S]+?)\}\}/g
    }
});

Optimization

This plugin is compatible with r.js.

Optimization brings three benefits to a project:

  • The templates are bundled within your code and not dynamically loaded which reduces the number of HTTP requests.
  • The templates are pre-compiled before being bundled which reduces the work the client has to do.
  • You can use the compiled, non-minimized version of the templates to step over the code in a debugger.

The most important build options are:

stubModules: ['underscore', 'text', 'tpl']

The list of modules to stub out in the optimized file, i.e. the code is replaced with define('module',{}); by r.js

removeCombined: true

Removes from the output folder the files combined into a build.

Example

You'll need a web-server to serve the files. requirejs-text is not compatible with the file:// protocol and thus opening index.hml directly from your browser will not work.

You can use the Node.js-based http-server package to serve the example files.

  $ npm install
  $ ./node_modules/.bin/http-server

Go to http://localhost:8080/example. Your browser should load:

  • index.html
  • require.js
  • main.js
  • tpl.js
  • underscore.js
  • text.js
  • message.html

Go to http://localhost:8080/example-build. Your browser should load:

  • index.html
  • require.js
  • main.js