Search code examples
jsrender

How to print object as json in jsrender


I am new to jsrender, so how to print array of object as json

            <ul>{{for my_array}}
                 <li>{{:.toString()}}</li>
                {{/for}}
            </ul>

Solution

  • Take a look at the {{props}} tag doc topic.

    You can also use the {{jsonview}} tag control - see the JsViews topics:

    {{jsonview}} can be used with JsRender (i.e. without doing JsViews data binding) as in this example:

    <head>
      ...
      <script src="//www.jsviews.com/download/jsrender.js"></script>
      <script src="//www.jsviews.com/download/sample-tag-controls/jsonview/jsonview.js"></script>
      <link href="//www.jsviews.com/download/sample-tag-controls/jsonview/jsonview.css" rel="stylesheet">
    </head>
    <body>
    
    <div id="team"></div>
    
    <script id="myTmpl" type="text/x-jsrender">
    {{jsonview/}}
    </script>
    
    <script>
    "use strict";
    var team = {
      members: {
        m1: {name: "Robert"},
        m2: {name: "Sarah"}
      }
    };
    
    $("#team").html($.templates("#myTmpl").render(team));
    
    </script>
    </body>