I wish I had cfnextif

A couple of years ago I did quite a bit of Perl programming. One thing I really liked was syntax like this:

next if()
It's very simple, and you use it in a loop. You can put it anywhere you want in the loop, and if the condition is true, the code will skip to the next loop item. I LOVE this.

I know we can do a similar thing in ColdFusion like so:

<cfloop from="1" to="10" index="i">
   <cfif myVar>
      <!--- Do loop stuff --->
   </cfif>
</cfloop>

But something like this just seems better, to me at least:

<cfloop from="1" to="10" index="i">
   <cfnextif not myVar />
   <!--- Do loop stuff --->
</cfloop>

So there's my first CF 9 wishlist item. :)

Related Blog Entries

Comments
That would be nice
# Posted By Allen | 12/15/07 11:11 AM
<cfloop condition="not myVar">???
# Posted By todd sharp | 12/15/07 12:22 PM
With pound signs of course...
# Posted By todd sharp | 12/15/07 12:23 PM
No, that's not the same thing Todd. Here's an example: Say you want to loop over a list of links in your site. You want to validate that all links work, EXCEPT for those that are internal links on your site. So you'd use the cfnextif to skip any internal links. A conditional loop would just loop until the condition is met...not the effect I'm after.
# Posted By Jacob Munson | 12/15/07 12:28 PM
Gotcha...
# Posted By todd sharp | 12/15/07 3:08 PM
Something like a cfcontinue tag ? It's exists other languages... But apparently Adobe thinks different. See comments on this page and note the date: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/...
# Posted By crisp | 12/15/07 3:52 PM
Yeah, crisp, something like that. It does seem discouraging that they haven't added it yet, if this has been an enhancement request for a long time...but I do know that I was talking to Ben Forta a while ago, and he said they were having a hard time thinking of things to add to CF 9...maybe they'll finally get to it. :)
# Posted By Jacob Munson | 12/15/07 3:59 PM
Are you talking about something like:

cfif condition
cfbreak
/cfif

?

Except, as a one-liner?
# Posted By Sammy Larbi | 12/15/07 5:23 PM
Ahh but not break, just skip to the next iteration of the loop?
# Posted By Sammy Larbi | 12/15/07 5:23 PM
'Continue' works in cfscript only unfortunately, but reading the comments on this post might give you more solutions.

http://www.chapter31.com/2006/11/29/will-cfmx8-hav...
# Posted By Michael Sharman | 12/15/07 5:33 PM
@Sammy,

Yes, just skip to the next iteration of the loop.

@Michael,

It seems that that cfcatch solution is a lot more code than a simple cfif...not sure I see the benefit there.
# Posted By Jacob Munson | 12/15/07 8:05 PM
It's about doing it in one simple short line. And that's the whole point of CF in terms of marketing, that it makes doing things easy. Soooooo...... that is what should be driving adobe to add it in 9.
# Posted By Allen | 12/15/07 8:24 PM
I hate to dissent here, but I wouldn't want to see this type of syntax introduced into the CF Language. I've seen a few people requesting similar syntax using the closed tag abbreviation. I think its important to maintain the hierarchy of the document as much as possible. it makes it easier for our tools to understand what's going on if there is a consistent nesting of tags and related content.

If you were to expand the sample to use the plain tag sequence and put the loop stuff in as the contents, you essentailly wind up with what we have now using the cfif. I think that if we are going to add multiple ways to do the same thing in the language, the effort would be better spent enhancing cfscript.
# Posted By Mike Rankin | 12/15/07 11:59 PM
I have to agree with Mike on this one.. you yourself already point out the way to do it in ColdFusion. CFML is a Markup Language... XML based.. working with opening and closing tags. The only way that Adobe will incorporate it in the language is in this way <cfnextif> </cfnextif> and that would be totally the same as your own example using the <cfif> notation.

It would not (in my humble opinion) make the language better... only bigger with tags that will do the same thing. ColdFusion developers expect a closing tag.. that's the way the language is build.
# Posted By Tjarko Rikkerink | 12/16/07 1:22 PM
Mike & Tjarko,

What about cflocation, cfset, and the many other tags that don't have a closing tag? And XML doesn't always have a closing tag either...that's what the "/>" is for.
# Posted By Jacob Munson | 12/16/07 4:03 PM
@Jacob. The /> is a closing tag.. XML needs a closing tag for every tag you use otherwise it's not XML. For example <tag></tag> is the same as <tag />

You are right about the tags described but those are exceptions on the rule, as in any language. The point in the request above is that adding <cfnextif /> is the same as <cfnextif> </cfnextif> and therefore is the same as the given example that Jack gave us in his first block, or second if you like, of code. I can certainly imagine wanting this if you are used to it coming from a different language but in the case of ColdFusion it's not necessary, because you can already do it in a correct way. If this then do this or else skip to the next.....
# Posted By Tjarko | 12/16/07 9:13 PM
Sounds like you are ready for a more flexible language. Something more open perhaps?
# Posted By Justin Kay | 12/17/07 9:25 AM
@Tjarko,

I know the difference between "/>" and "</tag>". Believe me, I've been doing web pages for a long time, you don't have to teach me about the basics.

I am illustrating that your logic doesn't hold, because you say "CFML is a Markup Language... XML based.. working with opening and closing tags.", but both CFML and XML have LEGAL ways to both open and close a tag at once, using the />. You said that cflocation and cfset are exceptions to the rule...well what made Allaire decide to "break" this rule? I'd say it was because cflocation and cfset are tags where it doesn't make sense to have a closing tag. I'd submit that <cfnextif /> would be the same thing. I'm just suggesting that such an addition would help in a lot of cases, particularly for folks like me that are used to this syntax.

Oh, and for the record, CFML is NOT XML based. Cold Fusion 1.0 was released before XML even existed:
http://en.wikipedia.org/wiki/Coldfusion#History
http://en.wikipedia.org/wiki/Xml#History
# Posted By Jake Munson | 12/17/07 12:10 PM
Hi Jake, sorry to have stepped on your toes.. that wasn't my intention. for the record.. XML is a subset of SGML, as is HTML as is any other Markup Language. Because almost everybody knows XML and not SGML I stated it was an XML based language, my bad and i'm sorry for misinforming. and that was the basis for ColdFusion 1.0 at least that's what they told me 11 years ago when I started to develop in this language. An exception to the rule still holds because why are the other 200+ tags in the ColdFusion language still with a closing tag??

I will not further go into this discussion because it doesn't make sence... you have your point of view.. I have mine, let's leave it at that because we both make strong points.. we just don't agree :-)
# Posted By Tjarko | 12/17/07 12:51 PM
Todd, not to oversimplify things, but why not just wrap the contents of the loop in a CFIF block? It accomplishes the same thing and it's not much (if any) more code.

<CFLOOP query="qAllSiteLinks">

<CFIF internalLink eq 0>

...

</CFIF>

</CFLOOP>

?
# Posted By Liam Kelly | 12/19/07 8:25 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner