Search code examples
jqueryasp.net-mvcjquery-pluginsjtemplate

Using jTemplate with ASP.NET MVC


In this article, Dave Ward describes how to use the jQuery plugin jTemplate to create what he calls a "client side repeater", that parses JSON data into a template on the client side.

Toward the end of the article, he suggests that the template is placed in a separate file with the extension ".tpl", and that the data is loaded into the document with the following syntax:

function ApplyTemplate(jsonData) {
  // This method loads the HTML template and
  //  prepares the container div to accept data.
  $('#Container').setTemplateURL('myTemplate.tpl');

  // This method applies the JSON array to the 
  //  container's template and renders it.
  $('#Container').processTemplate(jsonData);
}

However, when using ASP.NET MVC I can't just place the template file next to my view and call it with "/Guestbook/myTemplate.tpl". But I would like to place the template file next to the view, to keep things together.

How should I arrange this? A Controller Action that returns the text file contents? Some configuration in Global.asax.cs to make the Framework just return these files as is, without the Controller/Action url parsing? Any other ideas?


Solution

  • I'd suggest using .htm instead. That comes through okay in a default MVC app.

    It turns out that using .tpl is a bad idea anyway, due to some versions of IIS blocking it as an unknown file type unless you explicitly add it. I need to update my post.