Skip to content

Loading…

Workinprogress #75

Merged
merged 5 commits into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 81 additions and 21 deletions.
  1. +81 −21 cfml100mins.markdown
View
102 cfml100mins.markdown
@@ -12,6 +12,8 @@ ColdFusion Markup Language (CFML) is a web programming language, which is especi
2. Looping Through Results
7. [Arrays](#arrays)
8. [Structures](#structures)
+ 1. [structKeyExists Function] (#structKeyExists)
+ 2. [Looping](#structureLooping)
9. [Conditionals](#conditionals)
1. [If, Else If, & Else](#if)
2. [Looping](#loop)
@@ -28,9 +30,9 @@ CFML tag syntax resembles HTML and the CFML script syntax, CFScript, resembles E
And you want to learn CFML so here goes!
-<a name="syntax" />
+<a name="syntax" >
## 1. Syntax
-
+</a>
There are two ways to write CFML code. You can use tag or script syntax. For the examples, please focus on one or the other so this tutorial is not confusing.
CFML includes a set of instructions you use in pages. You will write one or more instructions in a file then run the file through a CFML engine. Three CFML instructions we will use in this tutorial are ```CFSET```, ```CFOUTPUT```, and ```CFDUMP```. ```CFSET``` is used to create a variable and assign it a value. Also ```CFSET``` is used to call methods. ```CFOUTPUT``` displays a variable's value. ```CFDUMP``` is used to display the contents of simple and complex variables, objects, components, user-defined functions, and other elements.
@@ -117,9 +119,10 @@ end
s = Sample.new
puts s.hello
```
-<a name="variables" />
-## 2. Variables
+<a name="variables" >
+## 2. Variables
+</a>
Everything needs a name so we can refer to it. A variable, like in math, is just a name for a piece of data. In CFML, variables are very flexible and can be changed at any time. Variables are assigned using a single equals sign ```=``` where the **right** side of the equals sign is evaluated first, then the value is assigned to the variable named on the **left** side of the equals.
Go into a CFML file, enter in these example instructions, and observe the output that CFML gives you back:
@@ -192,9 +195,9 @@ writeOutput("b = #b#<br/>");
*The first few lines in the first example are simple if you've done any programming language before, but the last few get interesting when combining strings and numbers. The code looks a little messy since after each instruction we output a variable.
-<a name="cfc" />
+<a name="cfc" >
## 3. Components, Methods, and Parameters
-
+</a>
### Components (aka Objects or Classes)
In CFML, a ColdFusion component (CFC) is a file that contains data and methods. Components are the building blocks for objects in CFML. Objects know information, called "attributes", and can do actions, called "methods". In ColdFusion the ```cffunction``` tag is used to define methods within a tag-based CFC. In a script-based CFC, you use the ```function``` keyword to define a method.
@@ -324,9 +327,10 @@ component name = "PersonalChef" {
}
}
```
-<a name="strings" />
-## 4. Strings
+<a name="strings" >
+## 4. Strings
+</a>
In CFML a string is defined as a quote ( **'** or **"** ) followed by zero or more letters, numbers, or symbols and followed by another quote ( **'** or **"** ). Some simple strings would be **hello** or **This sentence is a string!**. Strings can be anything from "", the empty string, to really long sets of text. This whole tutorial, for instance, is stored in a string. Strings have a few important instructions that we'll use.
### Len
@@ -469,9 +473,10 @@ message = "Happy #today#!";
If you compare the output you'll see the second example gives the exact same results. The code itself is a little more compact and, personally, I find it much easier to read.
Basically *interpolating* means evaluate the code inside this ```#``` wrapper and put it into the string.
-<a name="numbers" />
-## 5. Numbers
+<a name="numbers" >
+## 5. Numbers
+</a>
There are two basic kinds of numbers in CFML: integers (whole numbers) and real (numbers with a decimal point). For our workshop, we'll only be dealing with integers. You can use normal math operations with integers including ```+```, ```-```, ```/``` and ```*```. The ```++``` operator can be used to increment a number. It is also the only one we will use to control a loop. We will talk more about Conditional Looping in section 9. Try out this example for the ```++``` operator:
#### Tag Syntax
@@ -554,9 +559,10 @@ while (loop < 5) {
}
</cfscript>
```
-<a name="recordsets" />
-## 6. Recordsets
+<a name="recordsets" >
+## 6. Recordsets
+</a>
A query is a request to a database. It returns ```query object``` containing a **recordset** and other information. The query can ask for information from the database, write new data to the database, update existing information in the database, or delete records from the database. ```cfquery``` passes SQL statements to the ```datasource```. The ```datasource``` is set in the ColdFusion administrator.
#### Tag Syntax
@@ -596,8 +602,10 @@ There are #GetBreakfastItems.Quantity# #GetBreakfastItems.Item# in the pantry<br
```
While it's not strictly necessary to prepend the recordset name before the column name inside the ```<cfoutput>```, it's strongly recommended that you do in order to prevent referencing the wrong variable scope.
-<a name="sqlinjection" />
+
+<a name="sqlinjection" >
#### Prevent SQL Injection
+</a>
In ColdFuison it is easy to make your queries to the database dynamic by passing in variables, however a ColdFusion developer must make their sure their queries are not vulenrable to malicious code. This type of code, known as SQL Injection, allows a hacker to run queries on your database by passing code to your query through a url or form value. It is imperitive that queries are protected using a tag called cfqueryparam. **It is never a good idea to leave query variables unprotected**
For a single value the cfqueryparam tag is used like so:
@@ -784,8 +792,9 @@ There are some other ways to loop over queries, and one that comes in handy for
</cfscript>
```
-<a name="arrays" />
+<a name="arrays" >
## 7. Arrays
+</a>
Often we need to organize a group and put them into a *collection*. There are two main types of collections: **arrays** and **structures**.
@@ -960,8 +969,9 @@ Matt
</cfscript>
```
-<a name="structures" />
+<a name="structures" >
## 8. Structures
+</a>
A structure is a *collection of data* where each element of data is addressed by a name. As an analogy, think about a classroom of children. Under ideal circumstances, each student has a name and can be found by using that name. We might look in a science classroom for a child named Joey and that would result in finding an actual student. We could write this like ```science["Joey"]``` which could be read as "look in the collection named 'science' and find the thing named 'Joey'".
@@ -1042,15 +1052,64 @@ The last chunk of the example used ```StructSort``` to get the sorted array "stu
While that last part probably seemed complicated, it's just to illustrate that structures are unordered.
-<a name="conditionals" />
+<a name="structKeyExists">
+### 8.3 structKeyExists
+</a>
+When referencing a structure by key names, it is ususally good practice to make sure that the key exists in the structure first. This is done with the ``` structKeyExists(structure, "key") ``` function. Lets say we have a structure named Person and we want to check that it has a key named "yearBorn" before doing something with it. That would be done with the following code:
+
+```cfm
+<cfscript>
+if(structKeyExists(Person,"yearBorn")){
+ Person.age = year(now()) - Person.yearBorn;
+}
+</cfscript>
+```
+
+<a name="structureLooping">
+### 8.2 Looping through a structure
+</a>
+
+Sometimes it is convienent to loop through all the keys of a structure. This is done with a for-in type of loop. Lets look at an example using the following struct:
+
+```cfm
+<cfscript >
+ myStruct = {apple = "Apple", banana = "Banana", pear = "Pear"};
+</cfscript>
+```
+
+#### Tag Syntax
+
+Using tag syntax we use what is called a "collection" loop. The cfloop syntax takes two parameters, the name of the collection, and the variable name that holds the collection's key:
+
+```cfm
+<cfloop collection="#myStruct#" item="fruit">
+ #myStruct[fruit]#
+</cfloop>
+```
+
+#### Script Syntax
+
+Using script syntax we use what is called a "for-in" loop. The syntax of a for-in loop looks like ```cfm for(key in structure)```. The code to loop through the structure we created earler would look like this:
+
+```cfm
+<cfscript >
+ for(fruit in myStruct){
+ writeOutput(myStruct[fruit]);
+ }
+</cfscript>
+```
+
+<a name="conditionals" >
## 9. Conditionals
+</a>
Conditional statements evaluate to ```true``` or ```false``` only. The most common conditional operators are ```==``` (equal), ```!=``` (not equal), ```>``` (greater than), ```>=``` (greater than or equal to), ```<``` (less than), and ```<=``` (less than or equal to). You can also define the operators as abbreviations: ```EQ```, ```NEQ```, ```GT```, ```GTE```, ```LT```, and ```LTE```. You can **only** use the abbreviations when using tags.
Some instructions return a ```true``` or ```false```, so they're used in conditional statements, for example, "IsArray" which is "true" only when the variable is an "array". Structures have an instruction named ```StructKeyExists``` which returns "true" if a key is present in a structure.
-<a name="if" />
-### 9. 1. If, Else If, & Else
+<a name="if" >
+### 9. 1. If, Else If, & Else
+</a>
Why do we have conditional statements? Most often its to control conditional instructions, especially ```if``` / ```else if``` / ```else``` structures. Lets write an example by adding a method to our **PersonalChef** class:
#### Tag Syntax
@@ -1120,9 +1179,9 @@ An ```if``` block has:
Only *one* section of the ```if``` / ```else if``` / ```else``` structure can have its instructions run. If the ```if``` is ```true```, for instance, CFML will never look at the ```else if```. Once one block executes, that’s it.
-<a name="loop" />
+<a name="loop" >
### 9. 2. Looping
-
+</a>
Another time we use conditional statements is when we want to repeat a set of instructions. Try out this simple example by adding it to your _PersonalChef.cfc_:
#### Tag Syntax
@@ -1177,8 +1236,9 @@ the difference between ```=``` and ```==```.
* ```==``` is a *question*. It means "is the thing on the right equal to
the thing on the left" (or its *asking* not *telling*.)
-<a name="nothingness" />
+<a name="nothingness">
## 10. Nothingness & Null
+</a>
What is *nothingness*? Is there nothingness only in outer space? Really, when we think of *nothing* isn't it just the absence of something? Ok, that's too much philosophy
Something went wrong with that request. Please try again.