Fog Creek Software
Discussion Board




Knowledge Base
Documentation
Terry's Tips
Darren's Tips

How to EXCLUDE articles in the foreach loop?

Suppose I have in folder Articles more than 10 articles. The first 5 (based at fileddate) articles I want to show in the index.html. The loop looks like this:

{$foreach 5 x in (folder "articles") SortAscendBy .fileddate$}

<P><A href="{$x.link$}">{$x.headline$}</A></P>{$next$}

All the other articles I want to show in archive.html EXCLUDING the five articles in index.html. How would the foreach loop look like??

Like this?
{$foreach x in (and (folder "articles") (not(foreach 5 y in (folder "articles) SortAscendBy .fileddate next))) SortAscendBy .fileddate$}

<P><A href="{$x.link$}">{$x.headline$}</A></P>
{$next$}

As you may guess, this loop doesn't work. What would be the proper way?

Johan Koerts
Tuesday, April 09, 2002

I sure don't know how you'd do that unless you used different folders or key words to ID the files you want on your index page.  When you put a new article in your front page folder, you also move an old article from that folder into your archive folder.  I don't think that's what you want though.

But, my thinking is that an archive by definition should show everything.  As a surfer, how would I know that the archive is missing the the most recent 5 articles?

tk
Tuesday, April 09, 2002

I agree, an archive should show everything including the top 5 - that's the way my site works.

I put the index "article" in the folder with the other articles and this is the code I use:

(and
(folder "articles")
(not (keyword_contains "index") )
)

then all of the index pages are pulled in to the main page via (keyword_contains "index").

Mark W
Tuesday, April 09, 2002

Another idea is to do a loop that brings in the articles based on time - everything in the past week, and then another that has everything earlier than a week ago for the archive.

Mark W
Tuesday, April 09, 2002

If you are willing to change your concept from first 5 on one page and all the rest on another page, you could use the "after today - x" condition.  With this condition you could publish all articles in the last x days on the first page and all others (using the NOT modifier).

I tried playing around with javascript, and soon realized that the fields in citydesk are not 'variables'.  They are more like constants, and except for the limited date arithmetic, you can't manipulate them.

Somewhere here there was a discussion about CD eventually supporting vbscript as a more advanced scripting engine. 

Joel Goldstick
Tuesday, April 09, 2002

darn, while I was testing, Mark beat me to the post!

Joel Goldstick
Tuesday, April 09, 2002

=) At least your post is more thorough than mine, mine was a quick and dirty two sentancer.

Mark W
Tuesday, April 09, 2002

You can do it in JavaScript by rendering each of the articles within named blocks (div, span, table, td, etc.). I place an article ID in the extra2 field for this purpose.

You then use the original loop conditions to render JavaScript code to hide the blocks with the same IDs.

I use a similar technique on my website to render a submenu of articles on the left side of the screen and only display the submenu matching the folder that is currently selected. The folder name is rendered into a JavaScript variable and checked after all the submenus are rendered.

Check out the source for the World Trade Center page under Photos. You'll see that there are named blocks for all the folder submenus. Only the "photomenus" block is made visible.

The code is currently Internet Explorer specific but I will have the site updated to support Netscape this weekend.

My site is at:

  http://www.stuartistry.com

Stu
Wednesday, April 10, 2002

The low-tech solution that I use: two folders, Articles and Archives. Each time I add an article to the top of the Articles folder, I drag the last article in the folder down one notch to become the first article in the Archive folder (of course, I have the two folders adjacent in my CityDesk tree). Then the scripting is absurdly simply.

Mike Gunderloy
Wednesday, April 10, 2002

Mike, What a sweet elegant solution.

Mark W
Wednesday, April 10, 2002

Mike's way of doing things certainly is simple, but how would you consistently link to a particular article from an external site, you'd probably have to wait until it was moved into the archive. Not ideal.

I agree with TK and Mark, the archive should really contain all articles of that particular type. For the index page you should probably continue to use your {$ foreach 5 x in ... $} style, I would :-)

The other suggestion of using keywords is also good, I would turn it around and use a "Archived" keyword though, but that's just my preference.

If you use a date based schema you'll most probably have occasions when suddenly you only have one or two articles on the index page, say if you've been away on holiday for a couple of weeks or there just hasn't been any relevant news for a while. It always looks better to have a healthy number of articles on the index page.

Ian Jones
Thursday, April 11, 2002

"The other suggestion of using keywords is also good, I would turn it around and use a "Archived" keyword though, but that's just my preference."

Sure...

{$foreach in
(and)
(not (keyword_contains "Archived") )
(folder "Articles")
) $}

And on the next page, one that brings in everything in the folder Articles AND with the keyword Archived.

But then, I always include everything in the archive, even the current stuff.

Mark W
Thursday, April 11, 2002

*  Recent Topics

*  Fog Creek Home