Best way to package multiple jQuery templates in single xHttpRequest? Best way to package multiple jQuery templates in single xHttpRequest? ajax ajax

Best way to package multiple jQuery templates in single xHttpRequest?


I like the referenced article in your update, except the assumption that you can't cache templates unless you insert them into the DOM. From the jQuery.tmpl documentation,

"To cache the template when using markup that is obtained from a string (rather than from inline markup in the page), use $.template( name, markup ) to create a named template for reuse. See jQuery.template()."

Using this, we can build a javascript template management system that allows us to load as many templates at a time as we need while keeping the DOM clean. On the client, keep a hash of template objects by name. You can use your favorite object based javascript pattern here, but I would think the structure could be like this:

templates[templateName] = {    templateMarkup: markupFromServer,    loadedAt: Date.now(),    compiledTemplateFunction: jQuery.template( templateName, markupFromServer )}

Then use the templates to generate HTML like this:

templates['unique-name'].compiledTemplateFunction(inputData)

Then, build an unload mechanism to free up memory:

function unload(templateName) {    delete templates[templateName];    delete jquery.template[templateName];}

Most importantly, you now have a method of storing multiple templates so you can make requests like: $.get('/TemplateManagement/Render', arrayOfTemplateNamesToLoad, loadManyTemplatesSuccess) to load multiple templates at a time. The only thing we need is a controller TemplateManagement that will take an array of template names as an input and return JSON that pairs a template name with its markup. There are a few ways to do this but it seems to me the most convenient is to define partial views for each template. In ASP.NET MVC 3, you can use this technique and RenderPartial to emit each template's markup into a JSON response. You can either name the partial views the same as the templates or map the names in some custom way.


OK, I read the article you reference in this post. As I see it, his way is probably one of the best ways to load up the template page(s). The only thing I don't like is the asynchronous problems that could crop up, esp. if you need to immediately do some templating before the async get returns... plus any binding issues that could happen before it returns something. In several projects I have done I use his "ancient" SSI (server side includes), but I just use something even easier like:

<% Response.WriteFile("this_page_template_file.html"); %>

You could put it anywhere where you'd place a tag. Like he says, just put in only the templates you need, or maybe include two templates: one is a "base" template with commonly-used items and the second one would have the page-specific ones with template references {{tmpl}}.

Is this even close to an answer? ;-)


[First off, great question. I love this topic]

I have no experience with the plugin "jquery-template-libs", but there is a particular lightweight javascript template plugins that are becoming almost a standard and plays very nicely with jQuery, which is probably better suited for the task than JTL, Mustache:

https://github.com/janl/mustache.js

It's got something that's called a "partial" which is essentially a way to include smaller templates within another one. Which sounds like it will help you out a lot.

On top of that there is a library called ICanHaz.js:

http://icanhazjs.com/

ICanHaz essentially extends Mustache to include built in functionality for templates and works incredibly well.

Mustache/ICanHaz allow you to add templates by variable, by a json call or by using tags. The choice is yours.