Reusing Code - macromedia COLDFUSION MX 7 Web Application Construction Kit [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

macromedia COLDFUSION MX 7 Web Application Construction Kit [Electronic resources] - نسخه متنی

Ben Forta, Raymond Camden, Leon Chalnick, Angela Buraglia

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید





  • Reusing Code


    All developers writeor should writecode with reuse in mind. There are many reasons why this is a good idea:

    • Saving time.
      If it's written once, don't write it again.

    • Easier maintenance.
      Make a change in one place and any code that uses it gets that change automatically.

    • Easier debugging.
      Fewer copies exist out there that will need to be fixed.

    • Group development.
      Developers can share code more easily.

    Most of the code reuse in this book involves ColdFusion code, but to demonstrate basic reuse, let's look at a simple example.

    Orange Whip Studios is building a Web site, slowly. Figure 9.11 shows a Home page (still being worked on), and Figure 9.12 shows a Contact page (also being worked on).

    Figure 9.11. The Home page contains basic logos and branding.

    Figure 9.12. The Contact page contains the same elements as the Home page.

    But you're using ColdFusion, and ColdFusion makes code reuse incredibly simple. The CFML <cfinclude> tag is used to include one page in another. <cfinclude> specifies the name of a file to include. At runtime, when ColdFusion encounters a <cfinclude> tag, it reads the contents of the specified file and processes it as if it were part of the same file.

    To demonstrate this, look at Listings 9.14 and 9.15. The former is ows_header.cfm, and the latter is ows_footer.cfm. Between the two files, all the formatting for the Orange Whip Studios pages is present.

    Listing 9.14. ows_header.cfm


    <!---
    Name: ows_header.cfm
    Author: Ben Forta (ben@forta.com)
    Description: <cfinclude> header
    Created: 12/1/2004
    --->
    &l275>
    <head>
    <title>Orange Whip Studios</title>
    </head>
    <body>
    <!--- header --->
    <table width="100%">
    <tr>
    <td>
    <img src="../images/logo_c.gif"
    width="101"
    height="101"
    alt="
    border="0">
    </td>
    <td>
    <font face="Comic Sans MS" size="7" color="#ff8000">
    Orange Whip Studios</font>
    </td>
    </tr>
    </table>
    <p>

    Listing 9.15. ows_footer.cfm


    <!---
    Name: ows_footer.cfm
    Author: Ben Forta (ben@forta.com)
    Description: <cfinclude> footer
    Created: 12/1/2004
    --->
    <p>
    <hr>
    <p align="right">
    <i>&copy;Orange Whip Studios</i>
    </p>
    </body>
    </html>

    Now that the page header and footer have been created, <cfinclude> can be used to include them in the pages. Listing 9.16 is home.cfm, and Listing 9.17 is contact.cfm.

    Listing 9.16. home.cfm


    <!---
    Name: home.cfm
    Author: Ben Forta (ben@forta.com)
    Description: Demonstrate use of <cfinclude>
    Created: 12/1/2004
    --->
    <!--- Include page header --->
    <cfinclude template="ows_header.cfm">
    <h2>Home page goes here</h2>
    <!--- Include page footer --->
    <cfinclude template="ows_footer.cfm">

    Listing 9.17. contact.cfm


    <!---
    Name: contact.cfm
    Author: Ben Forta (ben@forta.com)
    Description: Demonstrate use of <cfinclude>
    Created: 12/1/2004
    --->
    <!--- Include page header --->
    <cfinclude template="ows_header.cfm">
    <h2>Contact page goes here</h2>
    <!--- Include page footer --->
    <cfinclude template="ows_footer.cfm">

    As you can see, very little code exists in Chapter 11, "The Basics of Structured Development".


  • / 281