September 20, 2012

Whitespace issues in cfscript based functions

Ok now we all know that ColdFusion is kind of lame when it comes to whitespace management, unless you specifically have “whitespace management” turned on for your server.

I have a function that is used to build the href value of a hyperlink. Nothing special there. However when I viewed the source of the page I was a little surprised to see a mountain of whitespace coming back from the function.

All I do is return a string from the function, I don’t output any text from within the function.

Here it is:

public string function getFullPageURI(required string pagename)
{
  var pages = new lib.services.pages();
  var page = pages.getPageByNavURL(arguments.pagename, getId());
  return page.getPageURI();
}

And the link source:

Hmm, could that suck any more? Now what happens if I add output=“false” (not particularly intuitive) to the end of the function, like this:

public string function getFullPageURI(required string pagename) output="false"
{
  var pages = new lib.services.pages();
  var page = pages.getPageByNavURL(arguments.pagename, getId());
  return page.getPageURI();
}

Now I get:

Sorry but that’s just insane. cfscript should behave like cfsilent all the time unless you specify a writeOutput();

Hope this helps any other people out there. The lesson learned is that you can specify output=“false” in a cfscript based function, but ONLY AFTER THE METHOD SIGNATAURE!

So if you did this:

public string output="false" function getFullPageURI(required string pagename)

If would fail. It needs to occur after the parens.

© Michael Sharman 2017