From 97946cbc141337fc1be55c26437c520cb51db72b Mon Sep 17 00:00:00 2001 From: Chris Pearce Date: Fri, 5 Sep 2014 17:35:30 +0100 Subject: [PATCH] Ability to create custom template files for custom tags. Eg. `tag-design.hbs` --- core/server/controllers/frontend.js | 2 +- core/server/helpers/template.js | 21 ++++++++++++ .../test/unit/server_helpers_template_spec.js | 32 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/core/server/controllers/frontend.js b/core/server/controllers/frontend.js index 96025b67c7..497f870d0f 100644 --- a/core/server/controllers/frontend.js +++ b/core/server/controllers/frontend.js @@ -189,7 +189,7 @@ frontendControllers = { // Render the page of posts filters.doFilter('prePostsRender', page.posts).then(function (posts) { getActiveThemePaths().then(function (paths) { - var view = paths.hasOwnProperty('tag.hbs') ? 'tag' : 'index', + var view = template.getThemeViewForTag(paths, options.tag), // Format data for template result = _.extend(formatPageResponse(posts, page), { diff --git a/core/server/helpers/template.js b/core/server/helpers/template.js index 008a18be5b..a508f2960d 100644 --- a/core/server/helpers/template.js +++ b/core/server/helpers/template.js @@ -45,4 +45,25 @@ templates.getThemeViewForPost = function (themePaths, post) { return view; }; +// Given a theme object and a tag slug this will return +// which theme template page should be used. +// If no default or custom tag template exists then 'index' +// will be returned +// If no custom tag template exists but a default does then +// 'tag' will be returned +// If given a tag slug and a custom tag template +// exits it will return that view. +templates.getThemeViewForTag = function (themePaths, tag) { + var customTagView = 'tag-' + tag, + view = 'tag'; + + if (themePaths.hasOwnProperty(customTagView + '.hbs')) { + view = customTagView; + } else if (!themePaths.hasOwnProperty('tag.hbs')) { + view = 'index'; + } + + return view; +}; + module.exports = templates; \ No newline at end of file diff --git a/core/test/unit/server_helpers_template_spec.js b/core/test/unit/server_helpers_template_spec.js index cc2a11056b..55c3123591 100644 --- a/core/test/unit/server_helpers_template_spec.js +++ b/core/test/unit/server_helpers_template_spec.js @@ -52,4 +52,36 @@ describe('Helpers Template', function () { }); }); + + describe('getThemeViewForTag', function () { + var themePathsWithTagViews = { + 'assets': null, + 'default.hbs': '/content/themes/casper/default.hbs', + 'index.hbs': '/content/themes/casper/index.hbs', + 'tag.hbs': '/content/themes/casper/tag.hbs', + 'tag-design.hbs': '/content/themes/casper/tag-about.hbs' + }, + themePaths = { + 'assets': null, + 'default.hbs': '/content/themes/casper/default.hbs', + 'index.hbs': '/content/themes/casper/index.hbs' + }, + TAG_CUSTOM_EXISTS = 'design', + TAG_DEFAULT = 'development'; + + it('will return correct view for a tag', function () { + var view = template.getThemeViewForTag(themePathsWithTagViews, TAG_CUSTOM_EXISTS); + view.should.exist; + view.should.eql('tag-design'); + + view = template.getThemeViewForTag(themePathsWithTagViews, TAG_DEFAULT); + view.should.exist; + view.should.eql('tag'); + + view = template.getThemeViewForTag(themePaths, TAG_DEFAULT); + view.should.exist; + view.should.eql('index'); + }); + + }); }); \ No newline at end of file