CFSCRIPT ColdFusion cheat sheets
Набор подсказок для работы с cfscript в ColdFusion
FOR Loop
1 2 3 | for (i=1;i LTE ArrayLen(array);i=i+1) { WriteOutput(array[i]); } |
User Defined Function (UDF)
1 2 3 4 5 6 7 | function funky(mojo) { var dojo = 0; if (arguments.mojo EQ dojo) { return "mojo"; } else { return "no-mojo"; } } |
Switch Case
1 2 3 4 5 6 7 8 9 10 | switch(fruit) { case "apple": WriteOutput("I like Apples"); break; case "orange": WriteOutput("I like Oranges"); break; default: WriteOutput("I like fruit"); } |
If / Else If / Else
1 2 3 4 5 6 7 8 9 | if (fruit IS "apple") { WriteOutput("I like apples"); } else if (fruit IS "orange") { WriteOutput("I like oranges"); } else { WriteOutput("I like fruit"); } |
While Loop
1 2 3 4 5 6 | x = 0; while (x LT 5) { x = x + 1; WriteOutput(x); } //OUTPUTS 12345 |
Do / While Loop
1 2 3 4 5 6 | x = 0; do { x = x+1; WriteOutput(x); } while (x LTE 0); // OUTPUTS 1 |
Assign a variable
1 | foo = "bar"; |
Single Line Comment
1 | mojo = 1; //THIS IS A COMMENT |
CFScript Component
CF9+
1 2 3 4 5 6 7 8 9 10 11 12 | component extends="Fruit" output="false" { property name="variety" type="string"; public boolean function isGood() { return true; } private void eat(required numeric bites) { //do stuff } } |
Try / Catch / Throw / Finally / Rethrow
CF9+
1 2 3 4 5 6 | try { throw(message="Oops", detail="xyz"); // } catch (any e) { WriteOutput("Error: " & e.message); rethrow; // } finally { // WriteOutput("I run even if no error"); } |
FOR IN Loop (Structure)
1 2 3 4 5 6 7 | struct = StructNew(); struct.one = "1"; struct.two = "2"; for (key in struct) { WriteOutput(key); } //OUTPUTS onetwo |
Multiline Comment
1 2 3 4 | /* This is a comment that can span multiple lines */ |
FOR IN Loop (Array)
CF9.0.1+
1 2 3 4 5 | cars = ["Ford","Dodge"]; for (car in cars) { WriteOutput(car); } //OUTPUTS FordDodge |
FOR IN Loop (Query)
CF10+
1 2 3 4 5 6 7 | cars = QueryNew("make,model", "cf_sql_varchar,cf_sql_varchar", [["Ford", "T"],["Dodge","30"]]); for (car in cars) { WriteOutput("Model " & car.model); } //OUTPUTS Model TModel 30 |
CFInclude
CF9+
1 | include "template.cfm"; |
Transactions
CF9+
1 2 3 4 5 6 7 8 | transaction { //do stuff if (good) { transaction action="commit"; else { transaction action="rollback"; } } |
Struct Literal
CF8+
1 | product = {id=1, name="Widget"}; |
Array Literal
CF8+
1 | fruit = ["apples", "oranges"]; |
Tip: Don’t forget to include a semicolin
;
after each statement!