Title: CFSCRIPT
1CFSCRIPT
2 types of modern scripting for the web
Tag based
Script based
2CFSCRIPT
- Block tags
- Whatever is inside will be executes (evaluation
zone) - No tags allowed
- Each expression must be terminated by a
semi-colon () - Assignment is simply variablenamevalue
ltCFSCRIPTgt Name"Michael" Age29 DateNow()
Sum117 AgeNextAge1 lt/CFSCRIPTgt
String Number Function Operation Expression
3CFSCRIPT
- 2 forms of comments
- Comments needed as the code will be more
obfuscated
ltCFSCRIPTgt // This is a one line comment. Use
this to comment on a code snippet / This is
a multi-line comment Use this when writing docs
at the top of a CFACRIPT block / lt/CFSCRIPTgt
4CFSCRIPT
- Execution functions (see handout) DO NOT NEED to
be assigned to a variable, especially in CFSCRIPT
ltCFSCRIPTgt // This will clear the session
structure StructClear(session) // This will
disconnect the data sources cfusion_dbconnection
s_flush() // Reset a query cell
QuerySetCell(myquery, 'name', 'Michael',
1) lt/CFSCRIPTgt
5CFSCRIPT
- Pay attention to standard Reserve Words
- New Reserve Words
6CFSCRIPT If
ltCFSCRIPTgt // Standard IF statment IF (score GT
1) result "positive" // If statment with
multiple parts in the if IF (score GT 1)
result "positive" totalscorescore1 lt
/CFSCRIPTgt
Statment
code block - seen as one complete unit
7CFSCRIPT IF - Else
ltCFSCRIPTgt // If with an Else IF (score GT
1) result "positive" ELSE result
"negative" // If, Else, Else If IF (score GT
1) result "positive" ELSE IF (score EQ
0) result"null" ELSE result
"negative" lt/CFSCRIPTgt
Sub IF statment
8CFSCRIPT Switch
ltCFSCRIPTgt switch(name) case
"John" maletrue break case
"Mary" malefalse break default
foundfalse break //end
switch lt/CFSCRIPTgt
9CFSCRIPT For Loop
- For loop
- FOR (init-expr test-expr final-expr)
statement - init-expr and final-expr can be
- single assignment expression, for example, x5
or looploop1 - any ColdFusion expression, for example,
SetVariable("a",a1) - empty
- The test-expr can be
- any ColdFusion expression, for example, A LT 5,
loop LE x, or Y EQ "not found" AND loop LT - empty
10CFSCRIPT For Loop
ltCFSCRIPTgt // Loop from 1 to 10 for(Loop11
Loop1 LT 10 Loop1 Loop1 1) scorescoreloop
1 // Loop from 1 to 10 for(Loop11 Loop1
Loop1 1) scorescoreloop1 IF (Loop1
EQ 10) break lt/CFSCRIPTgt
11CFSCRIPT While Loop
ltCFSCRIPTgt // While expression is true a
ArrayNew(1) while (loop1 LT 10) aloop1
loop1 5 loop1 loop1 1 // While
expression is true a ArrayNew(1) while
(loop1 LT 10) IF (loop1 mod 2 EQ
0) continue loop1 loop1
1 lt/CFSCRIPTgt
12CFSCRIPT Do-While Loop
ltCFSCRIPTgt // Multiline do-while loop // Tests
AFTER code is run a ArrayNew(1) do al
oop1 loop1 5 loop1 loop1
1 while (loop1 LT 10) lt/CFSCRIPTgt
13CFSCRIPT For-In Loop
Special loop for dealing with collections (I.e.
structures) for (variable in collection)
statement
ltCFSCRIPTgt for (x in Session) WriteOutPut(Sess
ionxltbrgt) lt/CFSCRIPTgt