Added handlebars.helpers (if_eq, etc)

This commit is contained in:
Mark McDowall 2013-06-26 17:29:48 -07:00
parent 281790ff0a
commit 2fd73ea446
5 changed files with 156 additions and 3 deletions

View File

@ -2,6 +2,7 @@
define( define(
[ [
'templates', 'templates',
'handlebars.helpers',
'Handlebars/Helpers/DateTime', 'Handlebars/Helpers/DateTime',
'Handlebars/Helpers/Html', 'Handlebars/Helpers/Html',
'Handlebars/Helpers/Numbers', 'Handlebars/Helpers/Numbers',

View File

@ -0,0 +1,145 @@
/* Handlebars Helpers - Dan Harper (http://github.com/danharper) */
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */
/**
* Following lines make Handlebars helper function to work with all
* three such as Direct web, RequireJS AMD and Node JS.
* This concepts derived from UMD.
* @courtesy - https://github.com/umdjs/umd/blob/master/returnExports.js
*/
(function (root, factory) {
if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory(require('handlebars'));
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['handlebars'], factory);
} else {
// Browser globals (root is window)
root.returnExports = factory(root.Handlebars);
}
}(this, function (Handlebars) {
/**
* If Equals
* if_eq this compare=that
*/
Handlebars.registerHelper('if_eq', function(context, options) {
if (context == options.hash.compare)
return options.fn(this);
return options.inverse(this);
});
/**
* Unless Equals
* unless_eq this compare=that
*/
Handlebars.registerHelper('unless_eq', function(context, options) {
if (context == options.hash.compare)
return options.inverse(this);
return options.fn(this);
});
/**
* If Greater Than
* if_gt this compare=that
*/
Handlebars.registerHelper('if_gt', function(context, options) {
if (context > options.hash.compare)
return options.fn(this);
return options.inverse(this);
});
/**
* Unless Greater Than
* unless_gt this compare=that
*/
Handlebars.registerHelper('unless_gt', function(context, options) {
if (context > options.hash.compare)
return options.inverse(this);
return options.fn(this);
});
/**
* If Less Than
* if_lt this compare=that
*/
Handlebars.registerHelper('if_lt', function(context, options) {
if (context < options.hash.compare)
return options.fn(this);
return options.inverse(this);
});
/**
* Unless Less Than
* unless_lt this compare=that
*/
Handlebars.registerHelper('unless_lt', function(context, options) {
if (context < options.hash.compare)
return options.inverse(this);
return options.fn(this);
});
/**
* If Greater Than or Equal To
* if_gteq this compare=that
*/
Handlebars.registerHelper('if_gteq', function(context, options) {
if (context >= options.hash.compare)
return options.fn(this);
return options.inverse(this);
});
/**
* Unless Greater Than or Equal To
* unless_gteq this compare=that
*/
Handlebars.registerHelper('unless_gteq', function(context, options) {
if (context >= options.hash.compare)
return options.inverse(this);
return options.fn(this);
});
/**
* If Less Than or Equal To
* if_lteq this compare=that
*/
Handlebars.registerHelper('if_lteq', function(context, options) {
if (context <= options.hash.compare)
return options.fn(this);
return options.inverse(this);
});
/**
* Unless Less Than or Equal To
* unless_lteq this compare=that
*/
Handlebars.registerHelper('unless_lteq', function(context, options) {
if (context <= options.hash.compare)
return options.inverse(this);
return options.fn(this);
});
/**
* Convert new line (\n\r) to <br>
* from http://phpjs.org/functions/nl2br:480
*/
Handlebars.registerHelper('nl2br', function(text) {
text = Handlebars.Utils.escapeExpression(text);
var nl2br = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
return new Handlebars.SafeString(nl2br);
});
}));

View File

@ -1,11 +1,11 @@
<div class="indexer-settings-item"> <div class="indexer-settings-item">
<div> <div>
<h3>{{name}}</h3> <h3>{{name}}</h3>
{{#if isNewznab}} {{#if_eq implementation compare="Newznab"}}
<span class="btn-group pull-right"> <span class="btn-group pull-right">
<button class="btn btn-mini x-delete"><i class="icon-nd-delete"/></button> <button class="btn btn-mini x-delete"><i class="icon-nd-delete"/></button>
</span> </span>
{{/if}} {{/if_eq}}
</div> </div>
<div class="control-group"> <div class="control-group">

View File

@ -6,6 +6,5 @@ define([
successMessage: 'Indexer Saved', successMessage: 'Indexer Saved',
errorMessage : 'Couldn\'t save indexer' errorMessage : 'Couldn\'t save indexer'
}); });
}); });

View File

@ -7,6 +7,7 @@ require.config({
'backbone' : 'JsLibraries/backbone', 'backbone' : 'JsLibraries/backbone',
'sugar' : 'JsLibraries/sugar', 'sugar' : 'JsLibraries/sugar',
'handlebars' : 'JsLibraries/handlebars.runtime', 'handlebars' : 'JsLibraries/handlebars.runtime',
'handlebars.helpers' : 'JsLibraries/handlebars.helpers',
'bootstrap' : 'JsLibraries/bootstrap', 'bootstrap' : 'JsLibraries/bootstrap',
'backbone.mutators' : 'JsLibraries/backbone.mutators', 'backbone.mutators' : 'JsLibraries/backbone.mutators',
'backbone.deepmodel' : 'JsLibraries/backbone.deep.model', 'backbone.deepmodel' : 'JsLibraries/backbone.deep.model',
@ -159,6 +160,13 @@ require.config({
[ [
'backgrid' 'backgrid'
] ]
},
'handlebars.helpers': {
deps:
[
'handlebars'
]
} }
} }
}); });