Slide 1
An Introduction to
ColdFusion
Simon Free
@simonfree
no notes exist for this slide
Slide 2
Course Aims
Understand ColdFusion Fundamentals
Understand how tags and functions are formed
Understand some of the best practices to use when developing in CF
Experience using ColdFusion through Tags and Script
See how Awesome ColdFusion is!!!
no notes exist for this slide
Slide 3
Class Structure
Take a pre existing application and implement ColdFusion
Cover theory and implement code
CD contains completed code examples if you get stuck
People are on hand if you have any problems
Feel free to ask questions
no notes exist for this slide
Slide 4
Topics
Quick Overview
Basic Features
ColdFusion Administrator
Datasources
Simple Debugging
Includes
Looping
no notes exist for this slide
Slide 5
Topics
Interacting with Databases
Custom Functions
Forms and ColdFusion Forms
Components
Application.cfc
Data Binding
no notes exist for this slide
Slide 6
What is ColdFusion?
Application Server
Rapid development
Easy to learn
Contains all the hooks to connect to most databases
Built on industry standard java
Ships with Integrated Apache Database
Extremely Scalable
no notes exist for this slide
Slide 7
no notes exist for this slide
Slide 8
A few ColdFusion Features
no notes exist for this slide
Slide 9
Super Quick Overview
ColdFusion files usually have .cfm or .cfc file extensions
These files house the CFML (ColdFusion Markup Language)
no notes exist for this slide
Slide 10
Super Quick Overview
CFML can be Tags or Script
<cfset foo = 1 />
<cfscript>
foo=1;
</cfscript>
no notes exist for this slide
Slide 11
Super Quick Overview
# signs are used to denote variables
<cfoutput> tag is used to evaluate variables
Examples:
The value of foo is: <cfoutput>#foo#</cfoutput>
<cfoutput>
The value of foo is: #foo#
<cfoutput>
no notes exist for this slide
Slide 12
Super Quick Overview
Most tags have a cfscript representation except for a for a few, such as:
cfquery
cfftp
cfldap
cfhttp
cfcomponent
no notes exist for this slide
Slide 13
Super Quick Overview
cfscript can be inside tags but tags can not be inside cfscript
cfscript does not natively output anything (must use writeoutput() function to output)
ColdFusion Tags are formatted as below:
<cfsomething foo=âbarâ>
<cf_something> (custom tag)
no notes exist for this slide
Slide 14
Super Quick Overview
ColdFusion is considered typeless because you do not need to specify the data type on creation
This feature gives you more freedom
<cfset foo = 1/>
<cfset bar = âThe value of foo is: â & foo/>
<cfset foobar = â5â + foo />
no notes exist for this slide
Slide 15
Super Quick Overview
The data types you need to know about are:
Simple
Strings
Integers
Real Numbers
Date-time
Booleans
Complex
Arrays
Structures
Queries
Binary
Raw Data
Objects
no notes exist for this slide
Slide 16
Super Quick Overview
Main variable scopes you need to know about:
Application
Form
URL
CGI
Cookie
Session
Variables
no notes exist for this slide
Slide 17
Super Quick Overview
Tags that you will use the most:
<cfset â¦>
<cfoutput>
<cfquery â¦>
<cfabort>
<cfdump â¦>
<cfloop â¦>
<cfif â¦>
no notes exist for this slide
Slide 18
Super Quick Overview
Comments in files are:
<!- - - something - - ->
Comments in cfscript are:
//
/* */
Commenting your code is a very good practice!!!
no notes exist for this slide
Slide 19
no notes exist for this slide
Slide 20
ColdFusion Administrator
All server settings are administrated through the Administrator
Accessed through {webroot}/CFIDE/Administrator
From datasources to memory usage you can control from 1 single location
no notes exist for this slide
Slide 21
cfif
cfif tag must have an end cfif tag
cfif can have 1 cfelse tag
cfif can have multiple cfelseif tags
The condition is located in the opening cfif tag or the cfelseif tag
The conditional must evaluate to true or false
no notes exist for this slide
Slide 22
no notes exist for this slide
Slide 23
no notes exist for this slide
Slide 24
Decision Operators
CFSCRIPT only
The following expressions are only allowed in cfscript:
== (eq)
!= (neq)
> (gt)
< (lt)
>= (gte)
<= (lte)
no notes exist for this slide
Slide 25
cfloop
cfloop allows for looping over:
Lists
Arrays
Structures
Queries
Also able to loop:
Conditionally
Between a start and end point
no notes exist for this slide
Slide 26
cfloop
Example:
<cfloop from="1" to="10" step="2" index="i">
index = #i#
</cfloop>
All cfloop tags use an index, except collection loops (item), conditional loops and query loops
no notes exist for this slide
Slide 27
cfinclude
Used to include files
Included files have access to the local variable scope and can change variables for the entire page
Examples:
Good
<cfinclude template="/gui/box.cfmâ />
Bad
<cfinclude template=âhttp://simonfree.com/gui/box.cfmâ />
<cfinclude template="c:\box.cfmâ />
no notes exist for this slide
Slide 28
cfparam
Using cfparam guarantees that the variable will exist on the page
cfparam can also type check data passed into the page
Example:
<cfparam name="form.id" default="0" type="numeric" />
no notes exist for this slide
Slide 29
no notes exist for this slide
Slide 30
cfquery
cfquery sends sql commands to a database and returns the result
cfquery tag must have a datasource at all times, unless you are doing a query of queries
The datasource contains connection information
In production environments it is wise to not store the database username and password in the datasource
no notes exist for this slide
Slide 31
cfquery
<cfquery name="qEntries" datasource="introtocf">
Select
id
FROM
contacts
ORDER BY
id DESC
</cfquery>
no notes exist for this slide
Slide 32
cfqueryparam
Must be used within a cfquery tag
Checks the data type of the value
Formats the data for the specific database type
Prevents sql injection
no notes exist for this slide
Slide 33
cfqueryparam
<cfquery name="qEntries" datasource="introtocf">
Select
firstname
FROM
contacts
WHERE
id = <cfqueryparam value="#id#" cfsqltype="cf_sql_numeric" />
</cfquery>
no notes exist for this slide
Slide 34
no notes exist for this slide
Slide 35
Code Resuse
There are 4 tags that allow for code reuse:
cf_
cfimport
cfmodule
cfinvoke
cffunction (covered later on)
no notes exist for this slide
Slide 36
cf_
cf_ represents a custom tag created by you
Everything after the underscore (_) is the name of the file being requested
Example: <cf_something> is requesting the something.cfm file
Attributes can be passed through the tag
ColdFusion looks in the current folder and then the Custom Tag folder to find the file
no notes exist for this slide
Slide 37
cfimport
cfimport imports a tag library
A folder of CF files are treated like a tag library
JSP, JAR and TLD tag libraries can be used in this tag
A prefix is provided so that the files can be accessed
Example:
<cfimport taglib="/tags/gui/" prefix="gui"/>
<gui:something>
Some text
</gui:something>
no notes exist for this slide
Slide 38
cfmodule
The most versatile code reuse tag
The tag accepts either a name or a template
The name is the name of the file within the custom tag folder
Names can use dot (.) notation to represent folder structures
Example:
something.somewhere
Represents
{customtag folder}/something/somewhere.cfm
Template represents a relative or absolute path to the file
no notes exist for this slide
Slide 39
Custom Tag Files
Custom tag files have access to the attributes that are passed through the tag
They are stored in the attributes scope
The custom tag is run once for the open tag and once for the close tag
The tag has access to the thisTag scope to limit data being displayed twice
no notes exist for this slide
Slide 40
Custom Tag File
<cfoutput>
<cfif NOT thisTag.HasEndTag>
I Need an End Tag!
<cfelse>
<cfif thisTag.ExecutionMode eq "start">
I am in Start Mode. Mode = #thisTag.ExecutionMode#
<cfelse>
I am in End Mode. Mode = #thisTag.ExecutionMode#
</cfif>
</cfif>
</cfoutput>
no notes exist for this slide
Slide 41
cfinvoke
cfinvoke has the ability to access Components (cfcâs) as well as web services
To access a cfc you provide the location of the cfc and the method you wish to call
To pass information to the invoke call you use the cfinvokeargument tag
It is best to only use this method for data related code reuse, and not display code reuse
no notes exist for this slide
Slide 42
no notes exist for this slide
Slide 43
cfform
cfform has 3 available formats:
Html
Flash
XML
Html forms must use html to position the form elements
Flash forms can not have HTML in them but can be positioned and styled through the style attribute and the cfformgroup tag
XML forms use XSLT Stylesheets
no notes exist for this slide
Slide 44
cfforminput
cfforminput tags are used within cfform tags
Depending on the type of cfform used they can offer different features
cfforminput allows for validation rules to be provided
no notes exist for this slide
Slide 45
no notes exist for this slide
Slide 46
no notes exist for this slide
Slide 47
cfformgroup
Creates containers for multiple form controls
Used with Flash and XML Forms Only
no notes exist for this slide
Slide 48
cfformgroup
Available group types are:
no notes exist for this slide
Slide 49
cfformitem
Used with Flash and XML Forms only
Inserts one of the following:
Horizontal Rule
Vertical Rule
Space
HTML
Text
Script
no notes exist for this slide
Slide 50
cftextarea
cftextarea can be a simple box or a WSYWIG editor
WSYWIG editor used is fckEditor
Custom toolbars can be configured
no notes exist for this slide
Slide 51
no notes exist for this slide
Slide 52
Components
Components encapsulate functions
Components can be accessed remotely
Components must contain at least 1 function
They can be invoked in the following ways:
cfinvoke tag
Via url which passes the method
From within cfscript tags
As a web service call
From flash code
no notes exist for this slide
Slide 53
cfcomponent
cfcomponent tag wraps around all cffunction tags
The cfcomponent can be associated with an interface
cfcomponent tags can extend other components
no notes exist for this slide
Slide 54
cffunction
Business Logic is encapsulated within the cffunction tag
Functions are given access types that specify if other functions and services can access it
A return type should be set for all functions, especially when being called as web services
Functions have the ability to specify a display name and a hint, that are very useful when opening the functionality to the public
no notes exist for this slide
Slide 55
no notes exist for this slide
Slide 56
cffunction - Return Types
no notes exist for this slide
Slide 57
cfargument
Defines the parameter requirements for a function
Arguments can be required or have default values for non required attributes
Arguments should have a data type associated with them
no notes exist for this slide
Slide 58
createObject()
createObject() instantiates an object so that methods can be called
Create object can be used in Tag or Script format
createObject() can also instantiate:
Com objects
Corba Objects
Java Objects
Web Services
no notes exist for this slide
Slide 59
application.cfm / application.cfc
Application files are the first file to be accessed for all requests
Usually located in the root folder of the site
Only 1 file can be used
Application.cfc take precedence over the two
Application.cfm runs the entire file
Application.cfc only runs the necessary methods
no notes exist for this slide
Slide 60
application.cfc Methods
The methods below are called when the events occur:
no notes exist for this slide
Slide 61
Application Settings
From within the application file you can set application wide settings
All applications should be named
Data storage methods and timeout settings can be changed from the defaults
no notes exist for this slide
Slide 62
Application.cfc example
<cfcomponent displayname="Application" output="false">
<cfscript>
this.name = 'abooksimplesdfsd';
this.sessionManagement = true;
this.sessionTimeOut = createTimeSpan(0,0,20,0);
</cfscript>
<cffunction name="onApplicationStart" access="public" output="false" returntype="boolean">
</cffunction>
<cffunction name="onSessionStart" access="public" output="false" returntype="boolean">
</cffunction>
<cffunction name="onRequestStart" access="public" output="false" returntype="boolean">
</cffunction>
</cfcomponent>
no notes exist for this slide
Slide 63
no notes exist for this slide
Slide 64
Data Binding
Data binding allows for interfaces to be updated when a variable changes
UI elements, such as data grids can be bound to a variable such as a select box
Curly Braces ({ and }) are used to denote data bindings of variables
no notes exist for this slide
Slide 65
no notes exist for this slide
Slide 66
Thats all!
Questions / Comments:
simon@simonfree.com
Resources:
http://livedocs.adobe.com/coldfusion/8/
no notes exist for this slide