PDA

View Full Version : Templates from a file?



oregontarheel
17 Apr 2007, 8:42 AM
Hey guys--

I have a situation where I want to render tabbed content panels on the fly, from a specific structure. Templates work very well for simpler HTML snippets, but preferrably, I'd like to be able to lay this out in a template file, rather than in-line javascript as these pages get to be more complex. What's the best practice in this case? Should i create HTML files with the template variables and then load to the client using AJAX, then compile? Or is there a better way?

mpriatel
17 Apr 2007, 10:26 AM
I commented on a situation like this in another post.

http://extjs.com/forum/showthread.php?t=4096

Basically, I put all my template HTML in their own files. I then use a servlet / PHP script to dynamically create a JS file which contains a list of vars where each one is a template. For example:


js_templates/
- template1.html ( which contains <div id='{id}'>{value}</div> )
- template2.html
- template_foo.html

then I add a <script> tag to my pages which points to the template builder

<script src='/jstemplate_builder.php'></script>

the script then returns a JS file which looks something like:


var jstpl_template1 = "<div id='{id}'>{value}></div>";
var jstpl_template2 = "etc..";
var jstpl_template_foo = "etc..";

and you can then reference those variables in your Ext code.

When I deploy the app I just use a static version of the template.js rather than use the script to dynamically build it on each request.