Adobe
产品
Acrobat
Creative Cloud
创意套装
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
更多产品
解决方案
数字营销
数字媒体
教育
金融服务业
政府部门
网页体验管理
更多解决方案
学习帮助下载公司
商店
在线商店
批量许可
查找经销商
搜索
 
信息 登录
欢迎,我的支持
我的帐户
注销
为何登录?登录后可以管理您的帐户,访问试用版下载、产品扩展和社区区域等。
Adobe
产品 分类 购买   搜索  
解決方案 公司
学习
登录 注销 我的货物 我的支持
Date Date
Qty:
Subtotal
Checkout
Adobe 开发者中心 / Dreamweaver 开发人员中心 /

Creating user-defined functions for ColdFusion 8 in Dreamweaver CS4

by 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
  • Performing full-featured form validations
  • Building your first dynamic website – Part 2: Developing the back end
  • Building your first dynamic website – Part 3: Displaying content from a database
  • Creating an event registration application in PHP – Part 1: Building the front end
  • Setting up a ColdFusion development environment for Dreamweaver
  • Working with Drupal in Dreamweaver CS5
  • Beginner's guide to databases
  • Creating master and detail ColdFusion pages
  • Creating custom server behaviors and Dreamweaver extensions

产品

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • 移动应用程序
  • Photoshop
  • Touch Apps

解决方案

  • 数字营销
  • 数字媒体
  • 网页体验管理

行业

  • 教育
  • 金融服务业
  • 政府部门

帮助

  • 产品帮助中心
  • 订货和退货
  • 下载和安装
  • 我的 Adobe

学习

  • Adobe 开发人员连接
  • 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 (更新)

京 ICP 备 10217899 号 京公网安备 110105010404