[thelist] CF: Passing structures to custom tags - and more.

Jeff jeff at lists.evolt.org
Tue Sep 26 02:35:25 CDT 2000


<ol>,

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: From: Oliver Lineham <oliver at lineham.co.nz>
:
: Probably someone with a similar OO background
: might be able to give me help, as you'll know where
: I'm coming from with hierarchies and encapsulation
: and the like.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

don't have OO background specifically, but i think my experience with cold
fusion should make up for it.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: 1.  First, why can't I make a scoped variable a structure?
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

you can.  i do it all the time.

session.styles = StructNew()
session.styles.body = StructNew()
session.styles.body.margin = '0px'
session.styles.body.border = '0px'
session.styles.table = StructNew()
.....

i think you get my point.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Same problem with somevar["somekey"] - it
: must be initialised with ArrayNew(), which
: doesn't like working within a scope.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

but, you can refer to items within a struct by either dot notation or by
"key-within-brackets" notation.  ArrayNew() isn't even necessary.  in fact,
arrays with keys aren't even proper in cold fusion.  at that point they are
structures.  fwiw, you can also scope an array to any scope you like, just
like structures.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: 2.  Can anyone tell me why, if you want to pass a
: structure to a custom tag, (i) you can only pass a
: maximum of one structure (via ATTRIBUTECOLLECTION),
: and (ii) all contents of the structure get copied into
: the ATTRIBUTES scope within the tag?  That's crazy.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(i) attributescollection is not the only method to move a structure into a
custom tag - it's just the easiest because you only have to give the
variable that holds the structure.  you can also create your own attribute,
pass it a variable that's a structure and then do an evaluate on that
attribute within the custom tag to bring the structure into the custom tag.
i've done this with structures, arrays, and even queries with no trouble.

(ii) why wouldn't you want all contents of a structure to be copied into the
attributes scope within the tag?  how else do you propose to access the
values if they're not in the attributes scope?

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:   (i) Collect related variables together in a structure
: more than 1-deep.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

see above

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: For example, you might have a structure called people,
: and one thing all employees have is an address - which
: is another structure, containing house number, street, etc.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

people = StructNew()
people.address = StructNew()
people.address.housenumber = '13415'
people.address.street = 'main street'
people.address.city = 'portland'
people.address.state = 'oregon'
....
etc.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:   (ii) Pass multiple structures to a custom tag.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

again, see above.

<!--- call custom tag "mycustomtag.cfm" --->
<cf_mycustomtag attribute1="#struct1#" attribute2="#struct2#">


<!--- mycustomtag --->
<cfparam name="attributes.attribute1" default="">
<cfparam name="attributes.attribute2" default="">

<cfset mystruct1 = Evaluate('caller.' & attributes.attribute1)>
<cfif IsStruct(mystruct1)>
  do stuff to your struct here
</cfif>

<cfset mystruct2 = Evaluate('caller.' & attributes.attribute2)>
<cfif IsStruct(mystruct2)>
  do stuff to your struct here
</cfif>

the difficult part will be figuring out how to loop over the structs to grab
the appropriate data and doing this in a performance-minded manner.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:   (iii) Pass structures to custom tags without all their
: contents being dumped, like a items from a broken
: grocery bag, into the global scope within the tag.  If
: I've passed a structure, it's because its data is meant
: to be distinct from the other attributes I'm passing.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

i'm not sure where you get the idea that a structure that's brought into a
custom tag has it's data dumped in with everything else within the tag.
it's assigned to a single attributes scoped variable.  does what i've
supplied help address this concern?

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Note that an appropriate solution is not "Oh, just use
: CFINCLUDE instead of CFMODULE and then access
: anything you like".
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

no, that's not a valid solution to the problem.  of course, i'm not sure
that the use of cfmodule to call a custom tag is the solution either.  i
personally prefer the cf_ method of calling a custom tag as shown above.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Yuck.  What an insane language.  I think it was invented
: by a GW-BASIC coder from way back.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

speak poorly of it all you want (i understand that's easy to do when you're
frustrated), but the reality is it's extremely powerful and will let you do
most anything, provided you have a solid understanding of it's strengths and
weaknesses, advantages and disadvantages, features and limitations, etc.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: And while I'm in this bout of code-rage, I have to
: say that Allaire's documentation is pathetic.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

i've seen plenty of doco (for all sorts of languages) in my journey to this
point and i'd have to say there isn't much out there that's on par with
allaire's cold fusion documentation.  on top of that, there are a couple of
top-notch books available from noted author ben forta that basically rounds
out what's missing.  there's also no shortage of experienced cf programmers
willing to share their knowledge whenever possible.  all ya gotta do is ask.
of course, there's nothing like good, old-fashioned practice too.

good luck,

.jeff

name://jeff.howden
game://web.development
http://www.evolt.org/
mailto:jeff at members.evolt.org






More information about the thelist mailing list