Skip to content

Commit 7f58596

Browse files
committed
initial committ
0 parents  commit 7f58596

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

LICENSE.html

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2012 David Beck, Rotunda Software
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Define global template helpers in underscore. Example:
2+
3+
_.addTemplateHelpers( {
4+
iff : function( condition, outputString ) {
5+
return condition ? outputString : "";
6+
}
7+
} );
8+
9+
Now in an underscore template:
10+
11+
<script type="text/template">
12+
<div class="button <% iff( isHightlighted, "highlighted" ) %>">
13+
My button
14+
</div>
15+
</script>

underscore.template-helpers.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(function() {
2+
var originalUnderscoreTemplateFunction = _.template;
3+
var templateHelpers = {};
4+
5+
_.mixin({
6+
addTemplateHelpers : function( newHelpers ) {
7+
_.extend( templateHelpers, newHelpers );
8+
},
9+
10+
template : function(text, data, settings) {
11+
// replace the built in _.template function with one that supports the addTemplateHelpers
12+
// function above. Basically the combo of the addTemplateHelpers function and this new
13+
// template function allows us to mix in global "helpers" to the data objects passed
14+
// to all our templates when they render. This replacement template function just wraps
15+
// the original _.template function, so it sould be pretty break-resistent moving forward.
16+
17+
if( data )
18+
{
19+
// if data is supplied, the original _.template function just returns the raw value of the
20+
// render function (the final rentered html/text). So in this case we just extend
21+
// the data param with our templateHelpers and return raw value as well.
22+
23+
_.defaults( data, templateHelpers ); // extend data with our helper functions
24+
return originalUnderscoreTemplateFunction.apply( this, arguments ); // pass the buck to the original _.template function
25+
}
26+
27+
var template = originalUnderscoreTemplateFunction.apply( this, arguments );
28+
29+
var wrappedTemplate = function( data ) {
30+
_.defaults( data, templateHelpers );
31+
return template.apply( this, arguments );
32+
};
33+
34+
return wrappedTemplate;
35+
}
36+
} );
37+
})();

0 commit comments

Comments
 (0)