views:

25

answers:

2

I've been reading about the jQuery template plugin http://api.jquery.com/tmpl/ as a means of binding data to markup. All the examples seem to show that you define the template in a script new script tag. I'd actually prefer not to clutter my html <head> with all those script tags.

Is there a better way to organize this? Maybe with a templates folder full of scripts that get loaded dynamically?

+1  A: 

You can store your templates in external files (and than fetch them via Ajax, for example).

Quote from http://api.jquery.com/jQuery.template/

"... for remote templates, you can get a template markup as a string using any AJAX call you want, or you can simply use a static script block pointing to a js file which defines the string. Then you should use jQuery.template to compile the template from the string, and go from there, rendering it using jQuery.tmpl"

Šime Vidas
A: 

You don't have to do that. You can (for example) keep your templates in a .js file (sort-of like a message catalog):

// this is a file full of templates
var SITE_TEMPLATES = {
  ERROR_1: 'This is an error template: the error is ${error.msg}',
  WHATEVER: 'I cannot make this stuff up but you get the ${idea}'
};

Pull that in and then you can use the templates via

$('#someplace').append($.tmpl(SITE_TEMPLATES.ERROR_1, { error: { msg: "hello world" }}));

In other words, $.tmpl() lets you pass in the template body as the first parameter, and it can come from anywhere.

Alternatively, because it's a little messy to write long templates as Javascript string constants, you could pile up a bunch of text <script> tags in a single static HTML file. You could then AJAX that and drop it in a hidden <div> or something. That way you can still get the advantage of caching, and it might be easier to maintain the templates.

Of course, along those lines, you could maintain the templates separately as individual files, and glue them together at site build time according to any packaging scheme.

Pointy
Shouldn't SCRIPT elements be placed as children of BODY and not (for example) inside DIVs?
Šime Vidas
`<script>` elements can appear just about anywhere, including inside `<div>` elements.
Pointy
@Pointy Yea, but is there a reason to put them inside other elements? Does putting a SCRIPT inside a hidden DIV prevent the script from executing?
Šime Vidas
@Šime Vidas well in this case we don't want the scripts to execute anyway! They're template bodies, so the `<script>` tags are marked as having type "text/html" or "text/plain". The browser won't execute those anyway. In general, however, placing a `<script>` inside a `<div>` works just fine. Now, when adding dynamic content, it's necessary to evaluate the script tags explicitly. That's true no matter where the script tags (with in-line Javascript) are appended to the document.
Pointy
@Pointy I don't think that we can prevent the browsers from evaluating SCRIPT elements. As soon as they are added to the DOM, they are going to be evaluated. BTW, I am using the `text/x-jquery-tmpl` type on my template SCRIPT element. I am not sure what steps the browsers take in the case of templates, but I don't believe that it is dependent on where the SCRIPT element is located. Then why placing them inside other elements then? It seems to me that it should be most reasonable to always append them as children of BODY.
Šime Vidas
Well they can go wherever you want them to go. What difference can it possibly make? We're talking about DOM modifications *after* the document has been parsed, so validation is irrelevant.
Pointy
@Pointy Yes, they can go inside any element. But, if it doesn't make a difference where they are placed, then why put them inside specific elements and not just inside BODY? (One scenario where it makes a difference is using `document.write`, but other than that I can't think of any.) If there is no difference, then it makes most sense to just always append them as children of BODY. That's what I'm trying to say here...
Šime Vidas
OK yes that's true - I was thinking that if they were inside some element then that'd make it possible to easily find them with a jQuery selector, but iterating through them might not ever be necessary.
Pointy