Question

Now I wrote a template very easily:

Hello, my name is {name}, I'm {age}.

And then open this website: http://linkedin.github.io/dustjs/test/test.html, do some pasting and the template js codes are generated below:

(function() {
  dust.register("demo", body_0);

  function body_0(chk, ctx) {
    return chk.write("Hello, my name is ").reference(ctx._get(false, ["name"]), ctx, "h").write(", I'm ").reference(ctx._get(false, ["age"]), ctx, "h").write(".");
  }
  return body_0;
})();

Then I created a js file and copy all the js codes generated above and refer this into my app:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>DustDemo</title>
    <script src="jsfiles/dust.js"></script>
    <script src="jsfiles/staticpagetemplate.js"></script>

    <script>
        function Demo1() {
            dust.render("demo", { name: "test", age: 13 }, function (err, out) {
                alert(out);
            });
        }
    </script>
</head>
<body onload="Demo1()">
    <h1>Demo 1——Static Page</h1>
    <div id="divDemo1">
    </div>
</body>
</html>

Question is that if I use like this, it reports me "_get" cannot be found. Later I found the problem in dust.js:

Context.prototype.get = function (key) {
        var ctx = this.stack, value;

        while (ctx) {
            if (ctx.isObject) {
                value = ctx.head[key];
                if (!(value === undefined)) {
                    return value;
                }
            }
            ctx = ctx.tail;
        }
        return this.global ? this.global[key] : undefined;
    };

So no "_get" but "get" instead!

Why this MISMATCHES the translator online?? Anyone can help me??

Was it helpful?

Solution

You are likely using an old version of Dust in jsfiles/dust.js. As of Dust 2.2.0 get has been replaced with _get. The compiler on http://linkedin.github.io/dustjs/test/test.html uses the new version of Dust, so it compiles the template using _get instead of get.

You have compiled your template with a new version of Dust, but you are trying to render it with an old version of Dust, and the two are not compatible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top