How to register a helper in Discourse?

AI-generated summary

The discussion revolves around registering a helper in Discourse. stevenpslade initially sought to register a handlebars helper for his plugin, but zogstrip suggested a simpler approach by using Discourse.ajax in the initializer and setting a property on the currentUser object. This solution worked for stevenpslade. Later, leog asked if it’s possible to register a helper without creating a plugin, and riking replied that it can be achieved using a theme component.

I’m trying to figure out the best way to register a handlebars helper for my Discourse plugin.

I have an ajax call in my initializer that determines whether the currentUser is part of a particular group. If they are, then they get an extra nav item.

I wanted to do something like this in my hbs file for the nav:

{{#if customHelper}}
//show nav item
{{/if}}

My other theories on how to do this:

  1. Keep the logic in the initializer and use jquery to append the item to the nav.
  2. Create a component for the nav item to only be displayed given the aforementioned logic.

So do you think registering a helper is the best to go, if so how? Or are one of my other approached better.

Thanks for any help or advise!

What would customHelper do? Can’t you just do something like this instead?

in your initializer

export default {
  initialize(container) {
    const user = container.lookup('current-user:main');
    Discourse.ajax("/some-url").then(result => user.set("isPartOfParticularGroup", true));
  }
}

and in your .hbs file

{{#if currentUser.isPartOfParticularGroup}}
   // your stuff
{{/if}

Wow so simple and works perfectly!

이 주제를 다시 꺼내는 건 죄송하지만, 제 질문과 완벽하게 맞아서요.

플러그인을 만들지 않고 헬퍼를 등록할 수 있을까요? 예를 들어 테마의 HTML head/body 섹션에서 스크립트로 등록하는 방식처럼요.

미리 감사합니다!

일반적으로 테마 컴포넌트를 사용하여 그렇게 하시죠.