Skip to main content
December 7, 2009
Question

Saving CF codes in a variable

  • December 7, 2009
  • 2 replies
  • 850 views

Hi,

I am trying to save some rather lengthy CF codes in a variable, ie. <cfset mycode = " ... all the codes here ...">.  However the codes to be saved may contain double quotes (") which would create a conflict.  I know of cfsavecontent which does not require double quotes; however that tag would process the codes rather than save them as string.

Is there an alternative way to saving the codes as a string in a variable?

Thanks,

MCL

This topic has been closed for replies.

2 replies

Inspiring
December 7, 2009

For your specific question, use single quotes in your cfset tag.

<cfset mycode = '<cfset somevar="xyz">'>

Once you accomplish this, what do you intend to do with the variable?

December 8, 2009

Hi,

I am trying to create an application that helps our users send out emails to clients.  The idea is for users to be able to enter in their own html email which may include dynamic variables (eg. Dear #firstName#,).  For some reasons, I wasn't able to save this input into a variable.  I ended up having to save them in a file and call it as a template using cfinclude.

MCL

Inspiring
December 8, 2009

You would have had to write the variable to a file and include the file anyhow if you wanted the code to execute.

Given the way you want to use this, make sure you test with a name containing an apostrophe.

Inspiring
December 7, 2009

Try like this,

<cfset CodeVar=StructNew()>
<cfset CodeVar='<cfset a="10"><cfset b="20"><cfset c=a+b>'>
<cfdump var="#CodeVar#">

HTH

December 7, 2009

Hi HTH,

Thanks for the suggestion.  I am actually trying to avoid using <cfset x = '  '> or <cfset x = " "> as I do not know in advance whether the cf codes have any double or single quotes.

MCL

Inspiring
December 7, 2009

Then use the HTMLCodeFormat function,

In the below example, I am just reading a file content and display the same in the browser.


<cffile action="read" file="#GetDirectoryFromPath(Getcurrenttemplatepath())#cfform_tab_ex2.cfm" variable="fileContent">
<cfoutput>#HtmlCodeFormat(fileContent)#</cfoutput>

HTH