Using StructKeyExists() With CALLER Scope In ColdFusion Custom Tags

Posted September 23, 2008 at 9:29 AM

Tags: ColdFusion

A long while back, I blogged that the CALLER scope in ColdFusion custom tags acts a sort of struct with special behavior. In that post, I demonstrated that compound variable paths could be used as keys to the CALLER scope:

 Launch code in new window » Download code as text file »

  • <cfset CALLER[ "REQUEST.Foo.Bar" ] = objValue />

This call would store objValue into the Bar key of the Foo struct located in the REQUEST scope. In a traditional struct call, this would store objValue into the "REQUEST.Foo.Bar" key of the CALLER scope; but, like I said, CALLER has this special (and tremendously useful) behavior. In a different post, I even demonstrated that you could get variables out of the CALLER scope in a the same manner.

Anyway, where I'm going with this is that just this morning, I tried for the first time to use StructKeyExists() on the CALLER scope. It's one thing for an object in ColdFusion to overload the its array and struct operators, but it's another thing to have complete struct functionlaity. As it turns out, StructKeyExists() works with exactly the same sort of special behavior:

 Launch code in new window » Download code as text file »

  • <cfif StructKeyExists( CALLER, "REQUEST.Foo.Bar" )>

This does not check to see if the key "REQUEST.Foo.Bar" exists in CALLER (as it would in a normal struct call); this checks to see if the Bar key exists in the Foo struct located within the REQUEST scope of the CALLER scope.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 23, 2008 at 9:49 AM // reply »
111 Comments

@Ben,

Doesn't the Request scope exist in the custom tag on its own, regardless of the Caller scope? In other words:

If StructKeyExists(Caller, "Request.foo.bar") then StructKeyExists(Request, "foo.bar") also, right?

Maybe it's not totally relevant to the post, but just curious since Request is also a scope with its own behavior, such as being available across all templates in the request, much like a session struct.

-jfish


Sep 23, 2008 at 10:03 AM // reply »
7,572 Comments

@Jason,

That is totally true. In fact, I used the VARIABLES scope when I first wrote the code. The VARIABLES scope might have been a better demonstration since both the calling page the custom tag have their own VARIABLES scope.

But, at the last minute, I decided to change the example to use the REQUEST scope.


ike
Sep 29, 2008 at 3:23 PM // reply »
78 Comments

I haven't tested it, but I think there's a small problem with that last example where structKeyExists(caller,"request.foo.bar") and structKeyExists(request,"foo.bar") would both return the same value. That would be true if the request scope also has that same special behavior that the caller scope has... I haven't tried it, so I can't say for certain, but I *think* that structKeyExists(request,"foo.bar") will look for the key "foo.bar" rather than looking for the key "bar" in the structure "foo". Personally I'm not so sure I like that behavior from the caller scope -- I think it has more potential to lead to confusion than it has really potential to help (as in the example here). YMMV of course.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 22, 2010 at 3:08 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Thanks for the response. I finally discovered that I was getting this error because I had cfsetting enablecfoutputonly="yes" in Application.cfc, and was neither setting it to false elsewhere nor brac ... read »
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »