garann means > totes profesh

a defunct web development blog

jTemplates: easy answers to dumb questions

Fri, 18 Dec 2009 20:21:19 +0000
I've been trying to use jTemplates for a fairly complex interface that displays quite a bit of data. The documentation on their site is pretty minimal, so I've come up with a lot of what may be very dumb questions that I've been forced to answer myself, by trial and error (how tacky). I figured I would post these in hopes of saving someone else some wondering: 1. Can you treat an array like an array once it's part of your template data? Yep. In my data, Columns is an array, and this code works great: <table class="{#if $T.Columns.length == 1}onecolumn{#/if}"> {#if $T.Columns[0].Label != null} <thead> ... 2. Does whitespace matter? Apparently not - see above. 3. How do you refer to a property of a parent object from a child template? I couldn't figure this out, so I worked around it by adding the property I needed to the root object I was passing into the child. I ended up doing this in a function because neither of the two delimiters jTemplates uses - {} and {#} - can be used for straight assignment as far as I could tell. {#foreach $T.Columns as c_f} {#include COLUMN_CELL root=addFieldType($T.c_f,$T.FieldType)} {#/for} ... function addFieldType(obj, ft) { obj.FieldType = ft; return obj; } 4. Can I put a property of the current object into my call to a child template? Seems like no. What I really wanted to do instead of the code above was this, which does not work: {#foreach $T.Columns as c_f} {#include COLUMN_CELL[$T.FieldType] root=$T.c_f} {#/for} ... {#template COLUMN_CELL[0]} <td><input type="radio" disabled="disabled"/></td> {#/template COLUMN_CELL[0]} {#template COLUMN_CELL[1]} <td><input type="checkbox" disabled="disabled"/></td> {#/template COLUMN_CELL[1]} For now, all I have is those four items.. I'm sure I'll come up with more as I get into the hard parts.