Tuesday 17 December 2013

Not really news: Railo does grouped looping in CFScript

G'day:
I've recently been trying to be more positive about CFML... I get a bit of shit from people who think being honest about my impressions of ColdFusion equates to being negative. Bleak, perhaps, but I don't think it's negative. Anyway, Brad started a Twitter tag #favoriteCFML (I'll forgive the US-EN spelling this once ;-), and I've been popping the odd positive thought up on Twitter when it occurs to me. Join in if you like.

Anyway... this one deserves more than 140 chars.

Railo has implemented almost all of CFML in script-ready syntax. They've simply lopped off the "<cf" and ">" from a tag and that's yer script syntax. It works OK, but I think it's not very "scripty": it's just tags without the angle brackets. I've mentioned this to Railo, Adobe and everyone else who will listen, but that's not my point here. Bottom line whilst it's not ideal, it's better than not having functionality available in CFScript at all.

Adobe promised 100% CFScript coverage in CF9 and CF10. And they're getting around to it finally in CF11, apparently. As far as I can tell, Adobe are furnishing the Railo-style syntax for cross-compat, but are doing a more thoughtful / thorough / directed implementation too. I've not seen it - ColdFusion 11 is not in public beta yet - but this is the first thing I'll be checking when it's available to the hoi polloi.

Anyhow, some of my bugbears in CFML have been these trivial things:
  • <cfoutput> performs looping. The tag is <cfoutput>. It should output. It should not loop. If one wanted to loop over stuff, then they should provide a... well... <cfloop> tag or something. oh wait. They did.
  • Except they didn't do a complete job of <cfloop>: <cfloop> for the longest time could not do one thing <cfoutput> could handle with looping: a nested / grouped loop. More ludicrous than creating a tag called <cfoutput> that loops, is creating a <cfloop> tag which doesn't do looping as well as <cfoutput> does!
  • None of this was available in CFScript.
So that's all a bit cocked-up.

Anyway, Railo's generic CFScripting syntax solves all this:

family = queryNew(
    "id,generation,name",
    "integer,string,string", [
        [1, "parent", "Donald"],
        [2, "parent", "Valmar"],
        [3, "child", "Mark"],
        [4, "child", "Fiona"],
        [5, "child", "Adam"],
        [6, "grandchild", "Fleur"],
        [7, "grandchild", "Zachary"]
    ]
);
loop query=family group="generation" {
    writeOutput("<h3>#generation#</h3>");
    loop {
        writeOutput(name & "<br>");
    }
    writeOutput("<hr>");
}

One good think about the Railo CFScript syntax is that given it works by just changing the tag syntax, I was able to guess at the script syntax here. And this - contrived example that it is - works fine:
 

parent

Donald
Valmar

child

Mark
Fiona
Adam

grandchild

Fleur
Zachary


And I think grouped looping like this - how easy it is in CFML - is definitely one of the good things about CFML. It's low-brow and won't sell any licences in and of itself... and I don't even use it that often... but it's a bloody good construct.

And that much better now that it's doable in script.

#favoriteCFML.

--
Adam