GithubHelp home page GithubHelp logo

handlebars-helper's People

Contributors

fauntleroy avatar joanniclaborde 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

handlebars-helper's Issues

Add `equal` helper

The equal helper is basically the same as #if except it allows you to check for things other than truthiness. Comparisons are made with ==.

count

5

Handlebars

<!-- Equal with value argument: 5 -->
{{#equal count 5}}There are five{{/equal}}
<!-- Equal with value argument: 22 -->
{{#equal count 22}}There are twenty two{{/equal}}

Output

<!-- Equal with value argument: 5 -->
There are five
<!-- Equal with value argument: 22 -->

Add `time` helper

A helper for formatting time strings. Ideally for converting things like 233 to 5:53.

ISO8601 formatDate preset

Inserting machine readable datetimes is sometimes needed in templates, i.e. for the <time> element's datetime attribute and similar microdata. This can also be useful when scripting. The format of course should be consistent, and ISO8601 is the best choice.

It'd be handy to have a preset for using this format rather than having to specify it by hand, i.e.

{{ formatDate date 'iso8601' }}

vs.

{{ formatDate date '%Y-%m-%dT%H:%M:%S%z' }}

Add `where` helper

The where helper should create a block that contains all of the items in an array that have a matching key/value. An optional limit can be provided that limits how many items are retrieved. A negative limit will traverse the array in reverse.

cats array

[{
    "name": "Wesley",
    "age": 8
},{
    "name": "Twizzler",
    "age": 8
},{
    "name": "Chip"
    "age": 1
}]

Handlebars

<!-- where with minimum arguments: cats age 1 -->
<ul>{{#where cats age 1}}<li>{{name}}</li>{{/where}}</ul>
<!-- where with limit: cats age 8 1 -->
<ul>{{#where cats age 8 1}}<li>{{name}}</li>{{/where}}</ul>
<!-- where with negative limit: cats age 8 -1 -->
<ul>{{#where cats age 8 -1}}<li>{{name}}</li>{{/where}}</ul>

Output

<!-- where with minimum arguments: cats age 1 -->
<ul>
    <li>Chip</li>
</ul>
<!-- where with limit: cats age 8 1 -->
<ul>
    <li>Wesley</li>
</ul>
<!-- where with negative limit: cats age 8 -1 -->
<ul>
    <li>Twizzler</li>
</ul>

Fail gracefully

Right now many helpers will completely break template rendering if they don't get data of the correct type. Ideally this should just return nothing (from the helper), rather than destroy the whole page render.

Make CommonJS compatible

Needs to work with CommonJS for node.js, but it would also be lovely to use this via Browserify.

Add `range` helper

We need a helper for traversing sections of arrays. Something like this:

cats array

["Wesley","Twizzler","Pixel","Chip","Milk"]

Handlebars

<!-- Range with only an end argument to 1-->
<ul>{{#range cats 1}}<li>{{this}}</li>{{/range}}</ul>
<!-- Range with a start and end argument 0 to 2 -->
<ul>{{#range cats 0 3}}<li>{{this}}</li>{{/range}}</ul>
<!-- Range with a negative start argument -3 to end -->
<ul>{{#range cats -3}}<li>{{this}}</li>{{/range}}</ul>
<!-- Range with a negative start and end argument -3 to -2 -->
<ul>{{#range cats -3 -2}}<li>{{this}}</li>{{/range}}</ul>

Output

<!-- Range with only an end argument to 1-->
<ul>
    <li>Wesley</li>
</ul>
<!-- Range with a start and end argument 0 to 2 -->
<ul>
    <li>Wesley</li>
    <li>Twizzler</li>
    <li>Pixel</li>
</ul>
<!-- Range with a negative start argument -3 to end-->
<ul>
    <li>Pixel</li>
    <li>Chip</li>
    <li>Milk</li>
</ul>
<!-- Range with a negative start and end argument -3 to -2 -->
<ul>
    <li>Pixel</li>
    <li>Chip</li>
</ul>

Add `number` helper

The number helper should provide a set of tools for turning a number into a string representation of the number. Numbers like 1000 can be converted to formats like: 1,000, one thousand, 1k, or 1,000.00.

dollars

1000

Handlebars

<!-- Number with no arguments -->
${{#number dollars}}
<!-- Number with format argument: quantity -->
${{#number dollars quantity}}
<!-- Number with format argument: word -->
{{#number dollars word}} dollars
<!-- Number with format argument: short -->
${{#number dollars short}}
<!-- Number with format and decimal arguments -->
${{#number dollars quantity 2}}

Output

<!-- Number with no arguments -->
$1,000
<!-- Number with format argument: quantity -->
$1,000
<!-- Number with format argument: word -->
one thousand dollars
<!-- Number with format argument: short -->
$1k
<!-- Number with format and decimal arguments -->
$1,000.00

Add `replace` helper

The replace helper should provide basic string replacement functionality. Users should be able to provide a string to replace and a replacement.

url

http://sparkart.com/images/cats.jpg

Handlebars

<!-- Replace helper with arguments: sparkart.com cdn.sparkart.com -->
<img src="{{#replace url sparkart.com cdn.sparkart.com}}" />

Output

<!-- Replace helper with arguments: sparkart.com cdn.sparkart.com -->
<img src="http://cdn.sparkart.com/images/cats.jpg" />

Add `encode` helper

The encode helper allows you to encode a string. At first, this will just cover URI encoding.

link

"http://sparkart.com"

Handlebars

<!-- Encode helper with type argument: uri -->
<a href="http://google.com/search?q={{#encode link uri}}">Search for Sparkart</a>

Output

<!-- Encode helper with type argument: uri -->
<a href="http://google.com/search?q=http%3A%2F%2Fsparkart.com">Search for Sparkart</a>

Review Assemble HH and Swag

Look over Assemble's Handlebars helpers and Swag for useful helpers. Carry over useful helpers and modify them as needed. Worry about documentation and organization later.

Cleaner module exporting

Hey, thanks for these helpers :)

For easier consumption, I would suggest re-organizing the helpers to follow this kind of pattern: https://github.com/solarmosaic/handlebars-helpers

As-is, requiring individual helpers is overly redundant:

var helpers = require("handlebars-helper").helpers;

And this would be cleaner:

var helpers = require("handlebars-helper");

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.