Fog Creek Software
Discussion Board

Knowledge Base
Terry's Tips
Darren's Tips

If only IF were more flexible

I just tried to find a way to do the following:

The site in question is a "not-blog" (i.e. it's a blog in denial). Items are posted and grouped into categories. Categories are defined within the keywords field. I would have liked to place the category name in front of the headline of each entry.

Basically, within a forEach X loop, this would have resulted in a statement like (WARNING: THIS DOES NOT WORK):

{$ if keywordContains "(html)" $}<label>HTML</label>{$ endlIf $}
{$ if keywordContains "(tc)" $}<label>TC</label>{$ endlIf $}
{$ if keywordContains "(etc)" $}<label>ETC</label>{$ endlIf $}
{$ x.headline $}

[Note: I have added line breaks for legibility. This code should not contain line breaks within an CD article because it'd produce unwanted whitespac.]

However, in CD2 the if statement only accepts "blank" or "nonblank". Hence, the aforementioned code doesn't work.

It looks as if I'll have to sacrifice another Extras field for this, create variables (<label>HTML</label> etc.) and place them into that particular field. If anybody has a more elegant idea, now's the proper time to tell me. :-)

If there is no more elegant way to solve this (effectively duplicating the effort that already went into religiously using keywords), I'd hereby like to ask the Fog Creek developers to give more power to the IF statement.

Monday, December 29, 2003

I can think of two solutions that don't involve going back through each article.

First, you can pass the {$x.keywords$} to a javascript function that could search the keywords string for the specific values and write out the correct value.  Let me know if you need help with the javascript required to do this.

Second, you could break your foreach loop into sections based on categories, and then just place the loops one after another, like this:
{$ foreach x (and (keyword contains "(html)") (yyyy)) $}
<label>HTML</label> {$x.headline$}
{$ next $}
{$ foreach x (and (keyword contains "(tc)") (yyyy)) $}
<label>TC</label> {$x.headline$}
{$ next $}

and so on, where "yyyy" is whatever condition you currently have for your loop.

Monday, December 29, 2003

Can't do either of those, unfortunately.

JavaScript is booboo because most of my visitors have Scripting turned off (as well they should, given the risks).

Secondly, I'm letting CD2 insert the last ten articles from all categories into that page. I.e. it's more like a {$ forEach 10 x $} loop.

Monday, December 29, 2003

Add this line for each keyword you want to substitute.
It works - adjust as necessary.

{$ foreach 1 x in (and (keywordcontains "foo") (thisfolder)(thisarticle)) $}<label>FOO</label> {$x.headline$}{$ next $}

But "if" would be simpler.

What sort of sites are your visitors visiting that they need Javascript turned off? - just kidding :)

Mike Osborne
Tuesday, December 30, 2003

The only other solution I can think of would be an external program or script that runs at publish time and modifies those files.  This isn't a particularly elegant solution, but would get the job done.

Wednesday, December 31, 2003

You could do it with PHP instead of Javascript. PHP is interpreted by the server, so it doesn't require users to have scripting turned on.

Darren Collins
Wednesday, December 31, 2003

*  Recent Topics

*  Fog Creek Home