Skip to main content
September 18, 2009
Answered

Trouble writing CFML to a file

  • September 18, 2009
  • 2 replies
  • 824 views

I am trying to create a CFC that will write a file with CFML in it using code like the following example...

    <cffile action="write" file="C:\ColdFusion8\wwwroot\T\test.cfm" output="<cfmenu name=""test"">">

but ColdFusion keeps trying to read what is in the output="". Is there any way to make it so that ColdFusion doesn't try to read the output text as CFML?

This topic has been closed for replies.
Correct answer Adam Cameron.

Yep.

Do it the way I showed you in that other thread.  Basically the way to get the CF compiler to not compile CFML instructions which are actually part of a string, rather than code, is to "escape" the tags.

Relevant snippet from other thread:

    <cfsavecontent variable="sMenus">
        <cfset sCfO = "<" & "cf">
        <cfset sCfC = "</" & "cf">
        <cfoutput>
            #sCfO#menu    type="horizontal">
                <cfquery name="qMenus" dbType="query">

                    [snip]

                </cfquery>
                <cfloop query="qMenus">
                    #sCfO#menuitem display="#label#" href="#CGI.script_name#?id=#id#" name="sub_#parentId#_#id#">
                        #getSubmenus(menuData=qMenuData, parentId=id)#
                    #sCfC#menuitem>
                </cfloop>
            #sCfC#menu>

Where sCfO and sCfC ("CF open" and "CF close") are variables holding "<cf" and "</cf" respectively.  So when creating your string, you have "#sCfO#menu" which will contain "<cfmenu".

Make sense?

--

Adam

2 replies

BKBK
Community Expert
Community Expert
September 19, 2009

I am trying to create a CFC that will write a file with CFML in it using code like the following example...

<cffile action="write" file="C:\ColdFusion8\wwwroot\T\test.cfm" output="<cfmenu name=""test"">">

but ColdFusion keeps trying to read what is in the output="". Is there any way to make it so that ColdFusion doesn't try to read the output text as CFML?

There is nothing wrong with the code. Coldfusion will read what is in the output attribute as text, and wont try to execute it.

However, you're copying CFML code to the file, test.cfm, which you save as CFM. Hence, when you open the file in the browser,  Coldfusion will run it like it would any other CFM file. Nothing special about that. One solution is to escape the tags, as follows

<cffile action="write" file="C:\ColdFusion8\wwwroot\T\test.cfm" output="#htmleditformat('<cfmenu name=""test"">')"#>

Adam Cameron.Correct answer
Inspiring
September 19, 2009

Yep.

Do it the way I showed you in that other thread.  Basically the way to get the CF compiler to not compile CFML instructions which are actually part of a string, rather than code, is to "escape" the tags.

Relevant snippet from other thread:

    <cfsavecontent variable="sMenus">
        <cfset sCfO = "<" & "cf">
        <cfset sCfC = "</" & "cf">
        <cfoutput>
            #sCfO#menu    type="horizontal">
                <cfquery name="qMenus" dbType="query">

                    [snip]

                </cfquery>
                <cfloop query="qMenus">
                    #sCfO#menuitem display="#label#" href="#CGI.script_name#?id=#id#" name="sub_#parentId#_#id#">
                        #getSubmenus(menuData=qMenuData, parentId=id)#
                    #sCfC#menuitem>
                </cfloop>
            #sCfC#menu>

Where sCfO and sCfC ("CF open" and "CF close") are variables holding "<cf" and "</cf" respectively.  So when creating your string, you have "#sCfO#menu" which will contain "<cfmenu".

Make sense?

--

Adam

September 19, 2009

Ah, I see. Very clever. Thanks. Don't take it personally if I don't copy/paste your code. I like to recode stuff people give me so that I can understand it better and also so it is my own creation.