Merge pull request #2103 from jfparadis/requirejs-tpl

[AUTHOR] Add requirejs-tpl 0.0.2
This commit is contained in:
Thomas Davis
2013-11-04 20:44:44 -08:00
3 changed files with 109 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
// RequireJS UnderscoreJS template plugin
// http://github.com/jfparadis/requirejs-tpl
//
// An alternative to http://github.com/ZeeAgency/requirejs-tpl
//
// Using UnderscoreJS micro-templates at http://underscorejs.org/#template
// Using and RequireJS text.js at http://requirejs.org/docs/api.html#text
// @author JF Paradis
// @version 0.0.2
//
// Released under the MIT license
//
// Usage:
// require(['backbone', 'tpl!mytemplate'], function (Backbone, mytemplate) {
// return Backbone.View.extend({
// initialize: function(){
// this.render();
// },
// render: function(){
// this.$el.html(mytemplate({message: 'hello'}));
// });
// });
//
// Configuration: (optional)
// require.config({
// tpl: {
// extension: '.tpl' // default = '.html'
// }
// });
/*jslint nomen: true */
/*global define: false */
define(['text', 'underscore'], function (text, _) {
'use strict';
var buildMap = {},
buildTemplateSource = "define('{pluginName}!{moduleName}', function () { return {source}; });\n";
return {
version: '0.0.2',
load: function (moduleName, parentRequire, onload, config) {
if (buildMap[moduleName]) {
onload(buildMap[moduleName]);
} else {
var ext = (config.tpl && config.tpl.extension) || '.html';
var path = (config.tpl && config.tpl.path) || '';
text.load(path + moduleName + ext, parentRequire, function (source) {
buildMap[moduleName] = _.template(source);
onload(buildMap[moduleName]);
}, config);
}
},
write: function (pluginName, moduleName, write) {
var build = buildMap[moduleName],
source = build && build.source;
if (source) {
write.asModule(pluginName + '!' + moduleName,
buildTemplateSource
.replace('{pluginName}', pluginName)
.replace('{moduleName}', moduleName)
.replace('{source}', source));
}
}
};
});
+13
View File
@@ -0,0 +1,13 @@
// RequireJS UnderscoreJS template plugin
// http://github.com/jfparadis/requirejs-tpl
//
// An alternative to http://github.com/ZeeAgency/requirejs-tpl
//
// Using UnderscoreJS micro-templates at http://underscorejs.org/#template
// Using and RequireJS text.js at http://requirejs.org/docs/api.html#text
// @author JF Paradis
// @version 0.0.2
//
// Released under the MIT license
define(["text","underscore"],function(text,_){var buildMap={},buildTemplateSource="define('{pluginName}!{moduleName}', function () { return {source}; });\n";return{version:"0.0.2",load:function(moduleName,parentRequire,onload,config){if(buildMap[moduleName])onload(buildMap[moduleName]);else{var ext=config.tpl&&config.tpl.extension||".html";var path=config.tpl&&config.tpl.path||"";text.load(path+moduleName+ext,parentRequire,function(source){buildMap[moduleName]=_.template(source);onload(buildMap[moduleName])},
config)}},write:function(pluginName,moduleName,write){var build=buildMap[moduleName],source=build&&build.source;if(source)write.asModule(pluginName+"!"+moduleName,buildTemplateSource.replace("{pluginName}",pluginName).replace("{moduleName}",moduleName).replace("{source}",source))}}});
+27
View File
@@ -0,0 +1,27 @@
{
"name": "requirejs-tpl",
"filename": "tpl.js",
"version": "0.0.2",
"description": "A RequireJS/AMD loader plugin for Underscore.js micro-templates",
"homepage": "http://github.com/jfparadis/requirejs-tpl",
"keywords": [
"require",
"requirejs",
"underscore",
"template"
],
"maintainers": [
{
"name": "jfparadis",
"email": "jf@jeanfrancoisparadis.com",
"web": "http://jeanfrancoisparadis.com/"
}
],
"repositories": [
{
"type": "git",
"url": "https://github.com/jfparadis/requirejs-tpl"
}
]
}