Title: Bracketology brak'itol'je
1Bracketology(brak'it-ol'?-je)
-noun 1. The science of efficiently using
brackets in ltCFMLgt
- Brian Love
- Hamilton College
- Web Integration Specialist
2Bracketology (brak'it-ol'?-je)
- Overview
- What is this bracket thing all about?
- Object.property notation
- Associative array notation
- How it all works
- Embedding brackets
- Mixing it up
- Avoiding Evaluate()
- Looping over arrays, structures, and queries
- Using brackets with objects
3Bracketology (brak'it-ol'?-je)
- Everything is ColdFusion is a structure (scoped)
- Variables (and this scope)
- Form
- Url
- Cgi
- Cookie
- Client, Cookie, and Session
- Application
- Server
- We can refer to complex structures using two
methods - object.property notation
- Associative array notation
4Object.property notation
- Sometimes referred to as dot notation
- Example
ltcfscriptgt mystr StructNew() mystr.key1
ColdFusion rocks mystr.key2 CF makes
coding fun lt/cfscriptgt ltcfoutputgt Key 1
value mystr.key1 ltbr /gt key 2 value
mystr.key2 lt/cfoutputgt
5Associative Array notation
- What the heck is the associative array stuff all
about? - Definition by Wikipedia
- An associative array is an abstract data type
composed of a collection of keys and a collection
of value, where each key is associated with one
value.
6Associative Array notation
- By using brackets we can perform the following
operations on structures in ColdFusion - Add binding a new key to a new value
- Lookup Find the value that is bound to a key
ltcfset myStr StructNew()gt ltcfset myStrkey1
value 1gt
ltcfoutputgt myStrkey1 lt/cfoutputgt
7How does it work?
- Anything inside of the brackets must be a valid
ColdFusion expression - Basically, think of it like cfset
- What is inside of the bracket is the right side
- Since the variable i is 1, myVar will be set to
guest1 - The difference Instead of returning the value to
a variable (in this case, myVar), the value is
returned to the whole expression to be evaluated
(and then acted upon)
ltcfset i 1gt ltcfset myVar guest i gt
8Embedding brackets
- No limit to the number of embedded
- Brackets are evaluated from the inside out
variablesbracketswithinbrackets
3
1
variablesbracketswithinbrackets
2
4
9Working from the inner-most out
- So, first we have the whole expression
- Then, the inner-most bracket is evaluated
- Then, the entire expression is evaluated (after
all brackets have been evaluated)
ltcfset i 1gt ltcfoutputgt formguesti
lt/cfoutputgt
ltcfoutputgt formguest1 lt/cfoutputgt
ltcfoutputgt form.guest1 lt/cfoutputgt
10Brackets are expressions
ColdFusion expressions consist of operands and
operators.
Operands are comprised of constants and variables
- So, what is in between the brackets, must be a
valid ColdFusion expression. - Most common uses
- VariablesmyVar something myVar2
- VariablesmyVarsomethingmyVar2
11Bracketology example 1
- A structure containing account numbers
- A query that is a transaction log
- Lets find out the ending balance for each account
and store the results in a structure
12Mixing it up
- ColdFusion also allows you to mix both
object.property notation with associative array
notation
ltcfscriptgt myStr StructNew() myStrkey1
value 1 myStrkey2 ArrayNew(1)
myStr.key21 Array value 1 myStr.key2.2
Array value 2 lt/cfscriptgt
13Do you do this
ltform ...gt ltcfloop from1 to10 indexigt
ltcfinput nameguesti ...gt
lt/cfloopgt lt/formgt
..And then
ltcfloop from1 to10 indexigt ltcfoutputgt
Evaluate(guesti) lt/cfoutputgt lt/cfloopgt
14Avoiding Evaluate()
You can concatenate the string guest with the
variable i
ltcfloop from1 to10 indexigt ltcfoutputgt
formguesti lt/cfoutputgt lt/cfloopgt
Or, you can evaluate the string guesti
ltcfloop from1 to10 indexigt ltcfoutputgt
formguesti lt/cfoutputgt lt/cfloopgt
15Timing Evaluate()
Test performed using CF8 Developer Edition with
CFTIMER running in standard mode with built-in
web server on Windows Vista Ultimate platform
with 2.80 GHz Pentium D and 2046MB RAM.
16Looping over arrays
- We can easily loop over the elements of a
ColdFusion array using brackets and the for loop
ltcfscriptgt myArray value 1, value 2,
value 3 //loop over array and output to
browser for(i1 i lt ArrayLen(myArray) i)
WriteOutput(myArrayi ltbr /gt)
lt/cfscriptgt
17Looping over structures
- We can loop over the elements in a ColdFusion
structure using brackets and the for-in loop
ltcfscriptgt myStr StructNew() myStrkey1
value 1 myStr.key2 value 2 //loop
over struct and output to browser for(key in
myStr) WriteOutput(myStrkey ltbr /gt)
lt/cfscriptgt
18Looping over queries
- We can easily loop over the elements of a
ColdFusion query object using brackets and the
for loop
ltcfscriptgt myQry QueryNew("values") for
(i1 i lte 3 i) QueryAddRow(myQry)
QuerySetCell(myQry,"values","Query value "i)
//loop over array and output to browser
for(i1 i lt myQry.recordcount i)
WriteOutput(myQry'values'i 'ltbr /gt')
lt/cfscriptgt
19Using brackets with objects
- We have a CFC with a generic setter and getter,
i.e. - userObj.get(name)
- If defined, we would like to call a custom getter
function, i.e. - userObj.get(name) will in turn call
- getCustomName() that will return the value.
20Using brackets with objects
- We can use bracket notation to reference the
custom function like this
ltcfcomponent nameusergt ltcffunction name"get"
output"false" access"public"gt ltcfargument
name"field" required"yes" /gt ltcfset var
newfuncname "" /gt ltcfif
structkeyexists(variables,'getarguments.field')
AND isCustomFunction(variables'get'argumen
ts.field)gt ltcfset newfuncname
variables'get'arguments.field /gt
ltcfreturn newfuncname() /gt lt/cfifgt
return value from data struct
lt/cffunctiongt lt/cfcomponentgt
21Thank youPresentation Examples available at
brianflove.com
- Brian Love
- Hamilton College
- Web Integration Specialist