Git Product home page Git Product logo

handlebars-helpers's Introduction

Handlebars Helpers

A small collection of useful helpers for Handlebars.js.

Build Status

This version includes a (very) large API change. Use version 1.1.0 if you'd prefer the "classic" style.

To use, just include helpers.js after you include Handlebars. Or, if you're using AMD/Node, just require the file.

Provided Helpers

The old if_eq, if_gt, unless_gte etc. helpers are now replaced with a much cleaner is helper.

Comparisons

Given one argument, is acts exactly like if:

{{#is x}} ... {{else}} ... {{/is}}

Given two arguments, is compares the two are equal (a non-strict, == comparison, so 5 == '5' is true)

{{#is x y}} ... {{else}} ... {{/is}}

Given three arguments, the second argument becomes the comparator.

{{#is x "not" y}} ... {{else}} ... {{/is}}
{{#is 5 ">=" 2}} ... {{else}} ... {{/is}}

Several comparators are built-in:

  • == (same as not providing a comparator)
  • !=
  • not (alias for !=)
  • ===
  • !==
  • >
  • >=
  • <
  • <=
  • in (check a value exists in either a comma-separated string, or an array, see below)
// Loose equality checking
{{#is x y}} ... {{else}} ... {{/is}}
{{#is x "==" y}} ... {{else}} ... {{/is}}

{{#is x "!=" y}} ... {{else}} ... {{/is}}
{{#is x "not" y}} ... {{else}} ... {{/is}}

// Strict equality checking
{{#is x "===" y}} ... {{else}} ... {{/is}}
{{#is x "!==" y}} ... {{else}} ... {{/is}}

// Greater/Less Than
{{#is x ">" y}} ... {{else}} ... {{/is}}
{{#is x ">=" y}} ... {{else}} ... {{/is}}

{{#is x "<" y}} ... {{else}} ... {{/is}}
{{#is x "<=" y}} ... {{else}} ... {{/is}}

// In comma separated list, or array
{{#is x "in" "foo,bar"}} ... {{else}} ... {{/is}}
{{#is x "in" anArray}} ... {{else}} ... {{/is}}

Registering Custom is comparators

You can extend the provided comparators by registering your own, like so:

// in browser
HandlebarsHelpersRegistry.add('same', function (left, right) { return left === right; });

// with AMD/Node
var HandlebarsHelpersRegistry = require('path/to/helpers');
HandlebarsHelpersRegistry.add('same', function (left, right) { return left === right; });

// usage
var template = '{{#is x "same" y}} Are the same {{else}} Not the same {{/is}}';
Handlebars.compile(template)({ x: 5, y: '5' }); // => " Not the same "

Logging

Log one or multiple values to the console:

{{log foo bar}}

Log one or multiple values to the console, with the current context:

{{debug foo bar}}

nl2br

Convert new lines (\r\n, \n\r, \r, \n) to line breaks

{{nl2br description}}

handlebars-helpers's People

Contributors

danharper avatar ekkis avatar fizerkhan avatar mirozoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

handlebars-helpers's Issues

doesn't work on IE8

this line...

eR.add('in', function (left, right) {
if (!isArray(right)) {
right = right.split(',');
}
return right.indexOf(left) !== -1;
});

will break in IE8. You should shim this or use underscore/lodash to fix this.

Missing license

AFAICS there is no documented license attached to Handlebars-Helpers, which IMHO makes it nearly impossible to use in a legal fashion.

or, and

Can you add operators like ||, && ??

String escaping in nl2br

I discovered that the nl2br helper function returns a Handlebars.SafeString which isn't being escaped during rendering. While this is necessary to leave the inserted br-Tags untouched, it can cause security issues (e. g. XSS) if the INPUT string is not escaped. In my opinion this should be done before the replacement is performed:

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);
});

Greater than always returning true

I am using a simple greater than if statement:

{{#if_gt total_transactions compare="10"}}
    <p>In here</p>
{{/if_gt}}

But I am always getting into the if statement, even though total_transactions is less than 10.

Any ideas?

Handlers lose model depth

Hi, thanks for this extension. I have a problem using my template code along with your handler:

{{#each model._revs_info}}
{{debug ../model}}
{{#if_eq status compare="available"}}
{{debug ../model}}
{{rev}}
{{/if_eq}}
{{/each}}

The first {{debug ../model}} statement works perfectly but the second doesn't. {{debug}} definition: http://thinkvitamin.com/code/handlebars-js-part-3-tips-and-tricks/

I am sure that I am missing something basic. Thanks for your help in advance.

Best, Sameer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.