Tuesday, February 21, 2006

Extension for TrimPath

Javascript Templates from TrimPath is good to work with. The documentation is quite good. It was fast and easy to get started. At the beginning I liked the idea of having the JST Template as the part of the page (hidden textarea), but it turned out that I will not be able to reuse in other pages. The suggested alternative of keeping *.jst files on the web server and load them into the browser using XMLHttpRequest sounded bit interesting.

I just extended TrimPath object by adding the function processTemplateFile, which does and XMLHttpRequest and fetch the jstfile and merge it with the context. You can pass function to get callback with processed template, response status and status text or pass dom element whose innerHTML will be updated once the template is processed.

Look at the extension I have added exttemplate.js

Example:
function mergeWithFile(trmfile, context, getcallback) {
var resultdiv = document.getElementById("result");
if(getcallback != null and getcallback == false)
TrimPath.processTemplateFile(trmfile, context, resultdiv);
else
TrimPath.processTemplateFile(trmfile, context, handler);
function handler(mergedJST, status, statusText) {
if(mergedJST == null) {
mergedJST = "HTTP STATUS: " + status + "," + statusText;
}
resultdiv.innerHTML = mergedJST;
}
}
function mergeWithoutCallback() {
var context = { values : ["prasad", "me"] };
mergeWithFile("jstfiles/mytemplate.jst", context, false);
}
function mergeWithCallback() {
var context = { values : ["prasad", "me"] };
mergeWithFile("jstfiles/mytemplate.jst", context);
// Same as
// mergeWithFile("jstfiles/mytemplate.jst", context, true);
}

Comments:
Hi Prasad,

I was looking for exactly such functionality when I came across your blog. However; the link for the js file is not working. In case you still have the file with you and you can share it please email it to me @ rajatmalhotraAtgmailDOTcom

Thanks,
Rajat
 
Hi Rajat,

You can reach the file here: http://onweb.googlecode.com/files/exttemplate.js

Regards,
Prasad
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?