1

I am using mustache.js to render template for 2 user data. Due to some error I am unable to get result, though for a single object I got results. Can someone help?

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.1.3/mustache.js"></script>
<script>
$(document).ready(function(){
    $('button').click(function(){
        loadUser();
    });
});
</script>
</head>
<body> 
<button>Click Me!</button>
<div id="target"></div>

<script>
function loadUser() {
  var template = $('#template').html();
  var jdata = [{name: "Luke", age:"43"},{name:"Lara",age:"19"}];
  var rendered = Mustache.render(template, jdata);
  $('#target').html(rendered);
}
</script>
<script id="template" type="x-tmpl-mustache">
<p>Hello {{ name }}! with age {{age}}</p>
</script>
</body>
</html>

1 Answer 1

1

You are getting this error because repeating expressions are missing in the mustache template. Use the code below instead for defining template.

<script id="template" type="x-tmpl-mustache">
    {{#.}}<p>Hello {{name}}! with age {{age}}</p>{{/.}}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

That's the right Answer dude! I did not know adding #. or /. for looping by Mustache.js! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.