mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-30 21:40:39 +03:00
✨New {{concat}} helper
- allows for concatenating strings using helpers and variables like {{concat (url) "?query=" slug}}
This commit is contained in:
parent
ff4d3f9e61
commit
8c03c3a0df
8
core/frontend/helpers/concat.js
Normal file
8
core/frontend/helpers/concat.js
Normal file
@ -0,0 +1,8 @@
|
||||
const {SafeString} = require('./proxy');
|
||||
|
||||
module.exports = function concat(...args) {
|
||||
const options = args.pop();
|
||||
const separator = options.hash.separator || '';
|
||||
|
||||
return new SafeString(args.join(separator));
|
||||
};
|
@ -8,6 +8,7 @@ coreHelpers.asset = require('./asset');
|
||||
coreHelpers.author = require('./author');
|
||||
coreHelpers.authors = require('./authors');
|
||||
coreHelpers.body_class = require('./body_class');
|
||||
coreHelpers.concat = require('./concat');
|
||||
coreHelpers.content = require('./content');
|
||||
coreHelpers.date = require('./date');
|
||||
coreHelpers.encode = require('./encode');
|
||||
@ -43,6 +44,7 @@ registerAllCoreHelpers = function registerAllCoreHelpers() {
|
||||
registerThemeHelper('author', coreHelpers.author);
|
||||
registerThemeHelper('authors', coreHelpers.authors);
|
||||
registerThemeHelper('body_class', coreHelpers.body_class);
|
||||
registerThemeHelper('concat', coreHelpers.concat);
|
||||
registerThemeHelper('content', coreHelpers.content);
|
||||
registerThemeHelper('date', coreHelpers.date);
|
||||
registerThemeHelper('encode', coreHelpers.encode);
|
||||
|
98
core/test/unit/helpers/concat_spec.js
Normal file
98
core/test/unit/helpers/concat_spec.js
Normal file
@ -0,0 +1,98 @@
|
||||
const should = require('should');
|
||||
const helpers = require.main.require('core/frontend/helpers');
|
||||
const handlebars = require.main.require('core/frontend/services/themes/engine').handlebars;
|
||||
|
||||
const configUtils = require('../../utils/configUtils');
|
||||
|
||||
let defaultGlobals;
|
||||
|
||||
function compile(templateString) {
|
||||
const template = handlebars.compile(templateString);
|
||||
template.with = (locals = {}, globals) => {
|
||||
globals = globals || defaultGlobals;
|
||||
|
||||
return template(locals, globals);
|
||||
};
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
describe('{{concat}} helper', function () {
|
||||
before(function () {
|
||||
handlebars.registerHelper('concat', helpers.concat);
|
||||
handlebars.registerHelper('url', helpers.url);
|
||||
configUtils.config.set('url', 'https://siteurl.com');
|
||||
|
||||
defaultGlobals = {
|
||||
data: {
|
||||
site: {
|
||||
url: configUtils.config.get('url')
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
it('can correctly concat nothing', function () {
|
||||
compile('{{concat}}')
|
||||
.with({})
|
||||
.should.eql('');
|
||||
});
|
||||
|
||||
it('can correctly concat things that resolve to empty', function () {
|
||||
compile('{{concat tag.slug slug}}')
|
||||
.with({tag: {}})
|
||||
.should.eql('');
|
||||
});
|
||||
|
||||
it('can concat simple strings', function () {
|
||||
compile('{{concat "hello" "world"}}')
|
||||
.with({})
|
||||
.should.eql('helloworld');
|
||||
});
|
||||
|
||||
it('can concat simple strings with a custom separator', function () {
|
||||
compile('{{concat "hello" "world" separator=" "}}')
|
||||
.with({})
|
||||
.should.eql('hello world');
|
||||
});
|
||||
|
||||
it('can concat strings and numbers', function () {
|
||||
compile('{{concat "abcd" 1234}}')
|
||||
.with({})
|
||||
.should.eql('abcd1234');
|
||||
});
|
||||
|
||||
it('can concat strings and global variables', function () {
|
||||
compile('{{concat @site.url "?my=param"}}')
|
||||
.with({})
|
||||
.should.eql('https://siteurl.com?my=param');
|
||||
});
|
||||
|
||||
it('can concat strings and local variables', function () {
|
||||
compile('{{concat tag.slug "?my=param"}}')
|
||||
.with({tag: {slug: 'my-tag'}})
|
||||
.should.eql('my-tag?my=param');
|
||||
});
|
||||
|
||||
it('can concat strings from custom helpers (SafeStrings)', function () {
|
||||
compile('{{concat (url) "?my=param"}}')
|
||||
// Simulate a post - using a draft to prove url helper gets called
|
||||
// because published posts get their urls from a cache that we don't have access to, so we just get 404
|
||||
.with({title: 'My Draft Post', slug: 'my-post', html: '<p>My Post</p>', uuid: '1234'})
|
||||
.should.eql('/p/1234/?my=param');
|
||||
});
|
||||
|
||||
it('can concat mixed args', function () {
|
||||
compile('{{concat @site.url (url) "?slug=" slug}}')
|
||||
// Simulate a post - using a draft to prove url helper gets called
|
||||
// because published posts get their urls from a cache that we don't have access to, so we just get 404
|
||||
.with({title: 'My Draft Post', slug: 'my-post', html: '<p>My Post</p>', uuid: '1234'})
|
||||
.should.eql('https://siteurl.com/p/1234/?slug=my-post');
|
||||
});
|
||||
|
||||
it('will output object Object for sill args', function () {
|
||||
compile('{{concat @site "?my=param"}}')
|
||||
.with({})
|
||||
.should.eql('[object Object]?my=param');
|
||||
});
|
||||
});
|
@ -8,7 +8,7 @@ var should = require('should'),
|
||||
describe('Helpers', function () {
|
||||
var hbsHelpers = ['each', 'if', 'unless', 'with', 'helperMissing', 'blockHelperMissing', 'log', 'lookup', 'block', 'contentFor'],
|
||||
ghostHelpers = [
|
||||
'asset', 'author', 'authors', 'body_class', 'content', 'date', 'encode', 'excerpt', 'facebook_url', 'foreach', 'get',
|
||||
'asset', 'author', 'authors', 'body_class', 'concat', 'content', 'date', 'encode', 'excerpt', 'facebook_url', 'foreach', 'get',
|
||||
'ghost_foot', 'ghost_head', 'has', 'img_url', 'is', 'lang', 'meta_description', 'meta_title', 'navigation',
|
||||
'next_post', 'page_url', 'pagination', 'plural', 'post_class', 'prev_post', 'reading_time', 't', 'tags', 'title', 'twitter_url',
|
||||
'url'
|
||||
|
Loading…
Reference in New Issue
Block a user