Getting Started With ColdFusion Templates and Parameters - Getting Started With ColdFusion Templates
(Page 2 of 5 )
Create a new folder called learncf under your server's root folder. This root folder differs depending on the web server you are using:
- IIS - c:\inetpub\wwwroot
- PWS - c:\inetpub\wwwroot
- Apache - c:\program files\apache group\apache\htdocs
After everything is setup, open your favorite text editor such as Notepad or Ultraedit. Enter the following code listing and save the file as hello.cfm under your newly created folder:
<html>
<head>
<title>Hello World</title>
<head>
<body bgcolor=ffffff>
<center>
<br><br><h1>Hello World!!</h1><hr>
<b>Today's date is:</b><br>
<cfoutput>
<cfset today = now()>
<i>#today#</i>
</cfoutput>
</center>
</body>
</html> View this file in your browser at http://localhost/learncf/home.cfm or http://127.0.0.1/learncf/home.cfm. Your page should look similar to this:
ColdFusion tags and HTML tags are very similar. They both have beginning and end tags, and the end tag is preceded by a forward-slash (/). In the above example we used <cfoutput>. The <cfoutput> tag is used if you want to output ColdFusion data. Its syntax is as follows:
<cfoutput>ColdFusion, HTML expressions</cfoutput> Another very useful tag that we used is <cfset>. The <cfset> tag is used to create and modify variables in ColdFusion. Its syntax is as follows:
<cfset variable_name = variable_value> We created a variable called today using <cfset>, and assigned it the value of now(). Now() is actually a ColdFusion function that returns the current server's date and time. You display the value of a ColdFusion variable by enclosing the variable name in pound signs (#), just like we did with #today# in our example above.
Let's go a bit further by formatting the date and time value to make it more user-friendly. Simply change the existing <cfset> statement to something like this:
<html>
<head>
<title>Hello World</title>
<head>
<body bgcolor=ffffff>
<center>
<br><br><h1>Hello World!!</h1><hr>
<b>Today's date is:</b><br>
<cfoutput>
<cfset today =
DateFormat(now(), "dddd, mmmm d, yyyy")>
<i>#today#</i>
</cfoutput>
</center>
</body>
</html> Save your file and reload your browser. We now have a human readable date and time:
You might have already guessed that DateFormat is a ColdFusion function that formats any date you pass to it. Its syntax is as follows:
DateFormat(date [,format mask]) The format mask sets how ColdFusion should display the date, according to the following:
- d: Day of the month as digits with no leading zero for single-digit days.
- dd: Day of the month as digits with a leading zero for single-digit days.
- ddd: Day of the week as a three-letter abbreviation.
- dddd: Day of the week as its full name.
- m: Month as digits with no leading zero for single-digit months.
- mm: Month as digits with a leading zero for single-digit months.
- mmm: Month as a three-letter abbreviation.
- mmmm: Month as its full name.
- y: Year as last two digits with no leading zero for years less than 10.
- yy: Year as last two digits with a leading zero for years less than 10.
- yyyy: Year represented by four digits.
- gg: Period/era string. Currently ignored, but reserved for future use.
Last but not least, comments can be added in ColdFusion by using <!--- ---> tags. These tags are very similar to those of HTML's <!-- and --> tags. The following code illustrates how to use a ColdFusion comment:
<!---
Author: Kongtechnology.com
Purpose: My first ColdFusion template
Date: 03/January/2003
---> You can put the above comment into hello.cfm too:
<html>
<head>
<title>Hello World</title>
<head>
<body bgcolor=ffffff>
<center>
<br><br><h1>Hello World!!</h1><hr>
<b>Today's date is:</b><br>
<cfoutput>
<!---
Author: Kongtechnology.com
Purpose: My first ColdFusion template
Date: 03/January/2003
--->
<cfset today =
DateFormat(now(), "dddd, mmmm d, yyyy")>
<i>#today#</i>
</cfoutput>
</center>
</body>
</html> Now that we know the basics of how a ColdFusion template works, let's see how we can pass parameters to our ColdFusion templates.
Next: Passing Parameters Through the URL >>
More ColdFusion Articles
More By Jackie Kong