views:

1423

answers:

3

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?

A: 

I think, you could just ignore the route, and link to your template file like you've suggested:

Check out this previous answer: http://stackoverflow.com/questions/273447/how-to-ignore-route-in-asp-net-forms-url-routing

Lewis
Thanks for the link! I have saved my template as in ~/Views/Guestbook/postListTemplate.htm, and added the following to my Global.asax.cs:<pre>routes.IgnoreRoute("Views/Guestbook/PostListTemplate.htm");</pre>However, I get a 404 error when trying to open the page in firefox. What am i doing wrong?
Tomas Lycken
+2  A: 

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.

Dave Ward
OK, will do. Meanwhile I have used a script tag with type="text/html" with the template, and then i accessed it with $(scriptTagId).html(). However, the .setTemplate() method could not parse it - not when I hardcoded its value into the js either... Does it have to be one line? What did I do wrong?
Tomas Lycken
To use inline templates, I believe you need to use setTemplateElement('ElementId').
Dave Ward
I tried this but without success. The js runs all the way, and debugging i find all values OK, but the content won't update. I have <textarea id="postListTemplate" name="postListTemplate" class="jTemplate"> and ("#gbPostList").setTemplateElement('postListTemplate');Any ideas? Thanks for all help!
Tomas Lycken
Nevermind, it's working now! Turned out my template wasn't specified the way it should have been. For future finders of this question: the inline template element has to be a textarea, and with the id property referenced in js (name is irrelevant). It can be hidden in css by display: block;Thanks!
Tomas Lycken
A: 

Hey Tomas can you explain a bit more into what you have done to get this to work ?

I am having the same issue, where the div or the element thats having the template inside of it is coming up as null

here is whats in my JS file

function buildHTMLTable(){ $j("#divListResults").setTemplate($("#templateHolder").html()); $j("#divListResults").processTemplate(jSon.data); }

This is my template.. which is on the code front of the.aspx file.

List Code Value {#foreach $T.d as ListValues} {$T.ListValues.code} {$T.ListValues.engVal} {#/for}

I get the error

$("#templateHolder") is null

That looks more like a jquery issue than a template one. Have you double-checked the id of your textarea for typos? Have you tried with a different selector?
Tomas Lycken