Skip to main content
Inspiring
November 14, 2009
Question

Can't Access Queries From application.cfm In Custom Tags

  • November 14, 2009
  • 3 replies
  • 1043 views

Maybe someone can help with this one... I've run into this in the past and just skirted around it, but now it's driving me crazy.

Why don't custom tags have access to data from queries in my application.cfm file?

For example, I have a query in my application.cfm file that pulls information about a currency in which a user has specified as their default currency (stored as a cookie), and I want to use that information within a custom tag that is later used to render a page of the shopping cart.

Unfortunately ColdFusion doesn't see the query. Are custom tags somehow run before/ignore the root application.cfm file?

    This topic has been closed for replies.

    3 replies

    Inspiring
    November 16, 2009

    I think I need to explain this one better. I have two files, application.cfm, and custom_tag.cfm. For sake of example, here they are:

    ===============================

    application.cfm

    ===============================

    <CFQUERY NAME="example_query" DATASOURCE="#request.datasource#" DBTYPE=ODBC MAXROWS=1>
         SELECT my_field

         FROM my_database

    </CFQUERY>

    ===============================

    custom_tag.cfm

    ===============================

    <CFOUTPUT>#example_query.my_field#</CFOUTPUT>

    ===============================

    Error Received

    ===============================

    Element MY_FIELD is undefined in EXAMPLE QUERY

    ===============================

    So basically I have a query that gets run within application.cfm, and I have a custom tag in which I want to display that data, but the custom tag acts as if that query has never run, even though I can output the query data just before the custom tag without fail. It's almost like queries done in application.cfm are completely invisible to custom tags. I know there's tons of ways around this, but I guess I'm more curious why this is the case?

    Inspiring
    November 16, 2009

    I can output the query data just before the custom tag without fail...

    but I guess I'm more curious why this is the case?

    When the query is executed in application.cfm it is placed in the "variables" scope of your main page. If you want to access the variables scope of the main page, from your custom tag, you must use the "caller" scope.

    http://livedocs.adobe.com/coldfusion/8/htmldocs/reuseCode_3.html#1122289

    Though, it is not the best way to pass information to a custom tag. If it is a single query, it would be cleaner to pass it in as an attribute.

    BKBK
    Community Expert
    Community Expert
    November 14, 2009
    information about a currency in which a user has specified as their
    default currency (stored as a cookie), and I want to use that
    information within a custom tag that is later used to render a page of
    the shopping cart.

    Your description is a classic use-case for a session variable. I would therefore use Chris Bowyer's example, but with session in place of request.

    Inspiring
    November 14, 2009

    Yep: session sounds better here.  This would also mean the data could be queried once per session, not on every request (it's not the sort of thing that would be changing on a request-byrequest basis, would it?)

    I'd consider passing the data into the tag though, rather than accessing it directly via the scope.

    --

    Adam

    chrisbowyer
    Known Participant
    November 14, 2009

    Have you tried using the request scope...

    <!--- Application.cfm --->

    <cfset request.dsn = myDsnName />

    <!--- myCustomTag.cfm --->

    <cfquery datasource="#request.dsn#">