Skip to main content
WolfShade
Legend
June 24, 2019
Answered

CFINCLUDE and URL parameters

  • June 24, 2019
  • 1 reply
  • 4879 views

Hello, all,

Has anyone else had any issues using URL parameters with a CFINCLUDE?

I've got something like:

<cfinclude template="#app_base#common/thisfile.cfm?its=1" />

app_base is a variable that sets the root depending upon what environment I'm in.  In DEV, it's "/public/".  In staging, it's "/preview/public/".  In production, it's "/".

I've got one page that is trying to use CFINCLUDE three times, so the its value changes with each include.

But I'm getting error messages stating that "ColdFusion cannot find /preview/public/common/thisfile.cfm?its=1".  It's there, I can see it in the FTP.

Thoughts?

V/r,

^ _ ^

    This topic has been closed for replies.
    Correct answer Dave Watts

    I tested this further by removing the URL parameter from the CFLOCATION, and it works.

    ????

    V/r,

    ^ _ ^


    I didn't even see that the first time around. That's your problem right there!

    CFINCLUDE doesn't work that way. It doesn't execute the other file, then bring its contents back into your file. Instead, it brings the contents of the other file into the including file before the whole thing is then executed. So, if you want to have something like this happen, you have to set a variable in the including file, then reference it within the included file. You could, for example, have something like this:

    <!--- the including file --->

    <cfset URL.its = "1">

    <cfinclude template="/path/to/thisfile.cfm">

    and then ...

    <!--- thisfile.cfm --->

    <!--- do something with URL.its --->

    Sorry I missed that the first time around.

    Dave Watts, Eidolon LLC

    1 reply

    Community Expert
    June 24, 2019

    CFINCLUDE relies on CF mappings when the first character is a slash. So you'd need to have a mapping for either /preview or / that resolves to the location of the preview directory. You probably already know that, but it's just the first thing that comes to mind here.

    Also, I would recommend not having one site nested within another, which is what it looks like you have here. This is an unrelated problem, but just isn't generally recommended.

    Dave Watts, Eidolon LLC

    Dave Watts, Eidolon LLC
    WolfShade
    WolfShadeAuthor
    Legend
    June 24, 2019

    Dave,

    Is this supposed to be done under Mapping?  Or Sandbox Security?  Or both?  (I'm assuming Mapping, but want to be sure.)

    V/r,

    ^ _ ^

    Community Expert
    June 24, 2019

    So, waitaminnit.. you can set a variable in the URL scope in the containing file, and it will be read by the included file?????

    That's so intuitive! /s   (SMH)

    I'll give that a shot.. thank you for the heads-up.

    V/r,

    ^ _ ^


    Well, in a certain context, it makes perfect sense. The problem is that you have to be aware of that context to understand why it makes sense. The way CFINCLUDE is supposed to work is almost literal - you are including one file within another. It basically gives you one big file when you're done. So, within that context, of course you can't pass arguments to the included file, because it doesn't make any sense for that file to have URL parameters or any other inputs as it's not being invoked by an HTTP request.

    Under the covers, it may not actually even work that way for all I know, but that's the "interface" for how we're supposed to understand CFINCLUDE, or includes in other languages/environments.

    Dave Watts, Eidolon LLC

    Dave Watts, Eidolon LLC