SharePoint Framework webpart: add Handlerbars helper

Copper Contributor

I've created a sharepoint framework webpart from the pnp spfx yo template

 

I have want to add a Handlerbars helper so I can have an if statement in my handlerbar template

 

When I add into the render method something like:

 

Handlebars.registerHelper('check', (node)=>{return node!==null;});
 
I get the following error:
TypeError: external__handlebars_.registerHelper is not a function
 
I can only find sample of using handlerbars where they haven't implemented any helpers, can anyone guide me on what to do to be able to add helpers
 
Thanks
 
1 Reply
First, did you install Handlebars and imported it correctly? If so, can you provide your source code?
I was able to create a webpart using Handlebars and create your helper within minutes. here are the steps:

1. In your console, install Handlebars using npm i handlebars, for example.
2. In your webpart, reference it using "import * as Handlebars from 'handlebars';"
3. After that, you can create your helper just as you would using javascript itself

If you find any issue, let me know.