Fog Creek Software
Discussion Board

Knowledge Base
Terry's Tips
Darren's Tips

Use of {$ else $}

When you use {$ else $} can you then refer to CD variables in the "else" part?

{$foreach x in (and(keywordContains "menu")(thisArticle))$}<a href="{$ $}" class="active"> {$ x.filename $} </a>
{$ else $}
<a href="{$ $}">{$ x.filename $} </a>{$ next $}

I'm getting an error "variable is not defined" and can't figure out why.

Wednesday, July 21, 2004

From the documentation:

Using the {$ else $} section in a forEach loop, you can provide an alternative which is displayed if the contents of the loop would otherwise be empty.

So {$ else $} isn't expecting x.variable because it presumes the loop is empty.

Wednesday, July 21, 2004

(thisArticle) means that you are working in ONE article. The x counter in, x.filename etc. has no meaning there.

Jorgen B.
Wednesday, July 21, 2004

Do you mean "not thisArticle" ?  I don't see a reason for thisArticle in the loop condition.  If you are dealing with only this article you use .headline, etc. within that article.

David Burch
Wednesday, July 21, 2004

He's using the "and keyword thisArticle" as a kludge for "if thisArticleHasKeyword(word)."  This was mentioned in a recent forum thread.

The solution, in this case, is to use .headline instead of x.headline, since x is undefined in the {$else$} section, but .headline should still be valid.

Wednesday, July 21, 2004

I don't think .headline would give what I'm after - I want to loop through a bunch of articles, and if it was thisArticle, handle it differently.

Jorgen's method to create an "if" led me to a solution though:

Thursday, July 22, 2004

*  Recent Topics

*  Fog Creek Home