Adobe
產品
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
更多產品
解決方案
數位行銷
數位媒體
教育機構
金融服務業
政府機關
網頁體驗管理
更多解決方案
教學 說明 下載 公司
購買
家庭使用 適用於個人和家庭辦公室
授權方案 適用於企業、學校和政府機關
其他購買方式
搜尋
 
資訊 登入
歡迎, 我的購物車 我的貨品 我的客戶支援
我的帳戶
登出
為何要登入?登入即可管理您的帳戶並取得試用下載、產品擴充功能、社群討論區等。
Adobe
產品 產業 購買   搜尋  
解決方案 公司
說明 學習資源
登入 登出 我的貨品 我的客戶支援
Date Date
Qty:
Subtotal
Checkout
開發人員中心 / Dreamweaver 開發人員中心 /

Creating user-defined functions for ColdFusion 8 in Dreamweaver CS4

作者 Raymond Camden

Raymond Camden
  • coldfusionjedi.com

Created

22 December 2008

頁面工具

在 Facebook 上共用
在 Twitter 上共用
在 LinkedIn 上共用
書籤
列印

Tags

系統需求

必須具備的知識

Familiarity with Dreamweaver and ColdFusion.

使用者等級

中級

要求的產品

  • Dreamweaver (Download trial)
  • ColdFusion (Download trial)

範例檔案

  • creating_udfs.zip (ZIP, 4K) (1 KB)

In this tutorial, you'll learn how to create a simple user-defined function (UDF) for ColdFusion 8 in Dreamweaver CS4 that will provide an easy way to format date and time data using one simple function. ColdFusion provides nice time and date formatting, but no way to format both a date and time together. The UDF will solve that problem.

You may wonder why building UDFs is useful at all. UDFs let you take a process, any process really, and collapse it into one simple call. A real-world comparison would be running to the store to get coffee. This process involves multiple steps: getting the car keys, driving to Starbucks, buying the coffee, and driving back home. If you needed to do this multiple times (and who doesn't drink more than one cup of coffee), you would have to repeat the steps multiple times as well. If you could gather these steps into one process, though, called "getCoffee," then you can simplify the process. If this were code, instead of having 5 to 6 lines of code every time you needed to get coffee, you would define the process once (in the UDF) and then simply run getCoffee() every time you needed it.

Setting up Dreamweaver CS4

In this section, you'll set up Dreamweaver CS4 and ColdFusion Server and the tags you'll need to create the UDF.

  1. Define a ColdFusion site in Dreamweaver CS4.

    Note: For more details on how to set up a site in Dreamweaver, see Setting up a Dreamweaver site in Dreamweaver Help or view the video Defining a site.

  2. Create a new CFML page and save it as test.cfm within /cf_webroot/dw_udfs/ (create the folder if it does not yet exist).
  3. In Dreamweaver, select the Insert > CFML > Advanced tab, and then the More Tags (the last option in the CFML Advanced tab) option.
  4. Expand the CFML Tags folder and select Extensibility (see Figure 1).
Selecting the cffunction
Figure 1. Selecting the cffunction
  1. Select cffunction from the list and click Insert. The Tag Editor dialogue box appears. Name the function formatDateTime (see Figure 2). Click OK and then Close.
Naming your cffunction tag.
Figure 2. Naming your cffunction tag.
  1. Change to Code View in Dreamweaver CS4 and locate the cffunction tag that Dreamweaver inserted into test.cfm.
  2. Place your cursor between the beginning and end tags for cffunction and press Enter twice to insert an extra space. Your code should look like the following:
<cffunction name="formatDateTime" output="no" verifyclient="no" securejson="false"> </cffunction>
  1. Put your cursor between the cffunction tag and press Enter so that you are on a new line. You will now repeat steps 3 through 6 for the cfargument tag. Complete the dialog box as shown in Figure 3 (name the cfargument tag datetime, selecting Date for Type, and select the Required check box). Be sure that you press Enter after the cfargument tag to begin a new line.
Naming the cfargument tag.
Figure 3. Naming the cfargument tag.

The argument will pass the datetime field (a date and time value) into the UDF from your page, and requires a value.

Next, you will create a variable. You will use the var keyword to tell ColdFusion that this variable is for this function only. It will be defined as a local variable and will be discarded when you execute the cffunction tag. You can leave your variable empty as it will be populated from the rest of the code in the UDF.

  1. Select cfsetfrom the Insert/CFML tag list. This inserts an empty cfset tag for you. Name the variable formattedString. Add var before formattedString and after cfset. Then add = "" to keep its value empty, as shown below:
<cfset var formattedString = "">
  1. As I mentioned at the beginning of this article, ColdFusion has functions to format dates and times. You will use both these functions together to create the result of the UDF. Press the Enter key to add a blank line after the previous cfset and paste in the following code:
<cfset formattedString = dateFormat(arguments.datetime) & " " & timeFormat(arguments.datetime)>

This sets the value of formattedString to the combination of two ColdFusion functions: dateFormat and timeFormat. Note that you also add an empty space between these two functions. Both dateFormat and timeFormat allow you to specify format options, but for now you are simply going to use the defaults.

  1. Now you need to return this value out of the UDF. Once again return to the CFML Advanced, Extensibility tag list, and this time select CFRETURN. This inserts a blank cfreturn tag. Inside the tag you need to specify the value to return. Enter formattedString. The following snippet shows the complete UDF:
<cffunction name="formatDateTime" output="no" verifyclient="no" securejson="false"> <cfargument name="datetime" type="date" required="yes"> <cfset var formattedString = ""> <cfset formattedString = dateFormat(arguments.datetime) & " " & timeFormat(arguments.datetime)> <cfreturn formattedString> </cffunction>

Testing the UDF

Now that you've written the UDF, it's time to begin playing with it. Place your cursor after the end of the function and press the Enter key a few times. To call the UDF you just created, you use the same syntax you would for any regular built-in user-defined function. For example:

<cfoutput> #formatDateTime(now())# </cfoutput>

This displays your current date and time (for example, 30-Oct-08 08:45 PM). You can also specify a date and time using the createDateTime function:

<cfoutput> Santa will pass #formatDateTime(createDateTime(2008, 12, 24, 23, 59, 59))# </cfoutput>

This displays: Santa will pass 24-Dec-08 11:59 PM.

Where to Go Next

This article only scratches the surface of what you can do with UDFs. For more information be sure to read the documentation included with ColdFusion 8. For a huge selection of UDFs to use with your own projects, visit the Common Function Library Project, which contains hundreds of ColdFusion UDFs—all free and ready for immediate download!

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

More Like This

  • Creating an event registration application in PHP – Part 2: Building the back end
  • Setting up a ColdFusion development environment for Dreamweaver
  • Setting up a PHP development environment for Dreamweaver
  • Setting up an ASP development environment for Dreamweaver
  • Which server-side technology should I choose?
  • Creating your first website – Part 6: Publishing your website
  • Building your first dynamic website – Part 1: Setting up the site and database
  • Building your first dynamic website – Part 2: Developing the back end
  • XML 概觀
  • XSL 概觀

產品

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • 行動應用程式
  • Photoshop
  • Touch Apps

解決方案

  • 數位行銷
  • 數位媒體
  • 網頁體驗管理

產業

  • 教育機構
  • 金融服務業
  • 政府機關

說明

  • 產品說明中心
  • 訂購與退貨
  • 下載與安裝
  • 我的 Adobe

學習資源

  • Adobe Developer Connection
  • Adobe TV
  • 培訓與認證
  • 論壇
  • 設計中心

購買方式

  • 適用於個人和家庭辦公室
  • 授權方案
  • 其他購買方式

下載

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

公司

  • 新聞室
  • 合作夥伴方案
  • 企業社會責任
  • 工作機會
  • 投資者關係
  • 事件
  • 法律聲明
  • 安全性
  • 聯絡 Adobe
選擇您所在的國家/地區 台灣 (變更)
選擇您所在的國家/地區 關閉

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

使用條款 | 隱私權政策和 Cookies (更新)