Skip to content

Instantly share code, notes, and snippets.

@jlbruno
Forked from scottjehl/tmplmeta.md
Created February 19, 2013 01:56
Show Gist options
  • Save jlbruno/4982460 to your computer and use it in GitHub Desktop.
Save jlbruno/4982460 to your computer and use it in GitHub Desktop.

I've been finding this little meta[name='tmpl'] pattern useful lately when making template-based decisions in JS, such as when loading a particular file or set of files that are needed only on a particular page of a site.

First, in the HTML of a particular template, like say, a search result page:

<head>
  ...
  <meta name="tmpl" content="searchresult">
</head>

..and then in JS:

var tmplmeta = document.querySelector && document.querySelector( "meta[name='tmpl']" );
var tmpl;

if( tmplmeta ){
  var tmpl = tmplmeta.content;
}

...now tmpl can be used just like you might use other feature or environmental tests, such as perhaps when loading files via a tool like yepnope.js:

yepnope({
  test: tmpl === "searchresult"
  yep: "js/searchresult.js"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment