Fix @blog globals in special templates

fixes #5024

- pass options through to the template for both navigation and pagination
- add a test for each
This commit is contained in:
Hannah Wolfe 2015-03-28 18:00:57 +02:00
parent 86e3835e4d
commit 501595127f
7 changed files with 84 additions and 4 deletions

View File

@ -54,7 +54,7 @@ navigation = function (options) {
context = _.merge({}, {navigation: output});
return template.execute('navigation', context);
return template.execute('navigation', context, options);
};
module.exports = navigation;

View File

@ -38,7 +38,7 @@ pagination = function (options) {
context.authorSlug = this.author.slug;
}
return template.execute('pagination', context);
return template.execute('pagination', context, options);
};
module.exports = pagination;

View File

@ -6,7 +6,7 @@ var templates = {},
// Execute a template helper
// All template helpers are register as partial view.
templates.execute = function (name, context) {
templates.execute = function (name, context, options) {
var partial = hbs.handlebars.partials[name];
if (partial === undefined) {
@ -19,7 +19,7 @@ templates.execute = function (name, context) {
hbs.registerPartial(partial);
}
return new hbs.handlebars.SafeString(partial(context));
return new hbs.handlebars.SafeString(partial(context, options));
};
// Given a theme object and a post object this will return

View File

@ -3,6 +3,7 @@
var should = require('should'),
hbs = require('express-hbs'),
utils = require('./utils'),
path = require('path'),
// Stuff we are testing
handlebars = hbs.handlebars,
@ -116,3 +117,46 @@ describe('{{navigation}} helper', function () {
rendered.string.should.containEql('nav-bar"');
});
});
describe('{{navigation}} helper with custom template', function () {
var optionsData;
before(function (done) {
utils.loadHelpers();
hbs.express3({
partialsDir: [path.resolve(utils.config.paths.corePath, 'test/unit/server_helpers/test_tpl')]
});
hbs.cachePartials(function () {
done();
});
});
beforeEach(function () {
optionsData = {
data: {
blog: {
navigation: [],
title: 'Chaos is a ladder.'
},
root: {
relativeUrl: ''
}
}
};
});
it('can render one item and @blog title', function () {
var singleItem = {label: 'Foo', url: '/foo'},
testUrl = 'href="' + utils.config.url + '/foo"',
rendered;
optionsData.data.blog.navigation = [singleItem];
rendered = helpers.navigation(optionsData);
should.exist(rendered);
rendered.string.should.containEql('Chaos is a ladder');
rendered.string.should.containEql(testUrl);
rendered.string.should.containEql('Foo');
});
});

View File

@ -3,6 +3,7 @@
var should = require('should'),
hbs = require('express-hbs'),
utils = require('./utils'),
path = require('path'),
// Stuff we are testing
handlebars = hbs.handlebars,
@ -122,3 +123,31 @@ describe('{{pagination}} helper', function () {
.should.throwError('Invalid value, check page, pages, limit and total are numbers');
});
});
describe('{{pagination}} helper with custom template', function () {
before(function (done) {
utils.loadHelpers();
hbs.express3({partialsDir: [path.resolve(utils.config.paths.corePath, 'test/unit/server_helpers/test_tpl')]});
hbs.cachePartials(function () {
done();
});
});
it('can render single page with @blog.title', function () {
var rendered = helpers.pagination.call({
pagination: {page: 1, prev: null, next: null, limit: 15, total: 8, pages: 1},
tag: {slug: 'slug'}
}, {
data: {
blog: {
title: 'Chaos is a ladder.'
}
}
});
should.exist(rendered);
// strip out carriage returns and compare.
rendered.string.should.match(/Page 1 of 1/);
rendered.string.should.containEql('Chaos is a ladder');
});
});

View File

@ -0,0 +1,5 @@
{{@blog.title}}
{{#foreach navigation}}
<a href="{{url absolute="true"}}">{{label}}</a>
{{/foreach}}

View File

@ -0,0 +1,2 @@
{{@blog.title}}
<span class="page-number">Page {{page}} of {{pages}}</span>