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.