# Escaping handlebars in template

**URL:** https://community.silverbullet.md/t/escaping-handlebars-in-template/1694
**Category:** General
**Created:** [January 25, 2025, 4:08pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694 "2025-01-25T16:08:52Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![viktomas](https://community.silverbullet.md/user_avatar/community.silverbullet.md/viktomas/32/377_2.png) [@viktomas](https://community.silverbullet.md/u/viktomas)
#### Post date: [January 25, 2025, 4:08pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694/1 "2025-01-25T16:08:52Z")

</div>

Is there a way to escape handlebars in **page templates** in a way that will unescape them once the template is applied?

I have this code as a part of my weekly note template. The `weeklyJournalSummary` is my custom space script function.

`````auto
```template
\{\{weeklyJournalSummary("{{weekStart}}")\}\}
````

`````

I would like the the page generated from this template to look like

`````auto
```template
{{weeklyJournalSummary("2025-01-20")}}
````

`````

But the backslashes stay in the template resulting in

`````auto
```template
\{\{weeklyJournalSummary("2025-01-20")\}\}
````

`````

This means I have to manually remove the backslashes after creating a new weekly note.

Does anybody have an idea how to unescape the handlebars?

I looked at and around the template language docs but haven’t found the answer [Template Language](https://silverbullet.md/Template%20Language)

PS: The reason I’m passing in the week start is that the live templating apparently doesn’t support the editor page name syscalls so I can’t ask for the page name of the weekly note.

---

<div class="post-metadata">

### Author: ![Hckr1123](https://community.silverbullet.md/user_avatar/community.silverbullet.md/hckr1123/32/915_2.png) [@Hckr1123](https://community.silverbullet.md/u/Hckr1123)
#### Post date: [January 25, 2025, 4:27pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694/2 "2025-01-25T16:27:34Z")

</div>

> [@viktomas](#):
>
> ````auto
> ```template
> \{\{weeklyJournalSummary("{{weekStart}}")\}\}
> 
> ````
> 
> ```auto
> 
> ```

I tired that snippet you shared and on my side it rendered like this:  
 ![image](https://community.silverbullet.md/uploads/default/original/1X/ef50f01c67cbb4732716ad3d6cf1c7dea2690bdb.png)

Seems to be working, is this what you were expecting/looking for?

---

<div class="post-metadata">

### Author: ![viktomas](https://community.silverbullet.md/user_avatar/community.silverbullet.md/viktomas/32/377_2.png) [@viktomas](https://community.silverbullet.md/u/viktomas)
#### Post date: [January 25, 2025, 5:01pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694/3 "2025-01-25T17:01:10Z")

</div>

Thanks for the response @Hckr1123 🙏

In my use-case, I don’t want the function to render, I want it to execute.

In other words:

If I use the following page template as a command:

````auto
---
description: "Open your weekly note page"
tags: template
hooks.newPage:
  suggestedName: "tmp/{{weekStart}}"
  confirmName: true
  openIfExists: true
  command: "Test Weekly Note"
---
## Daily notes

```template
\{\{weeklyJournalSummary("{{weekStart}}")\}\}
```

````

I get the following page:

````auto
## Daily notes

```template
\{\{weeklyJournalSummary("2025-01-20")\}\}
```

````

Notice how the backslashes have been preserved in the output.

The template renders as a simple string:

 ![image](https://community.silverbullet.md/uploads/default/original/1X/4d65975c739e997593dfda7a096fb52d1380c7f5.png)

Whilst I need the page to be

````auto
## Daily notes

```template
{{weeklyJournalSummary("2025-01-20")}}
```

````

(notice the missing backslashes)

So that it renders using the space script function

 ![image](https://community.silverbullet.md/uploads/default/original/1X/c543cd48267030d07fb4f14813300a662a303315.png)

---

<div class="post-metadata">

### Author: ![Hckr1123](https://community.silverbullet.md/user_avatar/community.silverbullet.md/hckr1123/32/915_2.png) [@Hckr1123](https://community.silverbullet.md/u/Hckr1123)
#### Post date: [January 25, 2025, 6:28pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694/4 "2025-01-25T18:28:22Z")

</div>

Just to know whether I understand you correctly, are you trying to place a template inside a page template so that the inner template gets rendered on all new pages created using the page template?

If you place `{{weeklyJournalSummary("2025-01-20")}}` directly in the page template, without the ````template` enclosure, that should render the `weeklyJournalSummary()` function on all pages created using this page template. Is that what you are trying to achieve?

---

<div class="post-metadata">

### Author: ![bent0n](https://community.silverbullet.md/user_avatar/community.silverbullet.md/bent0n/32/604_2.png) [@bent0n](https://community.silverbullet.md/u/bent0n)
#### Post date: [January 25, 2025, 9:28pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694/5 "2025-01-25T21:28:45Z")

</div>

You need to use `escapeDirective` for this.

More details [here](https://community.silverbullet.md/t/keep-raw-querry-on-new-page-from-template/981/2).

---

<div class="post-metadata">

### Author: ![viktomas](https://community.silverbullet.md/user_avatar/community.silverbullet.md/viktomas/32/377_2.png) [@viktomas](https://community.silverbullet.md/u/viktomas)
#### Post date: [January 30, 2025, 7:04pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694/6 "2025-01-30T19:04:36Z")

</div>

@bent0n This is it! thank you 🙏

@Hckr1123 Also thank you very much for your help 🙇‍♂️

---

<div class="post-metadata">

### Author: ![viktomas](https://community.silverbullet.md/user_avatar/community.silverbullet.md/viktomas/32/377_2.png) [@viktomas](https://community.silverbullet.md/u/viktomas)
#### Post date: [January 30, 2025, 7:44pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694/7 "2025-01-30T19:44:09Z")

</div>

@bent0n darn, I take it back. `escapeDirective` doesn’t solve the problem AFAICT since I still face the issue with nested handlebars.

The issue is that I need **the inner handlebars** to be evaluated at the time when I turn the page template into a page.

In other words,`escapeDirective(weeklyJournalSummary("{{weekStart}}"))` doesn’t work and I need the `weekStart` to turn into `2025-01-20` **when the template becomes a page** , otherwise the `weekStart` will be dynamic and after a week I see different results in my old weekly note.

I’m starting to thing that I’m approaching this from a wrong angle and I probably don’t know enough about the template language and the syscalls to find a more elegant solution.

EDIT: I can’t render the whole `weeklyJournalSummary` when turn the template into a page because I create the weekly page on Monday, but I want the query to be dynamically updated for the whole week.

---

<div class="post-metadata">

### Author: ![bent0n](https://community.silverbullet.md/user_avatar/community.silverbullet.md/bent0n/32/604_2.png) [@bent0n](https://community.silverbullet.md/u/bent0n)
#### Post date: [January 30, 2025, 7:56pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694/8 "2025-01-30T19:56:54Z")

</div>

You could make the weekstart remain dynamic but have the output dependent on the filename to solve your issue? It seems you name the file with the date your interested in…

edit: Or even easier, use the filename as input to your weeklySummary function

---

<div class="post-metadata">

### Author: ![viktomas](https://community.silverbullet.md/user_avatar/community.silverbullet.md/viktomas/32/377_2.png) [@viktomas](https://community.silverbullet.md/u/viktomas)
#### Post date: [January 30, 2025, 8:06pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694/9 "2025-01-30T20:06:57Z")

</div>

@bent0n That was the first thing I wanted to do, but think that live templates don’t support the syscall

> PS: The reason I’m passing in the week start is that the live templating apparently doesn’t support the editor page name syscalls so I can’t ask for the page name of the weekly note.

I tried it again and when I use

```js
const date = await editor.getCurrentPage();

```

in my spacescript function

```auto
Error rendering template Error: Unregistered syscall editor.getCurrentPage
    at data:application/javascript;base64,dmFyIERlPU9iamVjdC5k......dHtyaSBhcyBwbHVnfTsK:1:958
    at data:application/javascript;base64,dmFyIERlPU9iamVjdC5k......dHtyaSBhcyBwbHVnfTsK:1:1006
    at innerInvokeEventListeners (ext:deno_web/02_event.js:757:7)
    at invokeEventListeners (ext:deno_web/02_event.js:804:5)
    at dispatch (ext:deno_web/02_event.js:661:9)
    at dispatchEvent (ext:deno_web/02_event.js:1041:12)
    at pollForMessages (ext:runtime_main/js/99_main.js:218:7)
    at eventLoopTick (ext:core/01_core.js:175:7)

```

EDIT: This is the doc for the syscall [editor.getCurrentPage from syscalls - @silverbulletmd/silverbullet - JSR](https://jsr.io/@silverbulletmd/silverbullet/doc/syscalls/~/editor.getCurrentPage)

---

<div class="post-metadata">

### Author: ![bent0n](https://community.silverbullet.md/user_avatar/community.silverbullet.md/bent0n/32/604_2.png) [@bent0n](https://community.silverbullet.md/u/bent0n)
#### Post date: [January 30, 2025, 8:07pm UTC](https://community.silverbullet.md/t/escaping-handlebars-in-template/1694/10 "2025-01-30T20:07:56Z")

</div>

Try it in sync mode. I use the same syscall in my workflow and it works only in sync mode.
