Wednesday, 24 May 2017

Reuse partials for both server and client for handlebars.js?

I am using express-handlebars to render messages (a partial) on a chat page which is then served to the client.

Once the page has loaded, the user can type in a message and I would like it to reuse that same message html from my partial (that I used server side) and add it to the page (client side). I am not sure how to do this.

Currently, I have tried:

<script id="message" type="text/x-handlebars-template">
   
</script>

Doing the above results in the html showing up in view source but without the part, which means I cannot add text/context to my template.

For example, it would become

<script id="message" type="text/x-handlebars-template">
   <div class="message-bubble"></div>
</script>

instead of

<script id="message" type="text/x-handlebars-template">
   <div class="message-bubble"></div>
</script>

If I have \ inside my partial instead, it can then compile just fine with my new context client-side. This, however, breaks my server side usage of the partial because the context is now escaped.

Any help is appreciated. Thanks!



via C. Meadows

No comments:

Post a Comment