vendredi 1 juin 2018

How to use variable in Handlebars?

My question is: How can i use variable ime in Handlebars template(sadrzaj-template)?

My HTML code:

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>2 kolokvijum</title>
</head>

<body>

    Ime autora: <input id="imeAutora" type="text" value="..."><br><br>
    <button id="btnAutor" type="submit" onClick="autorIme()">Prikazi</button><br><br>

    <script src="handlebars-v4.0.11.js"></script>
    <script id="sadrzaj-template" type="text/x-hanldebars-template">
         
        <h2></h2>
        <img src="">
        <h4> strana</h4>
        <h3>Autor: </h3>
        <h3>cena: </h3>
         
    </script>

    <div id="sadrzaj"></div>

    <script>
        function autorIme() {
            var ime = document.querySelector("#imeAutora");
            console.log(ime.value);
            var ourRequest = new XMLHttpRequest();
            ourRequest.open('GET', 'json.json');
            ourRequest.onload = function() {
                if (ourRequest.status >= 200 && ourRequest.status < 400) {
                    var data = JSON.parse(ourRequest.responseText);
                    createHTML(data);
                } else {
                    console.log("We connected to the server, but it returned an error.");
                }
            };
            ourRequest.onerror = function() {
                console.log("Connection error");
            };
            ourRequest.send();

            function createHTML(knjigeData) {
                var knjigeTemplate = document.querySelector("#sadrzaj-template").innerHTML;
                var compiledTemplate = Handlebars.compile(knjigeTemplate);
                var ourGeneratedHTML = compiledTemplate(knjigeData);

                var knjigeContainer = document.querySelector("#sadrzaj");
                knjigeContainer.innerHTML = ourGeneratedHTML;

            };
        };
    </script>
    <script>
        Handlebars.registerHelper('equal', function(lvalue, rvalue, options) {
            if (arguments.length < 3)
                throw new Error("Handlebars Helper equal needs 2 parameters");
            if (lvalue != rvalue) {
                return options.inverse(this);
            } else {
                return options.fn(this);
            }
        });
    </script>
</body>

</html>




Aucun commentaire:

Enregistrer un commentaire