views:

40

answers:

4

As a JS developer, I always keep my design layer separate from my business layer. Meaning, HTML is always alone, CSS and JavaScript files are external and included.

Now, in the case of jQuery Templates, a declared template must apparently live within a script block of the page. How in the world are you supposed to keep all of your business separated? I don't want messy HTML. I want clean HTML that never needs to be touched because it's been designed that way...

Are there solid, proven methods for doing this?

A: 

You can call $.templ(yourTemplateString, data) if you want. That returns the built-up elements which you can then stick in your document with "append" or whatever.

I agree with you that doing templates as <script> tags is not a super cool idea for everyone.

Pointy
I've decided to essentially write my own functions that return the "template"
dcolumbus
I think that's a fine idea - in fact you could package them at build time into a separate static content file, sort of a big JSON object or something like that.
Pointy
A: 

There is a sacrifice, but what looks more maintainable and clean:

Original:

for(var i=0; i<client.name.length; i++) {
    clRec += "<li><a href='clients/"+client.id[i]+"'>" + client.name[i] + "</a></li>";
}

Templates

<script id="clientTemplate" type="text/html">
    <li><a href="clients/${id}">${name}</a></li>
</script>

Code from http://blog.reybango.com/2010/07/09/not-using-jquery-javascript-templates-youre-really-missing-out/

Dustin Laine
Yes, it "looks" better... but I'm not talking about short code... I'm talking about a complete separating of relevant content. I shouldn't have to edit my .htm file in order to change my javascript.
dcolumbus
A: 

You can use a different $.tmpl() syntax and $.ajax() to use jQuery Templates definitions stored in external files.

Dave Ward
A: 

I suppose that if you're looking to templatize your application, try out the jQuery Template. But I've come to realize that, like many frameworks, ends up complicating the code even further.

dcolumbus