PDA

View Full Version : template with inline code



extuzer
25 Jan 2008, 11:14 PM
There's this example in the docs on inline code inside templates :

Ability to execute arbitrary inline code
In an XTemplate, anything between {[ ... ]} is considered code to be executed in the scope of the template. There are some special variables available in that code:

values: The values in the current scope. If you are using scope changing sub-templates, you can change what values is.
parent: The scope (values) of the ancestor template.
xindex: If you are in a looping template, the index of the loop you are in (1-based).
xcount: If you are in a looping template, the total length of the array you are looping.
fm: An alias for Ext.util.Format.
This example demonstrates basic row striping using an inline code block and the xindex variable:



var tpl = new Ext.XTemplate(
'<p>Name: {name}</p>',
'<p>Company: {[company.toUpperCase() + ', ' + title]}</p>',
'<p>Kids: ',
'<tpl for="kids">',
'<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">,
'{name}',
'</div>',
'</tpl></p>'
);
tpl.overwrite(panel.body, data);


Wouldn't the 'company' variable generate an error ? I get an undefined variable error when i try to run that code with the data provided in the docs.

Condor
25 Jan 2008, 11:21 PM
Yes, the example is wrong.

Correct version:


var tpl = new Ext.XTemplate(
'<p>Name: {name}</p>',
'<p>Company: {[values.company.toUpperCase() + ', ' + values.title]}</p>',
'<p>Kids: ',
'<tpl for="kids">',
'<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
'{name}',
'</div>',
'</tpl></p>'
);

extuzer
25 Jan 2008, 11:54 PM
you rock ! thank you

extuzer
26 Jan 2008, 1:14 AM
ok another quick question :
is it invalid to use asterix as an object property.
I have a data that looks like this :
{'result' : ['key1' : 'value1', '*' : 'value2'] }

How do I access value2 ? If I write * in the template it errors out.

Condor
26 Jan 2008, 1:30 AM
Only alphanumeric characters and underscore and minus are allowed in property names (it assumes that object.propertyName is valid)

If you used an XTemplate you could use: {[values["*"]]}

temporary
31 Jan 2008, 3:00 PM
Will the wrong example be corrected?

This thread here just saved my life, I tried about an hour to get inline code working...