Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 412
user1666910 Avatar asked Jan 15 '23 11:01

user1666910


1 Answers

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">
like image 74
barnyr Avatar answered Jan 25 '23 18:01

barnyr