Fog Creek Software
Discussion Board




Knowledge Base
Documentation
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 doc.author$}{$doc.author$}&#13;&#10;{$endif$}{$doc.teaser$}" href="{$doc.link$}">{$doc.headline$}</A><BR>{$doc.fileddate$}</P>{$next$}

But if I assign a variable .doclink with value :

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

and use :

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

Citydesk barfs with "The variable doc.author is not defined". It's substituted the variable, otherwise it wouldn't know about doc.author - 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 08, 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 08, 2003

I think this is  the thread where Joel eplains this:
http://discuss.fogcreek.com/CityDesk/default.asp?cmd=show&ixPost=1493&ixReplies=5 (Scroll down).

David Burch
Wednesday, January 08, 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 http://www3.oup.co.uk/computer_journal/hdb/Volume_08/Issue_03/080225.sgm.abs.html for the prototypical example. It makes for pretty obscure code, though - not to mention huge stacks.

Michael Wild
Wednesday, January 08, 2003

*  Recent Topics

*  Fog Creek Home