View Full Version : template loader

5 May 2009, 7:18 AM
I found this code that is really good - for loading template files, instead of concatonating strings within javascript..


are there any flaws or is it safe to use?

5 May 2009, 7:28 AM
hi capn,

It's an interesting article. I honestly have not seen a need for this, but obviously the author has. :-\

That said,

You don't need a template to perform the string concatenation operations.

A utility that ext offers does what you need.

var replace = "Hello {0} and {1}! ... Wait, {0}. What's your age?";
String.format(replace, "John", "goodbye");

String.format(replace, "Sam", "go fly a kite");

5 May 2009, 8:37 AM
I haven't used this function yet, but it seems the difference is the replace function inserts by array value, whereas the template object inserts by json object values.

What I am using for:
I am creating html forms with extjs template tags, then i load the data from a json object.

similar to the documentation:
http://extjs.com/deploy/dev/docs/ search:"XTemplate"

the constructor for XTemplate can be a string, so you can go a step further and load the string from a file instead of concatenating. so that's the usefulness of this.

What are the disadvantages to doing it this way?
Is there easier way to load the template strings instead of having to make a request to server for file contents?
If you link to the template in header then I suppose that might be possible too?

5 May 2009, 10:24 AM
I found possibly better way:

to place template into a span with style="display:hidden;"
and then extract the text from the span with span.innerText, and then compile it...

5 May 2009, 10:26 AM
I like my confuration version better. :)

Actually, creating a factory utility should be simple, where you compile once on demand.

5 May 2009, 10:34 AM
Isn't a factory object overkill? It's not so much space in memory, and seems better to load before the application starts if you only have a few forms... But I guess if you have thousands of forms in application maybe a factory object would be useful - not for my case.. anyway- it's the clients machine that's doing the work, lol, so let their cpu sweat a little more

5 May 2009, 10:40 AM
I wouldn't want to keep my configuration inside of the DOM mainly because i do not wish to edit HTML directly. ;)

5 May 2009, 3:57 PM
thanks for your input jgarcia
i agree with you- i like having everything in javascript variables and no html elements- that's how i have my applications designed so far.

but i am going to move the template stuff to their own files locally.. small overhead of a few seconds of reading the files into memory when server starts up. it is so the css and html portions are completely separate from the extjs peices. the reason is also because there is web designer who is going to write css and make pretty images for applications -not my cup of tea - so i separate that to files they can play with and not interfere with version control.