Skip to main content
February 2, 2010
Question

Get calling script name

  • February 2, 2010
  • 2 replies
  • 677 views

Let's assume I have the following:

fileA.cfm > includes fileB.cfm > includes fileC.cfm

Within fileC.cfm, how can I figured out that fileB.cfm was the page that called it? I was looking through getPageContext() for any revalent information, but I'm stumped. If i use cgi.path_info that's invalid as it would give me fileA.cfm. This has to be something obvious that I'm missing

    This topic has been closed for replies.

    2 replies

    February 2, 2010

    One way would be to use the session scope...

    fileA.cfm source code:

    <cflock...>

    <cfset SESSION.last_source_location = GetFileFromPath(CGI.CF_TEMPLATE_PATH)>

    </cflock>

    <cfinclude template="fileB.cfm">

    fileB.cfm source code:

    <cflock...>

    <cfset SESSION.last_source_location = GetFileFromPath(CGI.CF_TEMPLATE_PATH)>

    </cflock>

    <cfinclude template="fileC.cfm">

    fileC.cfm source code:

    <cfif IsDefined("SESSION.last_source_location") AND Len(SESSION.last_source_location) AND SESSION.last_source_location IS "fileB.cfm">

         <!--- Source location was valid. --->

    <cfelse>

         <!--- Source location was  not valid. --->

    </cfif>

    <cfset SESSION.last_source_location = ""> <!--- Reset for use again. --->

    ilssac
    Inspiring
    February 2, 2010

    getPageContext() and the cgi structure are how the web server views your files and it only knows the one that was requested.  It has no idea what ColdFusion may be including to build that requested file.

    The getCurrentTemplatePath() and the getBaseTemplatePath() are probably more like the information you maybe wanting.

    Inspiring
    February 2, 2010

    That won't quite get it: getBaseTemplatePath() will only get the "bottom" one, and getCurrentTemplatePath() will only get the "top"one.

    As far as I know, the only way to get info about the intermediary templates are by creating an Exception object (coldfusion.runtime.CustomException), and looking at the tag context.

    --

    Adam