Skip to content

Instantly share code, notes, and snippets.

@toanz
Forked from tdack/README.md
Created October 19, 2020 07:52
Show Gist options
  • Save toanz/fdbf1cf44917e66e14f704beeb20c708 to your computer and use it in GitHub Desktop.
Save toanz/fdbf1cf44917e66e14f704beeb20c708 to your computer and use it in GitHub Desktop.

Revisions

  1. @tdack tdack created this gist Jun 12, 2016.
    25 changes: 25 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # Custom Handlebars Helpers for Ghost
    This is a Ghost "App" that will implement a custom Handlebars
    helper the same as https://apatchofcode.com/adding-custom-handlebars-for-ghost-equals-awesome/

    ## Installation
    Create a new directory in `contents/apps`

    eg:
    > `mkdir contents/apps/myhelpers`
    Place `package.json` and `index.js` in the directory.

    To enable the app you need to manually add it to the database as described at: https://github.com/TryGhost/Ghost/wiki/Apps-Getting-Started-for-Ghost-Devs

    Restart ghost

    ## Usage
    Simply use the new helper in your template, eg:

    {{#compare 'apples' '===' 'oranges'}}
    <p>Totally not the case.<p>
    {{/compare}}
    {{#compare 'apples' 'typeof' 'string'}}
    <p>I think we're onto something.<p>
    {{/compare}}
    46 changes: 46 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    var App = require('ghost-app'),
    myHelpers;

    myHelpers = App.extend({

    install: function() {},

    uninstall: function() {},

    activate: function() {
    this.app.helpers.register('compare', this.compareHelper)
    },

    deactivate: function() {},

    compareHelper: function (v1, operator, v2, options) {
    switch (operator) {
    case '==':
    return (v1 == v2) ? options.fn(this) : options.inverse(this);
    case '===':
    return (v1 === v2) ? options.fn(this) : options.inverse(this);
    case '!=':
    return (v1 != v2) ? options.fn(this) : options.inverse(this);
    case '!==':
    return (v1 !== v2) ? options.fn(this) : options.inverse(this);
    case '<':
    return (v1 < v2) ? options.fn(this) : options.inverse(this);
    case '<=':
    return (v1 <= v2) ? options.fn(this) : options.inverse(this);
    case '>':
    return (v1 > v2) ? options.fn(this) : options.inverse(this);
    case '>=':
    return (v1 >= v2) ? options.fn(this) : options.inverse(this);
    case '&&':
    return (v1 && v2) ? options.fn(this) : options.inverse(this);
    case '||':
    return (v1 || v2) ? options.fn(this) : options.inverse(this);
    case 'typeof':
    return (typeof v1 == v2) ? options.fn(this) : options.inverse(this);
    default:
    return options.inverse(this);
    }
    }
    });

    module.exports = myHelpers;
    12 changes: 12 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    {
    "name":"my-helpers",
    "version": "0.0.1",
    "dependencies": {
    "ghost-app": "0.0.2"
    },
    "ghost": {
    "permissions": {
    "helpers": ["compare"]
    }
    }
    }