mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-04-15 03:01:37 -05:00
✨ Added raw handlebars helper
- Allows using the 4-bracket raw block syntax e.g: {{{{raw}}}}{{{{/raw}}}} - This allows you to include handlebars inside a template that is not compiled and executed - The common usecase is if you want to include client-side handlebars templates inside server-side ones
This commit is contained in:
parent
4ee0b92eac
commit
67b8fbf6cf
4 changed files with 51 additions and 1 deletions
|
@ -38,6 +38,7 @@ coreHelpers.post_class = require('./post_class');
|
|||
coreHelpers.prev_post = require('./prev_next');
|
||||
coreHelpers.price = require('./price');
|
||||
coreHelpers.next_post = require('./prev_next');
|
||||
coreHelpers.raw = require('./raw');
|
||||
coreHelpers.reading_time = require('./reading_time');
|
||||
coreHelpers.t = require('./t');
|
||||
coreHelpers.tags = require('./tags');
|
||||
|
@ -86,6 +87,7 @@ registerAllCoreHelpers = function registerAllCoreHelpers() {
|
|||
registerThemeHelper('plural', coreHelpers.plural);
|
||||
registerThemeHelper('post_class', coreHelpers.post_class);
|
||||
registerThemeHelper('price', coreHelpers.price);
|
||||
registerThemeHelper('raw', coreHelpers.raw);
|
||||
registerThemeHelper('reading_time', coreHelpers.reading_time);
|
||||
registerThemeHelper('t', coreHelpers.t);
|
||||
registerThemeHelper('tags', coreHelpers.tags);
|
||||
|
|
8
core/frontend/helpers/raw.js
Normal file
8
core/frontend/helpers/raw.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
// # Raw helper
|
||||
// Usage: `{{{{raw}}}}...{{{{/raw}}}}`
|
||||
//
|
||||
// Returns raw contents unprocessed by handlebars.
|
||||
|
||||
module.exports = function raw(options) {
|
||||
return options.fn(this);
|
||||
};
|
|
@ -10,7 +10,7 @@ describe('Helpers', function () {
|
|||
ghostHelpers = [
|
||||
'asset', 'author', 'authors', 'body_class', 'cancel_link', 'concat', 'content', 'date', 'encode', 'excerpt', 'facebook_url', 'foreach', 'get',
|
||||
'ghost_foot', 'ghost_head', 'has', 'img_url', 'is', 'lang', 'link', 'link_class', 'meta_description', 'meta_title', 'navigation',
|
||||
'next_post', 'page_url', 'pagination', 'plural', 'post_class', 'prev_post', 'price', 'reading_time', 't', 'tags', 'title', 'twitter_url',
|
||||
'next_post', 'page_url', 'pagination', 'plural', 'post_class', 'prev_post', 'price', 'raw', 'reading_time', 't', 'tags', 'title', 'twitter_url',
|
||||
'url'
|
||||
],
|
||||
expectedHelpers = _.concat(hbsHelpers, ghostHelpers);
|
||||
|
|
40
test/unit/helpers/raw_spec.js
Normal file
40
test/unit/helpers/raw_spec.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
const should = require('should');
|
||||
const helpers = require('../../../core/frontend/helpers');
|
||||
const handlebars = require('../../../core/frontend/services/themes/engine').handlebars;
|
||||
|
||||
let defaultGlobals;
|
||||
|
||||
function compile(templateString) {
|
||||
const template = handlebars.compile(templateString);
|
||||
template.with = (locals = {}, globals) => {
|
||||
globals = globals || defaultGlobals;
|
||||
|
||||
return template(locals, globals);
|
||||
};
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
describe('{{raw}} helper', function () {
|
||||
before(function () {
|
||||
handlebars.registerHelper('raw', helpers.raw);
|
||||
});
|
||||
|
||||
it('can correctly compile space', function () {
|
||||
compile('{{{{raw}}}} {{{{/raw}}}}')
|
||||
.with({})
|
||||
.should.eql(' ');
|
||||
});
|
||||
|
||||
it('can correctly ignore handlebars', function () {
|
||||
compile('{{{{raw}}}}{{test}}{{{{/raw}}}}')
|
||||
.with({tag: {}})
|
||||
.should.eql('{{test}}');
|
||||
});
|
||||
|
||||
it('can correctly compile recursive', function () {
|
||||
compile('{{{{raw}}}}{{{{raw}}}}{{{{/raw}}}}{{{{/raw}}}}')
|
||||
.with({tag: {}})
|
||||
.should.eql('{{{{raw}}}}{{{{/raw}}}}');
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue