Flylib.com

Books Software

 
 
 

Example 1.1: My First Template

Team-Fly    

ColdFusion MX: From Static to Dynamic in 10 Steps
By Barry Moore
Table of Contents
Step 1.  The Basics


Example 1.1: My First Template

Let's put together everything we've learned and create our first ColdFusion template.

We want to accomplish the following:

  • Use <CFSET> to set the value of a variable

  • Use the Now() function to grab the current date/time

  • Use the DateFormat() function to format the date to our liking

  • Use <CFOUTPUT> to output the values of our variables and functions

  • Use <CFINCLUDE> to reuse some common code.

Let's get cracking. Try to create your own template or follow these steps:

  1. Open Homesite or your text editor of choice.

  2. Enter the following code or open the completed file from your CFMX10Steps folder. Completed example files can be found in the CompletedFiles\Examples\Step01 directory. For more information on downloading and installing the book's supporting files please see the Appendix.

    <!---
    File: 
    Description 
    Author: 
    Created: 
    ---> 
    <HTML> 
    <HEAD> 
          <TITLE>My First Template</TITLE> 
    </HEAD> 
    <!--- set the value of the FirstName variable to your name ---> 
    <CFSET FirstName= "Barry"> 
    
    <BODY> 
          <CFOUTPUT> 
                <H1>Welcome #FirstName#</H1> 
                Today's date is: #DateFormat(Now(), "DD MMMM, YYYY")# 
          </CFOUTPUT> 
          <HR> 
          <!--- include copyright information ---> 
          <CFINCLUDE TEMPLATE="Copyright.htm"> 
    </BODY> 
    </HTML>
    
  3. Enter your own information in the opening comments and save the file to your CFMX10Steps\Examples\Step01 directory as MyFirstTemplate.cfm. The CFMX10Steps\Examples directory is your working directory, as we create examples throughout the book save them here in the appropriate subdirectory.

  4. Make sure your web server and ColdFusion services are running. For information on creating a virtual mapping to the Examples directory please see the Appendix.

  5. Open your browser and enter the URL for the template. For example, it might be something like http://localhost/Examples/Step01/MyFirstTemplate. cfm. You must use the URL; you cannot simply open the file in the browser.

  6. You should see something similar to the page shown in Figure 1.13.

    Figure 1.13. MyFirstTemplate. cfm browser output.

    graphics/01fig13.gif

  7. If you do not see a similar page or receive some sort of error, double-check that your code matches the preceding example. Check carefully for quotation marks and pound signs. If you are still having trouble, jump ahead to the next section, "Troubleshooting."

NOTE

<CFSET> tags can be placed anywhere in your template. They do not have to go inside the <BODY> or <HTML> tags. However, any <CFOUTPUT> blocks usually follow the same rules that apply to normal HTML tag use.



Team-Fly    
Top
 

 
Team-Fly    

ColdFusion MX: From Static to Dynamic in 10 Steps
By Barry Moore
Table of Contents
Step 1.  The Basics


Troubleshooting

While you are learning to code in ColdFusion, you are bound to make some errors along the way. Unlike the forgiving environment of HTML, ColdFusion is very strict when it comes to syntax. Fortunately, ColdFusion Server provides very useful feedback when it comes across an error.

Some errors are relatively benign . For example, if you try to use a ColdFusion variable outside of a <CFOUTPUT> block, the page will continue to process. However, the user will see the literal name of the variable rather than its value, as illustrated in Figure 1.14.

Figure 1.14. Incorrect use of pound signs.

graphics/01fig14.gif

Most other errors will cause the page to stop processing. ColdFusion will return diagnostic information to the client browser to aid in solving the problem. In Figure 1.15, you can see that a typo in the Error2.cfm template causes ColdFusion to return some troubleshooting information.

Figure 1.15. Error2.cfm error output.

graphics/01fig15.gif

The diagnostic information returned from the ColdFusion Server indicates that it ran into problems trying to close the <CFOUTPUT> block on line 20 of the template. It goes on to say that the probable cause of the error is a misspelling of expression text. In addition, the error indicates that the last successful text that the server was able to parse was between line 17 and line 19. A closer examination of line 19 reveals the culprit, a missing quotation mark in the DateFormat() mask.

Figure 1.16 illustrates the error information you would receive if you neglected to use a closing </CFOUTPUT> tag.

Figure 1.16. Error3.cfm error output.

graphics/01fig16.gif


Team-Fly    
Top