Friday, 25 July 2014

<?cf ?>

I was thinking about this independently the other day, but then the same question came up on Stack Overflow today: "Is there a shorthand for <cfoutput>", in which the punter asks if this sort of thing is possible in CFML:

<input name="LastName" <?> value="#FORM.LastName#" </?> />

Where <?> / </?> is shorthand for <cfoutput>.

The obvious answer here is to just put one <cfoutput> block around the entire file's contents. Job done.

What I was more thinking was like how PHP has <?php / ?> instead of the - fairly cumbersome - <cfscript> / </cfscript>.

Having a more terse syntax here means not breaking the flow of mark-up quite so much if one wants to mix CFML with mark-up, eg:

<?cf array.each(function(i,v){ ?>
<?cf }); ?>

This would be analogous to:

<cfloop index="i" from="1" to="#arrayLen(array)#">
    <cfset v = array(i)>

(I've borrow from Ruby here, with the #{variable} syntax, but I think that'd be a great shorthand way of expressing what the person on Stack Overflow was asking for).

TBH, I personally don't often want or need to mix CFML and HTML like this, so the <?cf ?> construct would not be so useful for me, but I like the #{variable} idea.

People have in the past wanted to mix HTML in their CFScript blocks, and this would facilitate that. Other people have want an implicit <cfoutput> around all code in a file, and short of doing that the #{variable} notation could solve this: it would always be interpretted as "output the variable's value", anywhere in a CFML file.

I didn't put much thought into this as I'm just killing time before a flight (which I now must go board), but I thought I'd toss it out there as a quick idea for people to dissect and comment on.