mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Adds handlerbars helper 'foreach'
Function allows you to iterate through an array or object in handlebars Sets Key Values for @first/ @last entry and @odd/@even to true/false
This commit is contained in:
parent
17c8c02eae
commit
b9e1ddcb2e
1 changed files with 72 additions and 0 deletions
|
@ -5,6 +5,7 @@
|
|||
moment = require('moment'),
|
||||
when = require('when'),
|
||||
navHelper = require('./ghostNav'),
|
||||
hbs = require('express-hbs'),
|
||||
coreHelpers;
|
||||
|
||||
coreHelpers = function (ghost) {
|
||||
|
@ -44,6 +45,77 @@
|
|||
return JSON.stringify(object);
|
||||
});
|
||||
|
||||
ghost.registerThemeHelper('foreach', function (context, options) {
|
||||
var fn = options.fn,
|
||||
inverse = options.inverse,
|
||||
i = 0,
|
||||
j = 0,
|
||||
key,
|
||||
ret = "",
|
||||
data;
|
||||
|
||||
if (options.data) {
|
||||
data = hbs.handlebars.createFrame(options.data);
|
||||
}
|
||||
|
||||
if (context && typeof context === 'object') {
|
||||
if (context instanceof Array) {
|
||||
for (j = context.length; i < j; i += 1) {
|
||||
if (data) {
|
||||
data.index = i;
|
||||
data.first = data.last = data.even = data.odd = false;
|
||||
if (i === 0) {
|
||||
data.first = true;
|
||||
}
|
||||
if (i === j - 1) {
|
||||
data.last = true;
|
||||
}
|
||||
// first post is index zero but still needs to be odd
|
||||
if (i % 2 === 1) {
|
||||
data.even = true;
|
||||
} else {
|
||||
data.odd = true;
|
||||
}
|
||||
}
|
||||
ret = ret + fn(context[i], { data: data });
|
||||
}
|
||||
} else {
|
||||
for (key in context) {
|
||||
if (context.hasOwnProperty(key)) {
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
for (key in context) {
|
||||
if (context.hasOwnProperty(key)) {
|
||||
if (data) {
|
||||
data.key = key;
|
||||
data.first = data.last = data.even = data.odd = false;
|
||||
if (i === 0) {
|
||||
data.first = true;
|
||||
}
|
||||
if (i === j - 1) {
|
||||
data.last = true;
|
||||
}
|
||||
// first post is index zero but still needs to be odd
|
||||
if (i % 2 === 1) {
|
||||
data.even = true;
|
||||
} else {
|
||||
data.odd = true;
|
||||
}
|
||||
}
|
||||
ret = ret + fn(context[key], {data: data});
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (i === 0) {
|
||||
ret = inverse(this);
|
||||
}
|
||||
return ret;
|
||||
});
|
||||
|
||||
return when.all([
|
||||
// Just one async helper for now, but could be more in the future
|
||||
navHelper.registerWithGhost(ghost)
|
||||
|
|
Loading…
Add table
Reference in a new issue