Blaise
Blaise

Reputation: 22202

jQuery Template

This is about jQuery Template

I use NuGet to install it. The information is in GitHub. So I guess I am using the correct package.

jQuery.tmpl.min.js is added and referenced in the ASP.NET MVC 4 project.

The problem is, when I test using

$('#files-list').tmpl(data).appendTo('#Container');

It works.

But if I changes to:

var t = "div" + tmplName;
$(t).tmpl(data).appendTo('#Container');

It fails. And the FireBug gives me some console error like $(t).tmpl(data).appendTo is not a function.

Can anyone tell me why I cannot do this? and how can I fix it?

Thank you.

Upvotes: 0

Views: 1193

Answers (2)

naspinski
naspinski

Reputation: 34687

This:

var t = "div" + tmplName;

Can never equal to this:

'#files-list'

Wouldn't you want something more like:

var t = '#' + tmplName;

Upvotes: 0

VJAI
VJAI

Reputation: 32758

Why you are getting that exception is because you don't have a script block with the id t or "div" + tmplName. Make sure you have a script block with the passed id.

Ex.

<script id="movieTemplate" type="text/x-jquery-tmpl"> 
    <li><b>${Name}</b> (${ReleaseYear})</li>
</script>

<script>
var movies = [
    { Name: "The Red Violin", ReleaseYear: "1998" },
    { Name: "Eyes Wide Shut", ReleaseYear: "1999" },
    { Name: "The Inheritance", ReleaseYear: "1976" }
];

$("#movieTemplate" ).tmpl(movies).appendTo("#movieList");
</script>

Upvotes: 2

Related Questions