20

If I do something like this in ColdFusion:

<cfoutput>foo="#foo()#"</cfoutput>

The resulting HTML has a space in front of it:

foo=" BAR"

However, if it is not a function call it works fine, i.e.:

<cfset fooOut=foo() />
<cfoutput>foo="#fooOut#"</cfoutput>

Gives this output:

foo="BAR"

Where is this extra space coming from and is there anything I can do about it?


Edit To clarify, the space is not in the value returned by my foo function:

<cffunction name="foo" access="public" returntype="string">
  <cfreturn "BAR" />
</cffunction>

But I've also found that this doesn't happen with built-in functions, i.e.:

<cfoutput>"#UCase("bar")#"</cfoutput>

Prints:

"BAR"

However, it does happen if I pass the output of my function to the built-in function (this part makes no sense to me). i.e.:

<cfoutput>"#UCase(foo())#"</cfoutput>

Prints:

" BAR"
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Kip
  • 107,154
  • 87
  • 232
  • 265
  • This is definitely a hack and doesn't answer your question, but have you tried using the Trim() function to remove the whitespace? – dbyrne May 07 '10 at 21:41
  • @dbyrne: i tried that, but the value returned by foo() doesn't have a space to begin with, so the trim does nothing, and then the result of trim gets a space added to it, just like the result of foo() – Kip May 07 '10 at 21:42

2 Answers2

29

Make sure you have output attribute defined as false.

<cfcomponent output="false">

  <cffunction name="foo" access="public" returntype="string" output="false">
    <cfreturn "BAR">
  </cffunction>

</cfcomponent>

Or, do it in cfscript style, and no extra space will be introduced.

function foo()
{
  return "BAR";
}
Henry
  • 32,689
  • 19
  • 120
  • 221
1

See if this helps http://www.simonwhatley.co.uk/eliminating-whitespace-in-coldfusion

Sharjeel Aziz
  • 8,495
  • 5
  • 38
  • 37