Chapter 9 Modular Code

cortegesmashInternet και Εφαρμογές Web

10 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

118 εμφανίσεις

Chapter 9

Modular Code


Overview


Creating modular and efficient code is the focal point of Chapter 9. Using Include files to embed
other ColdFusion templates facilitates reusing a standard section of code throughout the site.
Incorporating this idea ens
ures consistency from page to page such as using a standard header
and footer. User
-
defined functions and CFCs encapsulate business logic and data access logic
for reuse in multiple web applications and different locations. This chapter first discusses
c
reating user
-
defined functions both using <cfscript> and using <cffunction> tags and continues
by describing the process of creating components that contain one or more user
-
defined function
definitions. In addition, the student will learn the process of
creating custom tags in this chapter
which covers creating, saving, and using a custom tag.


Teaching

Tips

and

Strategies


The creation of modular code can seem very straightforward in theory; however, some students
may find that it is quite confusing when

trying to apply the concepts in an application. You
should discuss with the students and possibly have a brain
-
storming session to determine the
differences between when to use each method presented in the chapter. You may even have the
students create
one application that uses Include files, custom tags, and components that
incorporate user defined functions.


Lecture

Notes


I.

Using Include Files


embeds other ColdFusion templates within another template

A.

Template attribute


contains a relative path an
d file name of the template to include

II.

User Defined Functions


a block of code that performs a task and returns a value; used to
encapsulate business logic

A.

UDFs are rarely used to output content

B.

Functions must be within the scope of the page either by def
ining the function inside
the page or including a template with the function definition

C.

Creating a User
-
Defined Function

1.

CFScript syntax

a)

Function functionName ([parameter1], [parameter2]…)

{


CFScript statements;


Return (returnvalue);

}

b)

Function name cann
ot be a ColdFusion built
-
in function name

c)

No two functions can have the same name

d)

All functions return a value


the Return statement causes the control
return to the calling page

ColdFusion

Chapter 9: Modular Code

CF_IM09
-
2


e)

Optional arguments sent to a function must be accessed via the
Arugments arr
ay

2.

<cffunction> tag

a)

<cffunction> defines the function and uses the following syntax:

(1)

<cffunction name=”nameOfFunction” returnType=”datatype”>

b)

<cfargument> defines the arguments that are passed into the function
and uses the following syntax:

(1)

<cfargument na
me=”nameOfArgument” type=”dataype”
required=”true”>

c)

<cfreturn> defines the value that is returned from the function and uses
the following syntax:

(1)

<cfreturn ValueReturned>

d)

ColdFusion tags are used to create the body of the function

3.

When using either <CFScr
ipt> or <cffunction>, functions may be called
using:

FunctionName(parameter1, parameter2…)

4.

Variable scope within functions

a)

Variables available in the calling page may be referenced using the
“variables” scope identifier and the name of the variable

b)

Variab
les declared within the function using the var statement are local
to that function and when control leaves the function, these variables
are no longer available

III.

Custom Tags


allow you to extend the CFML language by adding your own tags or using
custom ta
gs built by others

A.

To share a custom tag among applications in multiple directories and enable them to
be globally accessible, save the .cfm file in C:
\
CfusionMX
\
CustomTags. Otherwise,
the tag will only be available to pages within the directory where it
is stored.

B.

Access a custom tag by prefixing the name of the file with “cf_” and removing the
.cfm extension. For example, a file named
display.cfm
would be accessed via a tag
such as
<cf_display>

C.

ColdFusion first looks in the directory of the calling page

and then looks in the
CustomTags directory to find a custom tag

D.

Parameters are passed to a custom tag using attribute=value pairs

E.

Variables in the calling page cannot be accessed by the custom tag unless it is sent as
a parameter and variables in the cust
om tag cannot be accessed by the calling page

F.

Paired Tags


permit passing of data between the tags other than through attributes

1.

Data between tags is passed in a variable called thistag.genteratedcontent

2.

thistag.executionmode can be used to determine the
current stage of
execution; a value of “start” when the tag is called by the opening tag and a
value of “end” when the tag is called by the closing tag.

IV.

ColdFusion MX Components (CFC)


encapsulate functionality and provide a standard
interface to the cli
ent

A.

The client can access this functionality by invoking methods on the components

B.

Component files are written like other ColdFusion templates, but use the .cfc file
name extension and can be saved in any directory accessible through ColdFusion
mappings or

custom tags

ColdFusion

Chapter 9: Modular Code

CF_IM09
-
3


C.

<cfcomponent> tag contains one or more <cffunction> tags

D.

To call a component, use the <cfinvoke> which expects the following arguments:

1.

component


the name of the component without the .cfc extension

2.

method


name of the method to access

3.

ret
urnvariable


local variable which will accept the return value

ColdFusion

Chapter 9: Modular Code

CF_IM09
-
4


Review Questions

Answers:


1.

The <cfinclude> tag allows you to embed other ColdFusion templates within your template.

Example: <cfinclude template=”myHeader.cfm”>


2.

Files are included in t
emplates to:

1.

Eliminate repetition of code (reuse code)

2.

Separate business logic and program flow


3.

The kinds of files normally included are:

1.

Common header and footer for templates

2.

Functions and/or queries could be in Include files


4.

Two ways that you c
an write user
-
defined functions in ColdFusion are:

1.

Within <cfscript> tags

2.

Using the <cffunction> tag.


5.

Required arguments are the arguments defined in the function definition. Optional
parameters/arguments are any extra parameters sent into the functio
n by the function call.


6.

The var section of functions comes at the top of the function and is used for defining local
variables in the function.


7.

When we need to write an algorithm or business logic that returns a single value as a result,
we use a

UDF, since all functions return a value.


8.

When the code needs to output content and does not return a value to the calling template,
we use a custom tag.


9.

Custom tags get data through attributes. Values are passed into the tag using attributes
(name

value pairs) and read by the tag through the attributes.


10.

Custom tags are saved in a special directory made for them under the ColdFusion directory
usually c:
\
cfusionmx
\
CustomTags
\

if we want them to be accessed by all applications.


11.

Paired tags a
re used if you want to pass data between the tags and do some processing on
that data. Data between tags is passed in a variable called thistag.generatedcontent using
which Custom tags can access and change the results generated within the body of the tag.


12.

Code that is within a component but outside the functions is called initialization code. It
runs every time the component is invoked.

ColdFusion

Chapter 9: Modular Code

CF_IM09
-
5


True/False

Answers:


1.

True

2.

True

3.

True

4.

False: ColdFusion first searches for Custom Tags in the directory o
f the calling template.

5.

True

6.

True

7.

False: Functions return a single value so you should use a UDF if you need to return a
value.

8.

False: They are like any ordinary ColdFusion pages but with the .cfc suffix.

9.

True

10.

True


Programming Exerci
ses

Answers:


1.

See ch09ex01sol.cfm & ch09ex01solUDF.cfm (udf)

2.

See ch09ex02sol.cfm & validate.cfm (custom tag)

3.

See ch09ex03sol.cfm & validate.cfc (ColdFusion component)

4.


1.

<cfscript>

2.


function add(num1, num2, num3) {

3.


return num1 + num2 +

num3;

4.


}

5.

</cfscript>

6.

<cfoutput> #add(1,2,3)# </cfoutput>


Project Building Exercises

Answers:


1.

Done as part of final shopping cart project. See productlist.cfm, getlist.cfc.

2.

Done as part of final shopping cart project. See header.cfm and footer.c
fm.

3.

Headercftag.cfm

(save in the CustomTags folder)

1.

<cfparam name="attributes.pagetitle" default="Product Catalog">

2.

<head>

ColdFusion

Chapter 9: Modular Code

CF_IM09
-
6


3.

<title>Innovations
-

<cfoutput>#attributes.pagetitle#</cfoutput></title>

4.

</head>

5.

<body>

6.

<p align="center">

7.

<font size="+4" color=
"Blue"><b><i>Innovations </i></b><br /></font>

8.

<font size="+2" color="Blue"> <b>

9.

<cfoutput> #attributes.pagetitle# </cfoutput> </b> </font> <br /> </p>


ProductList.cfm

Instead of the cfinclude for the header file put this line:

<cf_headercftag pagetitle=
"Product Catalog">