简体   繁体   中英

Handlebars.js wont compile/output my template/data

I'm playing around with Handlebars.js and trying to compile a small template. I am only using Handlebars.js and nothing more. I am working locally on my computer, and not via any form of server.

my "html" looks like this. nothing special.

<!doctype html>
<html>
    <head>
        <script src="js/vendor/handlebars.min-4.0.4.js"></script>
    </head>
    <body>
        <script id="menu-template" type="text/x-handlebars-template">
            <h1>{{title}}</h1>
        </script>


        <section id="menu">

        </section>


        <script src="js/menu.js"></script>
    </body>
</html>

And my JS is

var source = document.getElementById('menu-template').innerHTML;
var template = Handlebars.compile(source);
var data = {title: "test"};

document.getElementById('menu').innerHTML = template(data);

It seems that template() does not return anything, i don't get any result except what looks like a empty string.

And i have been playing around with this for a few hours and searching the internet for answers but no result.

Am i doing anything wrong here?

// Edit added the rest of the html page.

The code seems correct just make sure the Handlebars.js file is properly included before the existing script tag.

Ref for Handlebars CDN : https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.4/handlebars.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM