Weird CFLOOP Quirk

I don't use CFLOOP with the condition attribute very much, and when I do, it's usually for simple stuff like a 'while true' loop. However, I ran into a weird quirk today with it, and thought I'd share. Take this code:

[cfloop condition="count LT #config.MAX_IMAGES_PER_IMPORT#">...

Pretty simple, right? As you undoubtedly know, CFLOOP evaluates the condition expression each iteration, so any "static" things can be enclosed in hashes so they're evaluated once (as part of parsing the CFLOOP tag) rather than every iteration. I've done this with the MAX_IMAGES_PER_IMPORT constant.

While I was doing some debugging, I wanted to temporarily truncate imports to a smaller (constant) number, so I did this:

[cfloop condition="count LT #config.MAX_IMAGES_PER_IMPORT / 8#">...

Unlike what you'd expect, this throws a syntax error! Specifically, it's complaining about the slash in there. What's the solution? Change the code to this (move the hashes to use per-iteration evaluation on the expression):

[cfloop condition="count LT #config.MAX_IMAGES_PER_IMPORT# / 8">...

If you're anything like me, a big ol' WTF is probably running through your head. As near as I can surmise, the value of the condition attribute is treated unliked any other string value in CFML. More specifically, variable substitution is performed with hashes, rather than expression evaluation. As my two year old daughter would say "… kinda weird …".

6 responses to “Weird CFLOOP Quirk”

  1. Patrick McElhaney

    For a while you couldn't use operators within hashes in CF. I think it worked originally, then it didn't work for a couple of versions, and then it worked again.

    Actually, I don't think you even need the hashes in this case. :)

    Patrick

  2. Tony Petruzzi

    I don't see the big surprise here.

    count LT #config.MAX_IMAGES_PER_IMPORT / 8#

    That would obviously throw a syntax error. This isn't a cfloop weirdness, it's a syntax error on your part. Sorry for coming off as sounding like a jerk but before you say something is weird or a bug, you should make sure that it is.

  3. Ben Nadel

    That is really strange! If you want to get crazy, this works :)

    [cfloop condition="count LT #IIF( true, DE( config.MAX_IMAGES_PER_IMPORT / 8 ), DE(0) )#"]

    No need to, but I just wanted to test it.