requirejs-micro-template

0.1.2 • Public • Published

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!!

<%== ...%>

Readme

Keywords

none

Package Sidebar

Install

npm i requirejs-micro-template

Weekly Downloads

0

Version

0.1.2

License

MIT

Last publish

Collaborators

  • martyglaubitz