Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a cfm file with some parameters in ColdFusion?

Is it possible to call a cfm file from another cfm file with some URL parameters?

cfinclude does not works as it simply includes the content only..

like image 355
Deepak Kumar Padhy Avatar asked Nov 21 '13 16:11

Deepak Kumar Padhy


3 Answers

When you use a <cfinclude>, any values available in the page that calls the file are available in the page you will be including.

So you could essentially pass in variables instead of URL parameters and achieve the same thing. You would have to declare the variables above the include, however.

example.cfm

<cfset x = 5 />
<cfinclude template="derp.cfm" />

derp.cfm

<cfif IsDefined("x")>
    <cfoutput>#x#</cfoutput> //this should output 5 since it was declared before the include
</cfif> //always good to make sure they're defined first
like image 184
Sterling Archer Avatar answered Nov 18 '22 07:11

Sterling Archer


You could also pass a variable through a cfmodule call (cfmodule template="myfile.cfm" x="5"). But that quickly gets a lot more complex and can be a bit excessive for a simple operation. I guess it would depend on what your called page needs to do and if it matters if the guts of your called page are exposed to the rest of the page. But then you're well into the realm of "I should probably do this in a CFC".

cfmodule is a possibility, but not one I'd recommend without a reason. If you have to have this level of encapsulation, I'd probably do it in a CFC.

like image 35
Shawn Avatar answered Nov 18 '22 07:11

Shawn


For my case I did in this manner,

<cfset URL.productId = 456>
<cfset URL.templateId = 2066>
<cfinclude template="mytemplate.cfm">

So in the my template page both productId and templateID will be available in URL scope.

like image 35
Deepak Kumar Padhy Avatar answered Nov 18 '22 08:11

Deepak Kumar Padhy