Skip to content

Instantly share code, notes, and snippets.

@denyskoch
Created July 26, 2015 11:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denyskoch/8cb294c81ea39c878dd4 to your computer and use it in GitHub Desktop.
Save denyskoch/8cb294c81ea39c878dd4 to your computer and use it in GitHub Desktop.
Handlebars block helper for filtering arrays by a path.
function filterHelper(context, options) {
if (!options || !options.hash.where) {
throw new Exception('Must pass <where> and <is> | <isnot> to #filter');
}
if (Handlebars.Utils.isFunction(context)) {
context = context.call(this);
}
if (options.data) {
var data = Handlebars.createFrame(options.data);
}
var newContext = context;
var skiped = 0;
var included = 0;
var path = options.hash.where.split('.');
var pathLength = path.length;
if (Handlebars.Utils.isArray(context)) {
newContext = context.filter(function(obj){
var current = obj;
for (var i = 0; i < pathLength; i++) {
if (current[path[i]] == undefined) {
current = undefined;
}
current = current[path[i]];
}
included++;
if (options.hash.maxSkip && skiped >= options.hash.maxSkip) {
return true;
}
if (options.hash.max && included > options.hash.max) {
return false;
}
if (current == options.hash.is) {
if (current == options.hash.is) {
return true;
}
}
else if (options.hash.isnot) {
if (current != undefined && current != options.hash.isnot) {
return true;
}
}
included--;
skiped++;
return false;
});
}
else {
throw new Exception('Must pass array to #filter');
}
if (options.hash.as)
data[options.hash.as] = newContext
return options.fn(newContext[0], {data: data});
}
{{#filter posts where="featured" is=true max=1}}
<article class="{{post_class}}">
<header class="post-header {{#if image}} with-image" style="background-image: url({{image}}){{else}}no-cover{{/if}}">
<h2 class="post-title"><a href="{{url}}">{{{title}}}</a></h2>
</header>
<section class="post-excerpt">
<p>{{excerpt words="26"}} <a class="read-more" href="{{url}}">&raquo;</a></p>
</section>
<footer class="post-meta">
{{#if author.image}}<img class="author-thumb" src="{{author.image}}" alt="{{author.name}}" nopin="nopin" />{{/if}}
{{author}}
{{tags prefix=" on "}}
<time class="post-date" datetime="{{date format='YYYY-MM-DD'}}">{{date format="DD MMMM YYYY"}}</time>
</footer>
</article>
{{/filter}}
{{#filter posts where="featured" isnot=true as="filteredPosts" maxSkip=1}}
{{#foreach @filteredPosts columns=3}}
{{#if @rowStart}}
<div class="show-3">
{{/if}}
<article class="column {{post_class}}">
<header class="post-header {{#if image}} with-image" style="background-image: url({{image}}){{else}}no-cover{{/if}}">
<h2 class="post-title"><a href="{{url}}">{{{title}}}</a></h2>
</header>
<section class="post-excerpt">
<p>{{excerpt words="26"}} <a class="read-more" href="{{url}}">&raquo;</a></p>
</section>
<footer class="post-meta">
{{#if author.image}}<img class="author-thumb" src="{{author.image}}" alt="{{author.name}}" nopin="nopin" />{{/if}}
{{author}}
{{tags prefix=" on "}}
<time class="post-date" datetime="{{date format='YYYY-MM-DD'}}">{{date format="DD MMMM YYYY"}}</time>
</footer>
</article>
{{#if @rowEnd}}
</div>
{{/if}}
{{/foreach}}
{{/filter}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment