Fix bug in Handlebars apply helper. Update docs

This commit is contained in:
Dmitriy Simushev 2012-10-11 13:25:14 +00:00
parent c1b633843f
commit 4102ce00f3
2 changed files with 10 additions and 2 deletions

View File

@ -5,5 +5,5 @@
Copyright (c) 2005-2011 Mibew Messenger Community
License: http://mibew.org/license.php
*/
Handlebars.registerHelper("apply",function(b,a){var c=b,e=/^[0-9A-z_]+$/,a=a.split(/\s*,\s*/),d;for(d in a)if(a.hasOwnProperty(d)&&e.test(a[d])){if("function"!=typeof Handlebars.helpers[a[d]])throw Error("Unregistered helper '"+a[d]+"'!");c=Handlebars.helpers[a[d]](c)}return new Handlebars.SafeString(c)});
Handlebars.registerHelper("apply",function(b,a){var c=b,e=/^[0-9A-z_]+$/,a=a.split(/\s*,\s*/),d;for(d in a)if(a.hasOwnProperty(d)&&e.test(a[d])){if("function"!=typeof Handlebars.helpers[a[d]])throw Error("Unregistered helper '"+a[d]+"'!");c=Handlebars.helpers[a[d]](c).toString()}return new Handlebars.SafeString(c)});
Handlebars.registerHelper("formatTime",function(b){var a=new Date(1E3*b),b=a.getHours().toString(),c=a.getMinutes().toString(),a=a.getSeconds().toString();return(10<b?b:"0"+b)+":"+(10<c?c:"0"+c)+":"+(10<a?a:"0"+a)});Handlebars.registerHelper("urlReplace",function(b){return new Handlebars.SafeString(b.replace(/((?:https?|ftp):\/\/\S*)/g,'<a href="$1" target="_blank">$1</a>'))});Handlebars.registerHelper("nl2br",function(b){return new Handlebars.SafeString(b.replace(/\n/g,"<br/>"))});

View File

@ -11,11 +11,19 @@
*
* This helper provide an ability to apply several helpers to single Handlebars
* expression
*
* Example of helper usage:
* <code>
* {{apply text "emHelper, strongHelper"}}
* </code>
* In the example above helpers will apply to text one after another: first
* 'emHelper' and second 'strongHelper'.
*/
Handlebars.registerHelper('apply', function(text, helpers) {
var result = text;
var validHelperName = /^[0-9A-z_]+$/;
helpers = helpers.split(/\s*,\s*/);
// Apply helpers one after another
for (var prop in helpers) {
if (! helpers.hasOwnProperty(prop) ||
! validHelperName.test(helpers[prop])) {
@ -26,7 +34,7 @@ Handlebars.registerHelper('apply', function(text, helpers) {
"Unregistered helper '" + helpers[prop] + "'!"
);
}
result = Handlebars.helpers[helpers[prop]](result);
result = Handlebars.helpers[helpers[prop]](result).toString();
}
return new Handlebars.SafeString(result);
});