1

I want to use Handlebars to add content to emails before they get sent, all of this happening on the server. We are using Dreamfactory and we can run scripts in Javascript to pre/post-process requests coming from the client (example: http://wiki.dreamfactory.com/DreamFactory/Tutorials/V8_custom_scripting_service_math)

I have uploaded handlebars-v4.0.11.js to the server, renamed it handlebars.js, and it fails on the first example given on the Handlebars website. Here is the code:

var Handlebars = require('../../../handlebars.js'); 

var source = `<div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
</div>`;

var template = Handlebars.compile(source);

var context = {
  title: 'My New Post',
  body: 'This is my first post!'
};

var html = template(context);

It fails with the message "Unknown template object: object"

What could I do?

Aurelien Giraud
  • 128
  • 2
  • 12
  • As you are assigning the `source` with the HTML block, it is considered as a string. You may wrap your HTML code inside `` and then assign the `source` as `var source = document.getElementById("entry-template").innerHTML;`. Hope this helps. – Gibin Ealias Feb 21 '18 at 12:10
  • Also please make sure the path `../../../handlebars.js` is set correct. – Gibin Ealias Feb 21 '18 at 12:11
  • @GibinEalias Thank you. 1. The path is correct, otherwise I get another error. 2. This is not running in the browser, so there is no `document` to add a script to and from which I would get some innerHTML. 3. I have run the same code locally, and this works, so I do not understand why it doesn't on the server. – Aurelien Giraud Feb 21 '18 at 13:58
  • Strange! Are you sure your whole code is wrapped inside a ? – Gibin Ealias Feb 21 '18 at 14:23
  • Also could you try removing the first line and use `` instead. – Gibin Ealias Feb 21 '18 at 14:23
  • @AurelienGiraud You should mention that you opened an [issue at the Handlebars project](https://github.com/wycats/handlebars.js/issues/1425). – Nils Knappmeier Feb 21 '18 at 20:43
  • @GibinEalias as I understood it, this code is executed on the server side inside DreamFactory, but inside a V8 javascript engine. – Nils Knappmeier Feb 21 '18 at 20:46
  • @NilsKnappmeier Oups! I did it there about here and forgot to do it the other way around, sorry. And, that's right, the code runs inside Dreamfactory with a V8 javascript engine. – Aurelien Giraud Feb 22 '18 at 15:46

0 Answers0