How to use @tryghost/helpers - 10 common examples

To help you get started, we’ve selected a few @tryghost/helpers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TryGhost / Ghost / core / frontend / helpers / foreach.js View on Github external
module.exports = function foreach(items, options) {
    if (!options) {
        logging.warn(i18n.t('warnings.helpers.foreach.iteratorNeeded'));
    }

    if (hbsUtils.isFunction(items)) {
        items = items.call(this);
    }

    // Exclude items which should not be visible in the theme
    items = ghostHelperUtils.visibility.filter(items, options.hash.visibility);

    // Initial values set based on parameters sent through. If nothing sent, set to defaults
    const {fn, inverse, hash, data, ids} = options;
    let {columns, limit, from, to} = hash;
    let length = _.size(items);
    let output = '';
    let frame;
    let contextPath;

    limit = parseInt(limit, 10) || length;
    from = parseInt(from, 10) || 1;
    to = parseInt(to, 10) || length;

    // If a limit option was sent through (aka not equal to default (length))
    // and from plus limit is less than the length, set to to the from + limit
    if ((limit < length) && ((from + limit) <= length)) {
github chadly / ghost / core / frontend / helpers / foreach.js View on Github external
module.exports = function foreach(items, options) {
    if (!options) {
        logging.warn(i18n.t('warnings.helpers.foreach.iteratorNeeded'));
    }

    if (hbsUtils.isFunction(items)) {
        items = items.call(this);
    }

    // Exclude items which should not be visible in the theme
    items = ghostHelperUtils.visibility.filter(items, options.hash.visibility);

    // Initial values set based on parameters sent through. If nothing sent, set to defaults
    const {fn, inverse, hash, data, ids} = options;
    let {columns, limit, from, to} = hash;
    let length = _.size(items);
    let output = '';
    let frame;
    let contextPath;

    limit = parseInt(limit, 10) || length;
    from = parseInt(from, 10) || 1;
    to = parseInt(to, 10) || length;

    // If a limit option was sent through (aka not equal to default (length))
    // and from plus limit is less than the length, set to to the from + limit
    if ((limit < length) && ((from + limit) <= length)) {
github chadly / ghost / core / frontend / helpers / authors.js View on Github external
'use strict';
// # Authors Helper
// Usage: `{{authors}}`, `{{authors separator=' - '}}`
//
// Returns a string of the authors on the post.
// By default, authors are separated by commas.
//
// Note that the standard {{#each authors}} implementation is unaffected by this helper.
const proxy = require('./proxy');
const _ = require('lodash');
const urlService = require('../services/url');
const {SafeString, templates} = proxy;
const ghostHelperUtils = require('@tryghost/helpers').utils;

module.exports = function authors(options = {}) {
    options.hash = options.hash || {};

    let {
        autolink,
        separator = ', ',
        prefix = '',
        suffix = '',
        limit,
        visibility,
        from = 1,
        to
    } = options.hash;
    let output = '';
github chadly / ghost / core / frontend / helpers / foreach.js View on Github external
// # Foreach Helper
// Usage: `{{#foreach data}}{{/foreach}}`
//
// Block helper designed for looping through posts
const _ = require('lodash');
const {logging, i18n, hbs} = require('./proxy');
const {Utils: hbsUtils, handlebars: {createFrame}} = hbs;
const ghostHelperUtils = require('@tryghost/helpers').utils;

module.exports = function foreach(items, options) {
    if (!options) {
        logging.warn(i18n.t('warnings.helpers.foreach.iteratorNeeded'));
    }

    if (hbsUtils.isFunction(items)) {
        items = items.call(this);
    }

    // Exclude items which should not be visible in the theme
    items = ghostHelperUtils.visibility.filter(items, options.hash.visibility);

    // Initial values set based on parameters sent through. If nothing sent, set to defaults
    const {fn, inverse, hash, data, ids} = options;
    let {columns, limit, from, to} = hash;
github TryGhost / Ghost / core / frontend / helpers / tags.js View on Github external
// # Tags Helper
// Usage: `{{tags}}`, `{{tags separator=' - '}}`
//
// Returns a string of the tags on the post.
// By default, tags are separated by commas.
//
// Note that the standard {{#each tags}} implementation is unaffected by this helper
const proxy = require('./proxy');
const _ = require('lodash');
const ghostHelperUtils = require('@tryghost/helpers').utils;

const urlService = proxy.urlService;
const SafeString = proxy.SafeString;
const templates = proxy.templates;

module.exports = function tags(options) {
    options = options || {};
    options.hash = options.hash || {};

    const autolink = !(_.isString(options.hash.autolink) && options.hash.autolink === 'false'),
        separator = _.isString(options.hash.separator) ? options.hash.separator : ', ',
        prefix = _.isString(options.hash.prefix) ? options.hash.prefix : '',
        suffix = _.isString(options.hash.suffix) ? options.hash.suffix : '',
        limit = options.hash.limit ? parseInt(options.hash.limit, 10) : undefined;

    let output = '',
github TryGhost / Ghost / core / frontend / helpers / authors.js View on Github external
'use strict';
// # Authors Helper
// Usage: `{{authors}}`, `{{authors separator=' - '}}`
//
// Returns a string of the authors on the post.
// By default, authors are separated by commas.
//
// Note that the standard {{#each authors}} implementation is unaffected by this helper.
const proxy = require('./proxy');
const _ = require('lodash');
const urlService = require('../services/url');
const {SafeString, templates} = proxy;
const ghostHelperUtils = require('@tryghost/helpers').utils;

module.exports = function authors(options = {}) {
    options.hash = options.hash || {};

    let {
        autolink,
        separator = ', ',
        prefix = '',
        suffix = '',
        limit,
        visibility,
        from = 1,
        to
    } = options.hash;
    let output = '';
github TryGhost / Ghost / core / frontend / helpers / foreach.js View on Github external
// # Foreach Helper
// Usage: `{{#foreach data}}{{/foreach}}`
//
// Block helper designed for looping through posts
const _ = require('lodash');
const {logging, i18n, hbs} = require('./proxy');
const {Utils: hbsUtils, handlebars: {createFrame}} = hbs;
const ghostHelperUtils = require('@tryghost/helpers').utils;

module.exports = function foreach(items, options) {
    if (!options) {
        logging.warn(i18n.t('warnings.helpers.foreach.iteratorNeeded'));
    }

    if (hbsUtils.isFunction(items)) {
        items = items.call(this);
    }

    // Exclude items which should not be visible in the theme
    items = ghostHelperUtils.visibility.filter(items, options.hash.visibility);

    // Initial values set based on parameters sent through. If nothing sent, set to defaults
    const {fn, inverse, hash, data, ids} = options;
    let {columns, limit, from, to} = hash;
github TryGhost / Ghost / core / frontend / helpers / reading_time.js View on Github external
module.exports = function reading_time(options) {// eslint-disable-line camelcase
    options = options || {};
    options.hash = options.hash || {};

    // only calculate reading time for posts
    if (!schema.isPost(this)) {
        return null;
    }

    let readingTime = calculateReadingTime(this, options.hash);

    return new SafeString(readingTime);
};
github chadly / ghost / core / frontend / helpers / reading_time.js View on Github external
module.exports = function reading_time(options) {// eslint-disable-line camelcase
    options = options || {};
    options.hash = options.hash || {};

    // only calculate reading time for posts
    if (!schema.isPost(this)) {
        return null;
    }

    let readingTime = calculateReadingTime(this, options.hash);

    return new SafeString(readingTime);
};
github TryGhost / Ghost / core / frontend / helpers / authors.js View on Github external
function createAuthorsList(authors) {
        function processAuthor(author) {
            return autolink ? templates.link({
                url: urlService.getUrlByResourceId(author.id, {withSubdirectory: true}),
                text: _.escape(author.name)
            }) : _.escape(author.name);
        }

        return ghostHelperUtils.visibility.filter(authors, visibility, processAuthor);
    }

@tryghost/helpers

Javascript Helpers for working with the Ghost [Content API](https://ghost.org/docs/content-api/)

MIT
Latest version published 7 months ago

Package Health Score

62 / 100
Full package analysis

Similar packages