# How to create a Handlebar helper function

**URL:** https://meta.discourse.org/t/how-to-create-a-handlebar-helper-function/214407
**Category:** Development
**Created:** [January 10, 2022, 4:26pm UTC](https://meta.discourse.org/t/how-to-create-a-handlebar-helper-function/214407 "2022-01-10T16:26:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Kumar\_Karuppaiah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kumar_karuppaiah/32/243669_2.png) [@Kumar\_Karuppaiah](https://meta.discourse.org/u/Kumar_Karuppaiah)
#### Post date: [January 10, 2022, 4:26pm UTC](https://meta.discourse.org/t/how-to-create-a-handlebar-helper-function/214407/1 "2022-01-10T16:26:22Z")

</div>

Hi,

I want to create a new helper function which can be across the application. I am not sure how to create a helper function.

Likewise, I tried myself to create a helper function, but it is not working.

Please find my code.

 ![image](https://global.discourse-cdn.com/meta/original/3X/6/5/651ecddac54952229d43c140f9ff3d32ecb89804.png)

I am referring to this helper function in .hbr file. Please find the attached screenshot.

 ![image](https://global.discourse-cdn.com/meta/original/3X/f/0/f072ef903e5580277822ac8811c3568ef068e256.png)

Please suggest me.

Thanks  
Kumar

---

<div class="post-metadata">

### Author: ![Kumar\_Karuppaiah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kumar_karuppaiah/32/243669_2.png) [@Kumar\_Karuppaiah](https://meta.discourse.org/u/Kumar_Karuppaiah)
#### Post date: [January 11, 2022, 9:47am UTC](https://meta.discourse.org/t/how-to-create-a-handlebar-helper-function/214407/2 "2022-01-11T09:47:12Z")

</div>

Can anyone please help me on this?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [January 11, 2022, 10:48am UTC](https://meta.discourse.org/t/how-to-create-a-handlebar-helper-function/214407/3 "2022-01-11T10:48:21Z")

</div>

Did you see [Developer’s guide to Discourse Themes](https://meta.discourse.org/t/developer-s-guide-to-discourse-themes/93648)?

What I usually do is look at existing themes or plugins for examples.

EDIT: But if you’re lucky @Johani will provide a really thoughtful and helpful answer. And you’re lucky!

---

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [January 11, 2022, 1:23pm UTC](https://meta.discourse.org/t/how-to-create-a-handlebar-helper-function/214407/4 "2022-01-11T13:23:49Z")

</div>

Your code should work, but you’re using your helper

```Handlebars
{{sample}}

```

in the template without passing it any arguments. Even if you’re testing to see if things work, pass it a test string like

```Handlebars
{{sample "some string"}}

```

You need to do that because you’re using the helper in a raw `hbr` template. For testing purposes, the helper should work even if you don’t pass it anything in regular `hbs` templates.

If you’re creating a block-helper, then it needs to have `#` and you need to close it like so.

```Handlebars
{{#sample}}

{{/sample}}

```

Adding `~` to your helper optionally trims whitespace between any HTML tags it returns. So

```Handlebars
{{~#sample}}

{{/sample}}

```

---

<div class="post-metadata">

### Author: ![Kumar\_Karuppaiah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kumar_karuppaiah/32/243669_2.png) [@Kumar\_Karuppaiah](https://meta.discourse.org/u/Kumar_Karuppaiah)
#### Post date: [January 12, 2022, 6:59am UTC](https://meta.discourse.org/t/how-to-create-a-handlebar-helper-function/214407/5 "2022-01-12T06:59:29Z")

</div>

Thank you so much for your reply. It is working fine now,

---

<div class="post-metadata">

### Author: ![Kumar\_Karuppaiah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kumar_karuppaiah/32/243669_2.png) [@Kumar\_Karuppaiah](https://meta.discourse.org/u/Kumar_Karuppaiah)
#### Post date: [January 12, 2022, 7:13am UTC](https://meta.discourse.org/t/how-to-create-a-handlebar-helper-function/214407/6 "2022-01-12T07:13:54Z")

</div>

Hi,

Thank you for your reply. You are helping me a lot.

Yes, I have already gone through the documentation, existing themes and few examples, and created this sample project. But I am not sure about how to call the helper function in handlebar file. I tried it in multiple ways, and it was not working. That’s why, I posted here.

Thanks  
Kumar

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [January 12, 2022, 12:13pm UTC](https://meta.discourse.org/t/how-to-create-a-handlebar-helper-function/214407/7 "2022-01-12T12:13:49Z")

</div>

Your question helped me too, thanks to @Johani! I’m going to look at refactoring some of my code as a result.
