0

I used to be able to store a JSRender template in formatted text in a html file and stored on server. Then I used $.get to retrieve my template and render.

My helper method is similar to: Store a jsRender template in a separate js file:

function LoadJSRenderTemplate(container, templateUrl, myArray) {
$.when($.get(templateUrl))
 .done(function(tmplHtml) {
     $.templates({ MyTemplate: tmplHtml });
     container.html($.render.MyTemplate(myArray));
  });
}

My template looks like the following:

<div class="ResultTableRow">
    <span>{{>Distributor}}</span>
    {{if FuelType}}
    <span>
        {{for FuelType}}
            {{>#data}}<br />
        {{/for}}
    </span>
    {{/if}}
</div>

Today I started another project and use the latest jsrender suddenly the same technique stopped working...urrr. Now I have to compress my easy-to-read template into an one-liner js file following this http://www.jsviews.com/#samples/jsr/composition/remote-tmpl sample. Although it works, but I hate to have to lose the easiness in reading the indented template format I was so used to.

Why? And what changed?

Community
  • 1
  • 1
MagicGuru
  • 175
  • 2
  • 11

1 Answers1

1

This works fine for me:

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery.js" type="text/javascript"></script>
    <script src="http://www.jsviews.com/download/jsviews.js"></script>
</head>

<body>
    <div id="container"></div>

    <script type="text/javascript">
        var myArray = [
            {Distributor: "a", FuelType:"qq"},
            {Distributor: "b"},
            {Distributor: "c", FuelType:"rr"}
        ];

        $.get("file.html")
            .done(function(tmplHtml) {
                $.templates({ MyTemplate: tmplHtml });
                $("#container").html($.render.MyTemplate(myArray));
            });
    </script>
</body>

</html>

file.html

<div class="ResultTableRow">
    <span>{{>Distributor}}</span>
    {{if FuelType}}
    <span>
        {{>FuelType}}<br />
    </span>
    {{/if}}
</div>

(I slightly simplified your template).

See also Store a jsRender template in a separate js file - in case there is an issue with get() vs ajax() type "text"

Community
  • 1
  • 1
BorisMoore
  • 8,444
  • 1
  • 16
  • 27
  • Much appreciated. In the end I found it was another piece of JavaScript code interrupting the rendering and caused the confusion. – MagicGuru May 20 '14 at 07:21