question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't figured out how to register handlebars-helper and other single helpers

See original GitHub issue

Hi, I’m trying to register handlebars-helper, handlebars-helper-repeat and a custom dump helper.

This is my last attempt:

const extra_helpers = require('handlebars-helpers')();
const repeat = require('handlebars-helper-repeat');
const hbs = require('@frctl/handlebars')({
  helpers: {
    repeat,
    dump: function(str) {
              return JSON.stringify(str);
          },
    extra_helpers
  },
 
});
fractal.components.engine(hbs);
fractal.docs.engine(hbs);

The error thrown, when I try to use, for example, ‘{{#contains …}}’ is:

ERROR RENDERING COMPONENT
Missing helper: "contains"

For me handlebars-helper works only alone defined like this:

const helpers = require('handlebars-helpers')();
const hbs = require('@frctl/handlebars')({
  helpers
});
fractal.components.engine(hbs);
fractal.docs.engine(hbs);

Can you help me?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
LeBenLeBencommented, Sep 8, 2018

extra_helpers is an object, when you do this:

const hbs = require('@frctl/handlebars')({
  helpers: {
    extra_helpers
  },
});

You actually do this:

const hbs = require('@frctl/handlebars')({
  helpers: {
    extra_helpers: {
      array: function() { … },
      code: function() { … },
      contains: function() { … },
      // All other `handlebars-helpers` helpers…
    }
  },
});

To make all those helpers available to your templates, you need to put them at the root of the helpers object. To do so, you can use the object spread syntax (Node ≥ 8.6):

const hbs = require('@frctl/handlebars')({
  helpers: {
    dump: function(str) {
      return JSON.stringify(str);
    },
    ...extra_helpers
  },
});

If you use an earlier version of Node, you can use Object.assign instead:

const helpers = Object.assign({
  dump: function(str) {
    return JSON.stringify(str);
  }
}, extra_helpers);
const hbs = require('@frctl/handlebars')({
  helpers,
});
0reactions
Maybach91commented, Feb 6, 2019

// EDIT: nevermind, i have no clue what happend. but right now everything works.

hej @LeBenLeBen ,

thanks for your help! i restarted it of course but i got an error on my page, thats its not able to export the url:

✘ Failed to export url /components/detail/atom-button--default - (/src/resources/fractal/themes/themeName/views/pages/components/detail.nunj)
  Template render error: (/src/resources/fractal/themes/themeName/views/partials/pen/browser.nunj)
  Template render error: (/src/resources/fractal/themes/themeName/views/partials/browser/browser.nunj)
  Template render error: (/src/resources/fractal/themes/themeName/views/partials/browser/panel-notes.nunj) [Line 5, Column 40]

(this error occur only when i try to use handlebar helpers, which are not there. so the panel-notes.nunj is fine)

my used versions:

    "@frctl/fractal": "^1.1.7",
    "handlebars-helpers": "^0.10.0",
    "handlebars-layouts": "^3.1.4",

const fractal = module.exports = require('@frctl/fractal').create();
const extraHelpers = require('handlebars-helpers')(fractal); // https://www.npmjs.com/package/handlebars-helpers

const hbs = require('@frctl/handlebars')({
  helpers: {
    splitName(stringName) {
      const t = stringName.split(' / ');
      return t[0];
    },
    ...extraHelpers,
  },
});
fractal.docs.set('engine', hbs);
fractal.docs.set('path', `${cfg.src.base}docs`);

the variable extraHelpers is defined with all the functions. but the engine has all the helpers added console.log(fractal.docs.engine(hbs)):

HandlebarsEnvironment {
  helpers:
   { blockHelperMissing: [Function],
     each: [Function],
     helperMissing: [Function],
     if: [Function],
     [...]
  }
}

i don’t know if the “helper missing” stuff related to my issue. but i think we are using the same handlebar & fractal version, right?

Read more comments on GitHub >

github_iconTop Results From Across the Web

can't register handlebar helpers - Stack Overflow
You should register the Handlebar helpers. Posting my full code: const express = require('express'); const exphbs ...
Read more >
Built-in Helpers - Handlebars
You can write any helper and use it in a sub-expression. For example, in checking for initialization of a variable the built-in #if...
Read more >
Create Custom Helper - Express Handlebars - #10 - YouTube
Step by step video on how to create custom helper with Express HandlebarsInterested in ... Your browser can't play this video. Learn more....
Read more >
10 Handlebars training: Custom Helpers Part 1 - Expressions
Your browser can't play this video. Learn more. Switch camera.
Read more >
Helpers in Handlebars.java
There are two way of registering helpers in Handlebars.java: Registering one ... Now you know how to register a helper, let's see what...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found