Coder Social home page Coder Social logo

underscore-template-fetcher's Introduction

underscore-template-fetcher

Usage

underscore-template-fetcher is a small utility function built on top of underscorejs. It adds to underscore another utility function, _.fetchTemplate(), which can be used like so:

// fetch a template -- /templates/my-awesome-template.jst -- then compile it
var raw = _.fetchTemplate('my-awesome-template');
var t   = _.template(raw, { text: 'woot' });

Explained

In essence, underscore-template-fetcher exposes the following two methods, a fetch method and config method:

// Fetch a template
_.fetchTemplate(name [, namespace] [,extension] );

// Setup config for fetching templates. Valid opts include:
var defaults =  {
    namespaces: { default: "/templates/" },
    extension:  ".jst"  
  };
_.templateFetcherSettings(defaults);

This method assumes all of your templates live under /templates/ and have a file extension .jst. There is no need to call _.templateFetcherSettings(defaults); if you're happy with the defaults.

Advanced

In certain apps, you may have templates located under various endpoints. Setup namepaces pointed at these endpoints like so:

// setup underscore-template-fetcher 
_.templateFetcherSettings({
  namespaces: {
    default:    '/my-templates/',
    hovercard:  '/templates/hovercard/',
    dialog:     '/plugins/dialogs/'
  },
  extension:  '.tmpl'
});

// fetch a template located at "/my-templates/awesome-template.tmpl"
_.fetchTemplate('awesome-template');

// fetch a template located at "/templates/hovercard/profile.tmpl"
_.fetchTemplate('profile', 'hovercard');

// fetch a template located at "/plugins/dialogs/confirm.jst"
_.fetchTemplate('confirm', 'dialog', '.jst');

// fetch a non-namespaced template located at "/super/random/location.jst"
// (when prefixed with a "/", default namespaces and extensions are ignored)
_.fetchTemplate('/super/random/location.jst');

Testing

You can run the test suite by running grunt server then pointing your browser at localhost:8000/test.html.

underscore-template-fetcher's People

Contributors

briangonzalez avatar

Stargazers

 avatar Narayan avatar

Watchers

James Cloos avatar  avatar

Forkers

web1o1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.