CityDesk: Variables Within Variables
15 March, 2002
Wheee, CityDesk SP1 has a ton of fun new stuff! I just did a little test, and it looks like variables now resolve to a depth that is greater than the previous two levels of nesting. (My test went up to four.)
What does this mean? Previously, CityDesk couldn't handle a situation where var1 contained a reference to var2 if var2 in turn referenced some var3. You could make the first leap (var1 to var2) but you couldn't make the second leap -- CityDesk didn't make enough passes through the script (or something like that was the explanation that was given).
With SP1 they've fixed this: you can get var3, even a var4 and probably higher (4 was as high as I got). So I'm going nuts with variables. You've been seeing this kind of thing:
(brought to you by the multilevel variable evaluation crew)
at the top of every page and with the teaser on my index page. Up until now, I had to be careful to stick something in extra1 (the field that contains the part that changes) or the page would get published as "brought to you by the crew", which looks kind of dumb. (So the whole concept is a little dumb, but hey, why not keep doing it.) Now I've moved the code that generates that line out of my Simple template and into a Credits variable. Up until now I would have gotten a message from CityDesk that "extra1" is not defined. Here's how it is defined:
{$if nonblank .extra1$}
<FONT size=1>
(brought to you by the {$.extra1$} crew)
</FONT>
{$endif$}
I'm sure there are more interesting applications for this, I've just begun experimenting.
Unfortunately, there are still some things you can't do. First, you can't have a variable that contains a reference to a loop variable (the following won't work):
Variable LoopBody:
<A href="{$x.link$}">{$x.headline$}</A>
In an article:
{$foreach x in (all)$}
{$LoopBody$}
{$next$}
CityDesk complains about "x" not being defined when it evaluates LoopBody. Which I guess makes sense, but it would be nice if it did a slightly lazier evaluation so this kind of thing could work. This means that I can't use the Credits variable in the loop on my index page.
Another variation on this theme involves putting the foreach into a variable and writing the loop body separately:
{$ForLoopVar$}
some stuff here {$next$}
CityDesk complains about "next without foreach".
It would also be cool if I could replace the "all" in the foreach condition with a variable:
{$foreach x in ({$.extra2$})$}
some stuff here
{$next$}
But this doesn't work either. Oh well, I'm excited about what does work, and I'm looking forward to SP2. Nice work, guys.