Difference between revisions of "Getting Started with CFML in ColdFusion and Railo"

From Hostek.com Wiki
Jump to: navigation, search
(adding basic CF tags wiki...will expand with more examples such as cfqueryparam .. ~~~~)
 
m (updated formatting..~~~~)
Line 1: Line 1:
CFML is the programming language used to create applications in ColdFusion and Railo, and it traditionally uses a tag-based syntax to perform tasks. Those who wish to avoid using tags also have the option of programming in the language's script-based alternative, CFScript. Below we'll cover some of the basics you'll need to master to build an application (Web site) using CFML.  
+
CFML is the programming language used to create applications in ColdFusion and Railo, and it traditionally uses a tag-based syntax to perform tasks. Those who wish to avoid using tags also have the option of programming in the language's script-based alternative, ''CFScript''. Below we'll cover some of the basics you'll need to master to build an application (Web site) using CFML.  
  
 
==Setting Variables==
 
==Setting Variables==
Line 5: Line 5:
 
<cfset foo = "bar">
 
<cfset foo = "bar">
  
In CFScript you just type the variable name and assign it a value:
+
In ''CFScript'' you just type the variable name and assign it a value:
 
<cfscript>
 
<cfscript>
 
foo = "bar";
 
foo = "bar";
 
</cfscript>
 
</cfscript>
 
===Displaying Contents of Variables===
 
===Displaying Contents of Variables===
If you wish to display a variable on a page (send the variable's contents to the page-output stream), you can either use the CFOutput tag:
+
If you wish to display a variable on a page (send the variable's contents to the page-output stream), you can either use the ''CFOutput'' tag:
 
<syntaxhighlight lang="cfm">
 
<syntaxhighlight lang="cfm">
 
<cfset foo = "bar">
 
<cfset foo = "bar">
Line 16: Line 16:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Or in CFScript you can make use of the WriteOutput() and Echo() functions:
+
Or in ''CFScript'' you can make use of the ''WriteOutput('') and ''Echo()'' functions:
 
<syntaxhighlight lang="cfm">
 
<syntaxhighlight lang="cfm">
 
<cfscript>
 
<cfscript>
Line 27: Line 27:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
==Conditional Statements==
 
==Conditional Statements==
The CFIf, CFElseIf, and CFElse tags allow you to create conditional statements for flow-control:
+
The ''CFIf'', ''CFElseIf'', and ''CFElse'' tags allow you to create conditional statements for flow-control:
 
<syntaxhighlight lang="cfm">
 
<syntaxhighlight lang="cfm">
 
<cfparam name = "Role" type = "string" default = "Guest">
 
<cfparam name = "Role" type = "string" default = "Guest">
Line 42: Line 42:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In CFScript you would make use of the ''if'' and ''else'' keywords. Combine them as ''else if'' to form the equivalent of CFElseIf:
+
In ''CFScript'' you would make use of the ''if'' and ''else'' keywords. Combine them as ''else if'' to form the equivalent of ''CFElseIf'':
 
<syntaxhighlight lang="cfm">
 
<syntaxhighlight lang="cfm">
 
<cfscript>
 
<cfscript>
Line 64: Line 64:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
''Note about CFParam:'' The CFParam tag checks that a variable is defined, validates the data if it is defined, and allows you to specify a default value if the variable is not defined. More info on the tag is available here:  
+
''Note about CFParam:'' The ''CFParam'' tag checks that a variable is defined, validates the data if it is defined, and allows you to specify a default value if the variable is not defined. More info on the tag is available here:  
 
*[https://wikidocs.adobe.com/wiki/display/coldfusionen/cfparam ColdFusion Documentation - CFParam]
 
*[https://wikidocs.adobe.com/wiki/display/coldfusionen/cfparam ColdFusion Documentation - CFParam]
 
*[http://railodocs.org/index.cfm/tag/cfparam Railo Documentation - CFParam]
 
*[http://railodocs.org/index.cfm/tag/cfparam Railo Documentation - CFParam]
 
==Loops==
 
==Loops==
With the CFLoop tag, you can create an index-based loop, a conditional loop, or a loop to itterate over queries, lists, and arrays. For example, this will create a basic loop that prints the numbers 1-10 on new lines:
+
With the ''CFLoop'' tag, you can create an index-based loop, a conditional loop, or a loop to iterate over queries, lists, and arrays. For example, this will create a basic loop that prints the numbers 1-10 on new lines:
 
<syntaxhighlight lang="cfm">
 
<syntaxhighlight lang="cfm">
 
<cfloop index = "i" from = "1" to = "10" step = "1">
 
<cfloop index = "i" from = "1" to = "10" step = "1">
Line 75: Line 75:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In CFScript, just use the ''loop'' keyword, followed by the attributes, and contain the statement(s) to be executed within braces:
+
In ''CFScript'', just use the ''loop'' keyword, followed by the attributes, and contain the statement(s) to be executed within braces:
 
<syntaxhighlight lang="cfm">
 
<syntaxhighlight lang="cfm">
 
<cfscript>
 
<cfscript>
Line 88: Line 88:
 
*[https://wikidocs.adobe.com/wiki/display/coldfusionen/cfloop ColdFusion Documenation - CFLoop]
 
*[https://wikidocs.adobe.com/wiki/display/coldfusionen/cfloop ColdFusion Documenation - CFLoop]
 
*[http://railodocs.org/index.cfm/tag/cfloop Railo Documentation - CFLoop]
 
*[http://railodocs.org/index.cfm/tag/cfloop Railo Documentation - CFLoop]
==Accessing Databases==
+
==Accessing Databases with CFQuery==
To interact with databases, first define a ColdFusion datasource (DSN) in your control panel or [[Application-Specific_Datasources|in your Application.cfc]] ''(Railo 4.1+ and CF 11+)''. Once you have a working datasource, you can use the CFQuery tag to run SQL queries against your database server:
+
To interact with databases, first define a ColdFusion datasource ''(DSN)'' in your control panel or [[Application-Specific_Datasources|in your Application.cfc]] ''(Railo 4.1+ and CF 11+)''. Once you have a working datasource, you can use the ''CFQuery'' tag to run SQL queries against your database server:
 
<syntaxhighlight lang="cfm">
 
<syntaxhighlight lang="cfm">
 
<cfquery name = "customers" datasource = "MyDSN">
 
<cfquery name = "customers" datasource = "MyDSN">
Line 98: Line 98:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In CFScript:
+
In ''CFScript'':
 
<syntaxhighlight lang="cfm">
 
<syntaxhighlight lang="cfm">
 
customers = new Query(datasource = "MyDSN", sql = "SELECT firstname, lastname, phone FROM contacts WHERE customer IS TRUE;").execute().getResult();
 
customers = new Query(datasource = "MyDSN", sql = "SELECT firstname, lastname, phone FROM contacts WHERE customer IS TRUE;").execute().getResult();

Revision as of 23:11, 29 August 2014

CFML is the programming language used to create applications in ColdFusion and Railo, and it traditionally uses a tag-based syntax to perform tasks. Those who wish to avoid using tags also have the option of programming in the language's script-based alternative, CFScript. Below we'll cover some of the basics you'll need to master to build an application (Web site) using CFML.

Setting Variables

You can create/update variables using the cfset tag: <cfset foo = "bar">

In CFScript you just type the variable name and assign it a value: <cfscript> foo = "bar"; </cfscript>

Displaying Contents of Variables

If you wish to display a variable on a page (send the variable's contents to the page-output stream), you can either use the CFOutput tag:

	<cfset foo = "bar">
	<cfoutput>#foo#</cfoutput>

Or in CFScript you can make use of the WriteOutput() and Echo() functions:

	<cfscript>
		set foo = "bar";
		set thud = "plug"
 
		WriteOutput("The value of foo is " & #foo#);
		Echo("The value of thud is " & #thud#);
	</cfscript>

Conditional Statements

The CFIf, CFElseIf, and CFElse tags allow you to create conditional statements for flow-control:

	<cfparam name = "Role" type = "string" default = "Guest">
 
	<cfif Role EQ "Author">
		<cfset AllowedToPost = true >
		<cfoutput>Welcome #UserName#! Feel free to add new content.</cfoutput>
	<cfelseif Role EQ "Reader">
		<cfset AllowedToPost = false>
		<cfoutput>Hello #UserName#, glad you're back.
	<cfelse>
		<cfoutput>Please log in to read articles or author new posts.</cfoutput>
	</cfif>

In CFScript you would make use of the if and else keywords. Combine them as else if to form the equivalent of CFElseIf:

	<cfscript>
		param name = "Role" type = "string" default = "Guest";
 
		if (Role EQ "Author")
		{
			AllowedToPost = true;
			echo("Welcome " & #UserName# & "! Feel free to add new content.");
		}
		else if (Role EQ "Reader")
		{
			AllowedToPost = false;
			echo("Hello " & #UserName# & "! Enjoy your reading.");
		}
		else
		{
			echo("Please log in to read articles or author new posts.);
		}
	</cfscript>

Note about CFParam: The CFParam tag checks that a variable is defined, validates the data if it is defined, and allows you to specify a default value if the variable is not defined. More info on the tag is available here:

Loops

With the CFLoop tag, you can create an index-based loop, a conditional loop, or a loop to iterate over queries, lists, and arrays. For example, this will create a basic loop that prints the numbers 1-10 on new lines:

	<cfloop index = "i" from = "1" to = "10" step = "1">
		<cfoutput>#i#<br></cfoutput>
	</cfloop>

In CFScript, just use the loop keyword, followed by the attributes, and contain the statement(s) to be executed within braces:

	<cfscript>
		loop index = "i" from = "1" to = "10" step = "1"
		{
			echo(#i# & "<br>");
		}
	</cfscript>

More CFLoop Examples:

Accessing Databases with CFQuery

To interact with databases, first define a ColdFusion datasource (DSN) in your control panel or in your Application.cfc (Railo 4.1+ and CF 11+). Once you have a working datasource, you can use the CFQuery tag to run SQL queries against your database server:

	<cfquery name = "customers" datasource = "MyDSN">
		SELECT firstname, lastname, phone
		FROM contacts
		WHERE customer IS TRUE;
	</cfquery>

In CFScript:

	customers = new Query(datasource = "MyDSN", sql = "SELECT firstname, lastname, phone FROM contacts WHERE customer IS TRUE;").execute().getResult();

CFQuery References:


Further Reading