Complex Conditional XTemplate Logic in ExtJS 2.2

Let’s say you’ve got a bit of JSON data — something like:

ReqPhoneSelection: "Primary",
ReqPrimaryPhone: "555-5555",
ReqAlt1Phone: "666-6666",
ReqAlt2Phone: "777-7777"

And you want to show just the one phone number as defined in “ReqPhoneSelection” in a fancy HTML template you’ve made in Ext.

How to do it? Well, you probably knew about ExtJS’s conditional logic in the XTemplate system, but you know you have to check against strings for something like this, so how to do it when you’ve already used your single and double quote allotments for that line of JavaScript?

Why, as I learned here, just start escaping single quotes:

ReadModeTemplate = new Ext.XTemplate(
, '{ReqPrimaryPhone}'
, '
, ''
, '{ReqAlt1Phone}'
, '
, ''
, '{ReqAlt2Phone}'
, '

(Yeah, duh, right? Took me hours to figure that out, so hush up. :P)

Now, no matter what JSON data you put into that template, you’ll only get out one number, depending upon what is stored in the “ReqPhoneSelection” property.

Leave a Reply