Search code examples
coldfusioncoldfusion-9coldfusion-10

ColdFusion include


I'm currently learning ColdFusion. I have a background in PHP and I am a bit confused by this.

I have a select menu and I want the options to be saved in a different file. (For example options.cfm) When I call the file I want to include the options inside the select menu. Now I realize I could probably do it with something like this:

<select>
    <cfinclude template="options.cfm">
</select>

Although what I really want to do is a bit more complicated. I want to have the cfinclude saved inside a variable. I realize this won't work but it is basically what I want to accomplish:

<cfset options=<cfinclude template="options.cfm">>

Is there anyway to do that? Or at least a better way to accomplish what I am doing.


Solution

  • Take a look at the cfsavecontent tag, It allows you to capture what would otherwise have been output to the response :

    <cfsavecontent variable="options">
        <cfinclude template="options.cfm">
    </cfsavecontent>
    

    UPDATE: Instead of using cfsavecontent every time you need those options saved to a variable, you could instead do it once inside of the options.cfm file. Then, anytime you include the file, it will create the variable.

    <!--- Inside options.cfm --->
    <cfsavecontent variable="options">
        <option value="val1">Value 1</option>
        <option value="val2">Value 2</option>
        <option value="val3">Value 3</option>
    </cfsavecontent>
    

    Then where ever you needed that variable to exist you would simply need to cfinclude that file.

    <cfinclude template="options.cfm">