Fog Creek Software
Discussion Board




Knowledge Base
Documentation
Terry's Tips
Darren's Tips

nested forEach would cause infinite loop

OK, I'm stuck.

I want a foreach loop which inserts the Body of each article from the condition. 

{$ forEach x in (not (thisArticle)) $}
<h3>{$x.headline$}</h3>
{$x.Body$}
{$ next $}

I've tried putting this code into an HTML instead of in an article.  I would have supposed this would work, since there is no way that HTML can be included in the foreach.

How do I do this without getting the infinite loop error?

Eric Sink
Wednesday, July 21, 2004

I think you have to define a folder as well or include the "all" command to collect results from all folders.

Jorgen B.
Wednesday, July 21, 2004

I don't think the conditional matters.  The following script gives the same result:

{$ forEach 10 x in (all) $}
<h3>{$x.headline$}</h3>
{$x.Body$}
{$ next $}

It seems to me this should work, since it is not in an article, but in an HTML file item.

CityDesk seems to be objecting simply to the notion of having Body inside the loop.

Eric Sink
Wednesday, July 21, 2004

This works for me:
{$ foreach x in ( and (all) (not thisArticle) ) $}
{$x.Body$}
{$ next $}

David Burch
Wednesday, July 21, 2004

Aha!

I tried the fragment above which worked for David Burch:

{$ foreach x in ( and (all) (not thisArticle) ) $}
{$x.Body$}
{$ next $}

and it didn't work.

So then I tried it in a brand new CityDesk site, and there it worked.

I fiddled for a while and realized the problem.  I have loops elsewhere in the site which use x as the loop variable.  So I changed the loop to say:

{$ foreach y in ( and (all) (not thisArticle) ) $}
{$y.Body$}
{$ next $}

and now it works.

Thanks!

Eric Sink
Wednesday, July 21, 2004

That's weird.  I wonder what the scope of a variable is...  Okay, terminology not right for CityDesk.  I wonder what the scope of $x is. 

Were these other $x loops in the same article or in variables used in the same article?

David Burch
Wednesday, July 21, 2004

I would get real worried if they were in other articles or in variables not used in that article, because I use the same variable name all over the place.

David Burch
Wednesday, July 21, 2004


The $x variable was used in other pages/articles.  It definitely appears that CityScript cannot handle any sort of scope nesting.

Eric Sink
Wednesday, July 21, 2004

Ugh!  Could we have someone from Fog Creek explain how this works?

David Burch
Thursday, July 22, 2004

As far as I can tell it's quite straight forward. If you nest loops you need separate iterators.

That is if
{$ foreach y in ( and (all) (not thisArticle) ) $}
{$y.Body$}
{$ next $}
encounters a loop in the body of an article.  You'll get the infinite loop message if that loop also uses $y as it's iterator.  If it uses $x, $z, $q or $topten you're fine.

Otherwise you can reuse iterators with abandon.  The problem is that it can be hard to predict when one loop might be nested within another. Especially if your site structure changes.

I try to use reasonably unique iterators for all my loops.

Ken McKinney
Thursday, July 22, 2004

Ken's got it right...

Michael H. Pryor
Fog Creek Software
Thursday, July 22, 2004

Thanks.

David Burch
Thursday, July 22, 2004

As in any scripting/programming language the more understandable your variables are the easier the code is to read and understand. Even when you don't *need* to using Article, ThisArticle, ThatArticle ... will make life a lot easier when comes time to figure out "What the hack was my intent/plan when I wrote this ???" What will x, y, z, a ,r, ... mean to you in a year or two?

Perpetual Newbie II
Friday, July 23, 2004

"What will x, y, z, a ,r, ... mean to you in a year or two?"

The same thing it means now - cityscript is so simple that the variables really don't need any explanation.

www.MarkTAW.com
Sunday, July 25, 2004

*  Recent Topics

*  Fog Creek Home