Fog Creek Software
Discussion Board

Knowledge Base
Terry's Tips
Darren's Tips

Variable substitution not working as expected

The following works :

{$foreach doc in (folder "docs/YWcons") sortdescendby .fileddate$}<P><A title="{$if nonblank$}{$$}&#13;&#10;{$endif$}{$doc.teaser$}" href="{$$}">{$doc.headline$}</A><BR>{$doc.fileddate$}</P>{$next$}

But if I assign a variable .doclink with value :

<P><A title="{$if nonblank$}{$$}&#13;&#10;{$endif$}{$doc.teaser$}" href="{$$}">{$doc.headline$}</A><BR>{$doc.fileddate$}</P>

and use :

{$foreach doc in (folder "docs/YWcons") sortdescendby .fileddate$}{$.doclink$}{$next$}

Citydesk barfs with "The variable is not defined". It's substituted the variable, otherwise it wouldn't know about - so why doesn't this work? I assumed Cityscript would evaluate recursively as macrogenerators usually do, but obviously not ...

Ideas, anyone?

Michael Wild
Wednesday, January 8, 2003

Yeah, annoyingly the parsing of the CityScript won't let that work. I'm sure Joel explained why somewhere but it's to do with the order it substitutes things. Maybe in the next major release...

John C
Wednesday, January 8, 2003

I think this is  the thread where Joel eplains this: (Scroll down).

David Burch
Wednesday, January 8, 2003

Thanks guys. I can (now) see what the problem is, but I don't understand why recursion is a big issue. Isn't this like any other recursive descent parsing problem, where you have an incomplete-expression stack, and if you run out of stack space the compiler stops and complains? In this case you could presumably count {$'s and stop at some (hopefully user-definable) limit.

Cityscript is a sort of macro language, so strictly speaking foreach loops are expanded rather than executed, but I can't see that making a huge difference. Of course "real" macro languages don't have iterators at all - EVERYTHING is done by recursion - see for the prototypical example. It makes for pretty obscure code, though - not to mention huge stacks.

Michael Wild
Wednesday, January 8, 2003

*  Recent Topics

*  Fog Creek Home