Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to register custom Handlebars Helper in AEM 6.1. Documentation seems to be wrong.

Avatar

Level 1

I want to write custom Handlebars helper for the server side execution.

The documentation is extremely limited (2 lines on this page):

https://docs.adobe.com/docs/en/aem/6-1/develop/communities/scf/handlebars-helpers.html#Custom SCF Helpers

"... Server-side Helpers

To implement a helper on the server-side, it is necessary to

create an implementation of the com.adobe.cq.social.handlebars.api.TemplateHelper interface
register the helper with the com.adobe.cq.social.handlebars.api.TemplateHelpersService

..."

And the TemplateHelperService methods are already deprecated in AEM 6.1.

The SCF examples that can be found do not show how to write custom server-side handlebars helper, whereas for the client side it is quite clear.

Is there any real life example on how to write the helper and much more important best practice for registering them in AEM 6.1?

Will the documentation be updated and be given more details?

1 Accepted Solution

Avatar

Correct answer by
Level 9

There is no need to use the TemplateHelpersService any longer.

Simply implement the TemplateHelper interface, make it an OSGi Service and install it as part of an OSGi bundle.

See updated documentation (with example) at http://docs.adobe.com/docs/en/aem/6-1/develop/communities/scf/handlebars-helpers.html#Custom SCF Hel...

Let us know if you need more information.

- JK

View solution in original post

9 Replies

Avatar

Correct answer by
Level 9

There is no need to use the TemplateHelpersService any longer.

Simply implement the TemplateHelper interface, make it an OSGi Service and install it as part of an OSGi bundle.

See updated documentation (with example) at http://docs.adobe.com/docs/en/aem/6-1/develop/communities/scf/handlebars-helpers.html#Custom SCF Hel...

Let us know if you need more information.

- JK

Avatar

Level 1

Hi sinners97099773,

Is it working for you? I tried it but not working for me..

Avatar

Level 1

HI JK Kendall,

I have tried creating a custom server-side handlebar Helper. But its not working.

Getting this exception:java.io.IOException: Trying to use unregistered helper.

As you have mentioned that there is no need to register the helper then what to do in this case.

Avatar

Level 9

Hi Shiffali,

I asked whether it's required to create both client-side and server-side helpers and the answer was 'no'.

However, what was said was if you try to execute a template that uses the custom helper in an environment (server/client) where the helper is not available, then you would run into exceptions.

You would need to provide more details if this isn't helpful.

- JK

Avatar

Level 1

Hi JK Kendall,

As given in the documentation, i have created FooTextHelper class that implements TemplateHelper. When i tried to use this helper in a script, it is throwing an exception i.e

this helper is not registered.

Do we need to explicitly register this helper??

Avatar

Level 9

Hi,

I wanted to let you know that the problem with registering a custom helper has been reproduced and will be investigated.

- JK

Avatar

Level 1

Hi JK ,

We are able to register server side custom handle bar as OSGI service. When we are trying to invoke this handler using client side it's giving us exception.

"org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: /apps/hmi/components/content/social/forum/components/hbs/topic/list-item.hbs:19:14: could not find helper: 'foo-text' ".

Client Side Helper Invoking Code :

 Handlebars.registerHelper('foo-text', function(context, options) {
        if (!context) {
            return "";
        }
        return "foo-" + context;
    });

 

Do we need to call/adapt any other interface ?

 

BR

Rahul

Avatar

Level 9

An issue, CQ-74751, has been identified that will be fixed in the release following 6.1 FP4 and 6.2 GA.

The issue has to do with a custom OSGi embedding handlebars.jar, which conflicts with the internal handlebars.jar.

You may want to open a customer care support ticket.

- JK