Categories

Partners
  • Website Templates

  • Design Feeds

  • Adobe Photoshop Tutorials

  • Website Templates, WordPress Themes

  • Website Templates

  • Flash Web Sites

  • Photoshop Templates

  • Photo Contests

  • Photo Tips
  • Search


    Advanced Search


    Partner Links
  • Adobe Photoshop Tutorials

  • Free Stock Photos and Images



  • Website Templates

    ColdFusion Tutorial - Template Basics
    Published  05/19/2006 | Macromedia ColdFusion MX
       




    Page 2 — Template Basics

    Let's look at a basic template file. We'll assume we have a data source set up as contents_of_my_pocket and that the data source is a Microsoft Access database called contents_of_my_pocket.mdb.

    Within that database is a single table called Contents, and you have fields defined as Item, Acquired, and Value. If you wanted a Web page to list all of the items in the database, the date you acquired them, and their value, the top of your template file would look something like this:

    <CFQUERY DATASOURCE="contents_of_my_pocket" NAME="pocket">
    SELECT * FROM contents
    </CFQUERY>
    This code tells ColdFusion to query your data source:
    • NAME can be anything you like, as long as it matches the name in the CFOUTPUT, which I describe below. This allows you to set up more than one query on a page and name each one, so your output could include information from more than one database.
    • DATASOURCE is the name of your DSN, exactly as you defined it in ColdFusion Administrator.
    • The SELECT statement is a standard SQL statement (we'll cover these in the next section) that tells ColdFusion which records you want to select from what table. In this case, we're using the wildcard symbol "*" to indicate that we want to grab the information from all fields in the table called Contents.
    Now, below our query, we begin the standard HTML part of the template:


    Contents of My Pocket

    <CFOUTPUT QUERY="pocket">
    #Item#

    #Acquired#

    #value#


    </CFOUTPUT>

    The variables in our template are the items enclosed in hash marks (#). Note that they're keyed exactly to the field names in our database contents_of_my_pocket.mdb. We can use these variables anywhere in our HTML document, as long as we enclose them in <CFOUTPUT>tags. CFOUTPUT tells ColdFusion that we're about to refer to some variables from a specific query named at the top of the document.

    Note the bold tags around the #item# variable. That's one of the great things about this whole deal: You can format text from your variables just like any static HTML text.

    If a user accessed this page from his or her browser, the user would see something like this:

    Contents of My Pocket

    One Bouncy Ball with Psychedelic Markings
    12 December 1998
    25 cents

    Half of a Cheese Sandwich
    14 December 1998
    25 cents

    A Bus Transfer
    14 December 1998
    75 cents

    One Plastic Baggie, Empty
    12 December 1998
    $50 (when full)

    A Ticket for Having an Unleashed Dog in the Park
    12 December 1998
    -$75

    A Picture of Cyndi Lauper Torn from The Star
    15 December 1998
    $0

    ColdFusion runs through all of the records you chose in your SELECT statement (in this case, all of them) and returns formatted HTML code until you run out of records. To add items to our page, we only have to add items to the database. ColdFusion handles the rest.

    There are a lot of fancy tricks you can do with your variables, but we won't cover them until Day Three. Now on to some basic SQL.

    Article Series
    This article is part 3 of a 7 part series. Other articles in this series are shown below:
    1. ColdFusion Tutorial - Lesson 2
    2. ColdFusion Tutorial - ColdFusion Templates
    3. ColdFusion Tutorial - Template Basics
    4. ColdFusion Tutorial - SQL Statements
    5. ColdFusion Tutorial - More CFOUTPUT
    6. ColdFusion Tutorial - CFIF and CFELSE
    7. ColdFusion Tutorial - Review