Book Image

ColdFusion 9 Developer Tutorial

By : John Farrar
Book Image

ColdFusion 9 Developer Tutorial

By: John Farrar

Overview of this book

Adobe ColdFusion is an application server, renowned for rapid development of dynamic websites, with a straightforward language (CFML), powerful methods for packaging and reusing your code, and AJAX support that will get developers deep into powerful web applications quickly. However, developing rich and robust web applications can be a real challenge as it involves multiple processes.With this practical guide, you will learn how to build professional ColdFusion applications. Packed with example code, and written in a friendly, easy-to-read style, this book is just what you need if you are serious about ColdFusion.This book will give you clear, concise, and practical guidance to take you from the basics of ColdFusion 9 to the skills that will make you a ColdFusion developer to be reckoned with. It also covers the new features of ColdFusion 9 like ORM Database Interaction and CF Builder.ColdFusion expert John Farrar will teach you the basics of ColdFusion programming, application architecture, and object reuse, before showing you a range of topics including AJAX library integration, RESTful Web Services, PDF creation and manipulation, and dynamically generated presentation files that will make you the toast of your ColdFusion developer town.This book digs deep with the basics, with real-world examples of the how and whys, to get more done faster with ColdFusion 9.
Table of Contents (22 chapters)
ColdFusion 9 Developer Tutorial
Credits
About the Author
About the Reviewers
Preface
Index

Turning HTML into a dynamic web page


Let's jump right in and take a look at the difference between common HTML pages and the power of a server-side language. We will leave out the "designer side" of web pages for now. This is again because we are going to focus on pragmatic programming concepts with ColdFusion.

Note

Visit this book's site at http://books.sosensible.com/index.cfm/books/ for help with things such as installing ColdFusion. If you go to this book's home page, there is also a link to the ZIP file for this book's code.

HTML requests

Let's work through some examples. Copy the tutorial/chapter_1 directory from the book's code bundle to the cfb/chapter_1 directory inside your server root. Enter http://localhost/cfb/complete/chapter_1/1_1.htm into the address bar of your browser. You will see a basic FAQ page as follows:

You should open the View Page Source from the browser and compare that to the code you see if you open the file in an editor. You will find that the pages are the same in every way. Here you will see how a normal server returns common HTML pages:

When we add ColdFusion to a web server, we get an extra step in how the pages are managed. This is why we are now able to create pages that are made dynamic from the server side of the equation. This is the same concept for all server-side languages.

Now let's get into a very basic introduction to ColdFusion. We will take this one step at a time. Here is the same code segment with common HTML. If you type this into an HTML page and load it into the browser, it will look the same in code as it does in the view source from the browser:

<!-- Example: 1_1.htm -->
<!-- HTML Comment -->
<div>
  <h3>Question: What is a variable?</h3>
  <p>
    <strong>Answer:</strong>
  </p>
  <p>
    Variables are named storage containers inside programming languages. Just think of variables as any type of named container holding any type of stored content. You simply name the container and store the content. Later you retrieve the content by using the same name.
  </p>
  <p>12:53 PM</p>
</div>

This is a screenshot of the previous html code running in the browser:

ColdFusion requests

Now we will look at a little more dynamic version of the page. The changes will be indicated so we can start to see what ColdFusion adds to the mix. You may be able to tell what is going on without any help depending on your programming background.

Copy the file named 1_1.htm and save the copy as 1_2.cfm in the same directory. Go back to the browser and this time look at this web page: http://localhost/cfb/chapter_1/1_2.cfm. You should note that this page looks the same and the code is basically the same if you use the browser view source.

Comments

We are going to add the following two lines above <!-- HTML Comment -->:

<!--- Processing --->
<!--- Content --->
<!-- HTML Comment -->

You will notice the two new lines have three dashes instead of two. This is because they are ColdFusion comments.

If you go back to your browser and refresh the page, you will find something interesting when you view its source. The new comment lines you added do not show on the page source since they are server-side ColdFusion comments. Remember to save the file before checking for results.

Next we need to create our first variables in ColdFusion. Add a line between the processing and content comments and create the following two variables inside <cfset> tags. These variables are just containers to store content for later use. Currently, we will be using them as text containers or what you will come to know as "string variables".

<!--- Processing --->
<cfset myQuestion = "">
<cfset myAnswer = "">

<!--- Content --->

Cut the actual question out of the bottom content section and paste it into the quotes of the string variable, myQuestion. Now, cut out the answer text in the content section and paste it inside the quotes of the string variable, myAnswer. (Don't forget, you can look at the previous code example if you have questions.)

Variable output

Now, we need to put the content, which we have moved to variables, back in the page. ColdFusion was the first to standardize server-side code tags. These look very much like HTML tags, but they add power and simplicity. What we need to do is wrap our entire content section with <cfoutput>. Add <cfoutput> open tag right after the <--- Content ---> comment tag and then put </cfoutput> close tag as the new last line of the code page.

We have to place the variables in the content section of the code with some special ColdFusion output markers. ColdFusion uses the pound symbols on both sides of the dynamic content for markers. So put #myQuestion# where you cut out the content for the myQuestion variable.

Also, place #myAnswer# where you cut out the content you pasted into the myAnswer variable. Save the file and run the page! (Check the sample code in the following section to make sure you typed things in correctly. Then refresh the page again.)

Functions

ColdFusion is very powerful and allows outputting the results of functions as well as variables. If you want you can replace the time with the function in the following code at the end of the ColdFusion code sample and you can refresh the page over and over and see that the time is being dynamically generated on the server:

<!--- Example: 1_2.cfm --->
<!--- Proccessing --->
<cfset myQuestion = "What is a variable?">
<cfset myAnswer = "Variables are named storage containers inside programming languages. Just think of variables as any type of named container holding any type of stored content. You simply name the container and store the content. Later you retrieve the content by using the same name.">

<!--- Content --->
<cfoutput>
  <div>
    <h3>Question: #myQuestion#</h3>
    <p>
      <strong>Answer:</strong>
    </p>
    <p>
      #myAnswer#
    </p>
    <p>
      #timeFormat(now())#
    </p>
  </div>
</cfoutput>

The now() function in ColdFusion returns the current date or time and the timeFormat() function converts the output to display text showing the time of its contents. Notice that when we update the screen, the time will be the current time on each refresh:

Congratulations! You have just created your first dynamic web page. If you are new to this technology, then work through the examples and complete the entire chapter. Then take a break. If you feel the need, come back after going through some chapters and repeat the exercises. You will be surprised by what you have learned.

Note

We will be removing the <html></html> standard wrappers from most examples going forward in this book. Browsers don't need those markers to present the content. In your live site code, however, those should be included.