requirejs-micro-template

An AMD loader plugin for loading micro templates.

npm install requirejs-micro-template
12 downloads in the last month

requirejs-template

embedded JavaScript templates for requirejs, based on John Reisigs micro-template

features

  • any valid JavaScript syntax allowed
  • r.js optimzer will inline the precompiled templates

including in your project

//shortcut to plugin in requirejs config
require.config({
   paths: {
      template: 'path/to/requirejs-template'
   }
});

dependencies

This plugin depends on requirejs-text

template syntax

it's just JavaScript!

<!-- catTemplate.html -->

<!-- iteration -->
<ul>
<% for (var i = 0, len = woolballs.length; i < len; i++) {  %>
   <li><%= woolballs[i]%></li>
<% } %>
</ul>

<!-- condition -->
<div class="cat mood">
<% if (cat.hasCheezburger() === true) {  %>
   <img src="img/happy-cat.png" alt="happy-cat">
<% } else { %>
   <img src="img/sad-cat.png" alt="sad-cat">
<% } %>
</div>

require/use the template

//jquery is not required
define(['jquery', 'template!path/to/catTemplate'], function ($, catTemplate) {
  var $node = $(catTemplate({
      woolballs: [
          'red',
          'green',
          'blue',
          'purple'
      ],

      //could be any arbitary JavaScript object
      cat: {
          hasCheezburger: function () {
              return true;
          }
      }
  }));

  $('body').append($node);
});

content escaping

escaped

<%= ...%>

raw - potential dangerous!!

<%== ...%>
npm loves you