Other Ways of Calling Custom Tags

Contact Us or call 1-877-932-8228
Other Ways of Calling Custom Tags

Other Ways of Calling Custom Tags

One problem with calling custom tags using the cf_ syntax we have seen thus far is that there no way to deal with name conflicts. ColdFusion simply uses the first file it finds that matches the tag name specified. For example, if you had a file in the current directory called "FetchRecord.cfm" and a custom tag file in the cfusion/CustomTags directory with the same name. You cannot access the custom tag file in the cfusion/CustomTags directory using the cf_FetchRecord syntax. ColdFusion provides two ways to deal with the naming conflict.

CFMODULE

The <cfmodule> tag allows you to point directly to the custom tag you want to call. It takes the following syntax:

<cfmodule template="path_to_tag_file" attribute_name="custom_tag_attribute_value">

The custom tag's attributes are passed to the custom tag in the same way as they are using the cf_ syntax. The example below illustrates the use of <cfmodule> to call a custom tag.

Code Sample:

CustomTags/Demos/Runners-cfmodule.cfm
<html>
<head>
<title>Flipping Image</title>
</head>

<body>
<cfmodule template="ImageFlipper.cfm" directory="Images/Runners" height="205" width="150">
</body>
</html>

The example above shows how the attributes can be passed in to the custom tags in separate name-value pairs. It is also possible to pass in the attributes as a single structure using <cfmodule>'s attributecollection attribute:

Code Sample:

CustomTags/Demos/Runners-cfmodule2.cfm
<html>
<head>
<title>Flipping Image</title>
</head>

<body>
<cfset Runner.directory = "Images/Runners">
<cfset Runner.height = "205">
<cfset Runner.width = "150">
<cfmodule template="ImageFlipper.cfm" attributecollection="#Runner#">

</body>
</html>

Custom tags with close tags can also be called with <cfmodule> as shown in the example below.

Code Sample:

CustomTags/Demos/ProfitLoss-cfmodule.cfm
<html>
<head>
  <title>Profit Loss</title>
</head>
<body>
<h1>Profit Loss</h1>

<cfset FilePath = ExpandPath("ProfitLoss.txt")>
<cffile action="read" file="#FilePath#" variable="myfile">
<cfmodule template="Text2Table.cfm"><cfoutput>#myfile#</cfoutput></cfmodule>

</body>
</html>

Finally, with <cfmodule>, custom tags can be specified using dot notation. In this case, the name attribute replaces the template attribute. This is shown in the sample below:

Code Sample:

CustomTags/Demos/ProfitLoss-cfmodule-name.cfm
<html>
<head>
  <title>Profit Loss</title>
</head>
<body>
<h1>Profit Loss</h1>

<cfset FilePath = ExpandPath("ProfitLoss.txt")>
<cffile action="read" file="#FilePath#" variable="myfile">
<cfmodule name="Strings.Text2Table"><cfoutput>#myfile#</cfoutput></cfmodule>
<!---This calls the Text2Table.cfm custom tag file in the cfusion/CustomTags/Strings directory.--->

</body>
</html>

CFIMPORT

The <cfimport> tag is used to import a group of custom tags that can be referenced with a given prefix. The syntax is shown below:

<cfimport prefix="prefix" taglib="path_to_tag_directory">

The prefix attribute is used to specifiy a tag qualifier. A custom tag called "bar" in a library qualified with the "foo" prefix would be called as follows:

<foo:bar>

The taglib attribute points to the directory holding the custom tags. The directory path can be relative to:

  1. the current page location
  2. the web root (the page should start with a "/")
  3. a directory specified in the Administrator ColdFusion mappings page (the page should start with a "/")
Next