0

I have a CFC that returns a string containing part of a URL. I want to concatenate this to the end of the domain name of the site so it makes a fully qualified URL.

However, the ColdFusion is creating a space before the concatenation. Here is how my concatenation looks:

http://www.mywebsite.com#APPLICATION.MyCFC.GetURL(urlid = url.id)# 

So we have two parts:

  1. The domain part which is just http://www.mywebsite.com
  2. The string that's returned from the CFC which is like this /products/20

However the final output ends up like this:

http://www.mywebsite.com /products/20

So for some reason it puts a space just before concatenating the string from the CFC. I have tried to put a Trim() around the CFC invokation but it doesn't do anything.

What I have also tried to do is put the string from the CFC in a variable like this <cfset myurl = #APPLICATION.MyCFC.GetURL(urlid = url.id)#. I then concatenated this variable to the domain like this: http://www.mywebsite.com/#url# and it works fine without adding any spaces.

Why is it doing this? I don't want to keep storing the output of the CFC in yet another local variable everytime I want to use it.

This is the code from the CFC (I've left out the database stuff that it does for sake of confidentially and clarity but its essentially just this):

<cffunction name="GetURL" access="public" returntype="string">
  <cfargument name="urlid" required="yes">
  <cfset var result="/products/#urlid#">
  <cfreturn result>
</cffunction>
volume one
  • 6,800
  • 13
  • 67
  • 146
  • Post your `MyCFC.GetURL` function? You may not want to use `url` as a var name since it's also the name of a scoope – Henry Nov 26 '14 at 22:18
  • That was a typo sorry. Its called myurl. I have updated my question to include the CFC code. – volume one Nov 26 '14 at 22:20
  • 1
    Please show the code immediately before and after the line where you output the results of the CFC call. – Carl Von Stetten Nov 26 '14 at 22:30
  • @CarlVonStetten I'm not sure what you mean? I am using it like this `` which produces a space after the .com part. But if I do it like this `` using value from the CFC stored in the `myurl` local variable on the page then theres no space after the .com – volume one Nov 26 '14 at 22:40
  • I was working toward the answer Henry provided. I wanted to rule out whitespace being generated by preceding code. – Carl Von Stetten Nov 26 '14 at 22:53
  • I don't understand why using output=false makes a difference. According to the CF docs, it stops variables surrounded in hashes from resolving. But yet the #urlid# and other variables in there seem to be resolving. – volume one Nov 26 '14 at 22:55
  • No, the docs say "no: the function is processed as if it were within a cfsilent tag." (https://wikidocs.adobe.com/wiki/display/coldfusionen/cffunction); "no: Constructor code is processed as if it were within a cfsilent tag." (https://wikidocs.adobe.com/wiki/display/coldfusionen/cfcomponent) – Adam Cameron Nov 26 '14 at 23:26
  • how come the leading space is removed when I stored the same output from the CFC in a variable on the calling page? – volume one Nov 27 '14 at 11:05

1 Answers1

5

add output="false" to your <cffunction> (and <cfcomponent> if it is not an UDF) may solve your problem.

Henry
  • 32,689
  • 19
  • 120
  • 221
  • that has solved the issue! But I'm confused why I have to use this. I thought that output="false" means it won't resolve variables surrounded in hashes. And I'm using variables in my CFC. – volume one Nov 26 '14 at 22:50
  • 3
    Output="false" prevents any output to the content buffer from the function/component, including whitespace generated by line breaks (think of it like wrapping the function in `` tags. Unless a function /component is intended to output view content directly, output="false" should always be used. See https://wikidocs.adobe.com/wiki/display/coldfusionen/cffunction for more details. – Carl Von Stetten Nov 26 '14 at 23:01
  • or just get with the time and use cfscript for functions and cfc's. They don't generate extra whitespaces by default. – Henry Nov 26 '14 at 23:38
  • As much as I prefer cfscript, not everything is easy to write with it. The query syntax is still such a cluster**** that we don't allow it @ work. – Adrian J. Moreno Nov 26 '14 at 23:42
  • @AdrianJ.Moreno yes, I use cfml for DAO as well. `` is still the best. However, cfscript is awesome for everything else. – Henry Nov 27 '14 at 00:14
  • I don't agree that cfscript that is in anyway more advanced or better to use. The whole thing that attracted me to ColdFusion many years ago was that it was tag based. – volume one Nov 27 '14 at 15:41
  • @volumeone well at least you don't need to write `output="false"` on every `` for one. :) – Henry Nov 27 '14 at 18:34