Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars Pass a string or Handlebars AST to Handlebars compile

I know its been asked many times, I have looked at the answers and not sure where I am going wrong.

I have looked at the docs on Handlebarsjs and followed a tutorial and both times I am getting the same error.

<!DOCTYPE html>
<html>
  <head>
     <script src="handlebars-v1.3.0.js"></script>
     <script src="jquery.min.js"></script>
     <script src="test.js"></script>
  </head>
  <body>
    <script id="header" type="text/x-handlebars-template">
      div {{ headerTitle }} div
      Today is {{weekDay}}
    </script>
  </body>   
</html>

And this is my Javascript

var theData = {headerTitle:"name", weekDay:"monday"}
var theTemplateScript = $("#header").html();
var theTemplate = Handlebars.compile(theTemplateScript);
$(document.body).append(theTemplate(theData));

I keep on getting the following error and i am unsure why

Uncaught Error: You must pass a string or Handlebars AST to Handlebars.compile. 
You passed undefined 
like image 548
user3765218 Avatar asked Jun 22 '14 18:06

user3765218


People also ask

Is handlebars server side rendering?

Handlebars is good for rendering in CLI-apps, non-HTML text content, server-side rendering of pure contents. Handlebars has been ported to many programming languages (Java, Rust etc).

What are handlebars helpers?

Custom Helpers You can create your own helpers to perform complex logics using the expression system that Handlebars provides. There are two kinds of helpers: function helpers and block helpers. The first definition is meant for a single expression, while the latter is used for block expressions.

How do you define handlebars?

Definition of handlebar : a straight or bent bar with a handle at each end specifically : one used to steer a bicycle or similar vehicle —usually used in plural.


1 Answers

You are running Handlebars.compile() before theTemplateScript is loaded into the DOM. Move your test.js down below the template script and you should be good to go.

like image 157
Rob_vH Avatar answered Sep 16 '22 13:09

Rob_vH