using blocklayout xar:set
Posted by: Jo on October 2, 2008 | Updated: December 30, 2011 01:50 PM
setting variables with xaraya blocklayout
The Xaraya Blocklayout (BL) <xar:set /> tag is probably one of the most misused tags, but also one of the handiest.
The <xar:set /> tag1 is (read ... should be) used to set a variable to the value of an expression. The "name" attribute identfies the variable name, and the value is set in the tag body.
<xar:set name="foo">'some expression'</xar:set>
Sets the value of the variable $foo to 'some expression'. The variable is available in the current page load and does not persist outside of that.
id : not required, an identifier for the tag
name: required, the name of the variable to be set.
While in early days of Blocklayout, 'name' attribute required a $ to refer to the variable, it was deprecated very early on (2004) for the Xaraya 1x branch and there is no guarantee of further support for usage of the $ in the variable name (in 1x). In Xaraya 2x it was originally illegal but now can be used with and without the $ in the variable name but with different usage and meaning.
Best practice - 'do not use the $ in the name attribute in Xaraya 1x'.
Usage of arrays or objects in the name attribute are not supported, and results would be unreliable in the least case.
<xar:set name="dummy">1; $array['key'] = 'my array value set at last'<xar:set>
The <xar:set /> tag has been used to do all sorts of things that were not intended for it. In that respect it has often been one of the handiest tags as it enables themers and developers to use php in xaraya templates.
At the same time, xar:set is a bit of an Achilles heel in terms of security and blocklayout due to allowing the use of php in the tag body.
Xaraya Core files and core distribution modules have attempted to keep the use of xar:set to its intended purpose and usage. Outside of these core files and modules, xar:set use could probably be described in many cases as 'do whatever you need to do to get it working'.
Theme (and template) portability - this is a consideration that many forget. Using xar:set for php coding that is best done in a function is one of the biggest issues why upgrading a theme can become troublesome. The xar:set expressions in these cases often tie the theme to the site content making changing themes or upgrading from one module or xaraya core version to the next a painful experience. Of course this could be offset by the plus side that makes Xaraya so attractive to many - enabling them to do so much in a template that they could not previously do without hacking code.
In the 1x line the xar:set tag has had a number of bugs (mostly 2) that demonstrate why a workover is needed for this tag if arrays or objects in the object are used in addition to allowing data to be known in subtemplates 2, 3, 4.
Related project :