Home > Notes Last Updated: 11/17/06

Cold Fusion Markup Language (CFML) -- Code Basics
 

~~COMMENTS for CFML

<!--- this is how you write comments in CFML --->  //notice that there's 3 dashes for CFML
 

~~COMMENTS for HTML

<!-- this is how you write comments in HTML -->  //notice that there's 2 dashes for HTML
 

~~VARIABLE Basics

Declaring Variables: a variable is most frequently defined by the <CFSET> tag

<CFSET variable_name="xxx">
   //stores the value xxx to variable_name
 

Retrieving defined variables: by placing # and # between a variable, its value can be retrieved

<CFSET new_variable=#variable_name#>   //returns value of variable_name and stores it to new_variable
<CFSET new_variable=#variable_name# + 1>
 //adds 1 to variable_name and store it in new_variable
 

Using Expressions on variables:

<CFSET evaluation_variable=(#new_variable# + 2) / 5>   //does calculation to new_variable and stores it to evaluation_variable
 

Boolean Variables: variables that stores 0 if false, or 1 if true; comparison operators are as follows: EQ, NEQ, GT, GTE, LT, LTE

<CFSET x = #new_variable# EQ 3>  //return and store 0 in #x# if comparison is false, or 1 in #x# if comparison is true
<CFSET x = #new_variable# LT 3>  //*same as above*
 

String Variables: have special functions for concatenation (&) and for comparison between two strings: CONTAIN, DOES NOT CONTAIN

<CFSET string_variable = #string_1# & #string_2#>  //concatenates variables string_1 and string_2 and store it in #string_variable
<CFSET y = #string_variable# CONTAIN #string_1#>  //compares both strings and return 0 to #y# if false, and 1 to #y# if true
 

~~Flow Control Statements

if/else Statements:

<CFIF "logical_expression EQ True">  //eg. of logical expression: #y# GT #x# (if variable y is greater than variable x: y > x )
do if stuff
<CFELSE>
do else stuff
</CFIF>
 

Switch Statement:

<CFSWITCH EXPRESSION="expression">  //eg. of expression: ??
<CFCASE VALUE="value1">
do first case stuff
</CFCASE>
<CFCASE VALUE="value2">
do second case stuff
</CFCASE>
<CFCASE VALUE="value3">
do third case stuff
</CFCASE>
...
</CFSWITCH>
 

for Loops:

// #LoopCount# will be incremented by 1 for each loop started from value assigned to start_no to end_no
<CFLOOP INDEX="LoopCount" FROM="start_no" TO="end_no"> 
execute stuff
</CFLOOP>

<CFLOOP INDEX="LoopCount" FROM="0" TO="20">do stuff</CFLOOP>  //loops from 0 to 20 (??? 20 times or 21 times?)
 

~~Other CF FUNCTIONS and Tags

Output Tag: permits display of the results from CFMX operations

<CFOUTPUT> text </CFOUTPUT>
 

Include Tag: permits reference to another CF template

<CFINCLUDE TEMPLATE="template_name.cmf">
 

Randomize(<number>) and RandRange(no1, no2)

<CFSET temp=Randomize( second( Now() ) )>   //get a random seed for next line
<CFSET target_variable=#RandRange( 50, 100 )#>  //assigns a random integer from range 50 to 100 to variable target.variable

*note that Now() retrieves the computer's clock time in year, date, hour, minute, second and second() extracts the seconds from that
 

session. Tag: declares a static variable ( variable that retains its data throughout multiple use in different templates )

<CFSET session.target_variable=#RandRange( 50, 100 )#>  //target_variable variable will be one variable throughout all templates as used by same user

 

Home | Profile | Notes | Space | Links