Welcome!

You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

Calling all custom tags

Calling all custom tags

You probably know about custom tags, but are you aware of all the possibilities for controlling who can access them and how?

Perhaps you knew that a custom tag could be placed in the cfusion\customtags directory of the CF server to provide shared access by all users on the server, and you may even know that a tag placed in the same directory as the calling template will be found first (we'll call this a "local" custom tag).

But what if you want to share a custom tag — not among everyone on the server, but among only a handful of applications not even in the same directory? How do you solve that problem? This is also a real problem on hosted servers, where perhaps you're specifically precluded from adding to the shared customtags directory. What if you want to share a custom tag among several of your applications? Do you believe you have to place a copy in every directory you have on that server? That would negate almost entirely the benefits of easily reused code!

And have you ever wondered if you could literally prevent someone from executing a custom tag? (Still another benefit important to those on shared servers.) Or did you know you could code your app such as to prevent someone from overriding a call to a shared custom tag with a local one? (Both these concerns would also be important in secured departmental or organizational environments.) Last, what if you wanted to call a custom tag whose name was in fact driven by a variable? All these things, and more, are possible.

There are at least six ways to control how and where a custom tag is called. In this month's Journeyman ColdFusion article, I'll expand upon these topics and discuss not only the language aspects of calling custom tags but also some advanced security aspects to control access to custom tags. We'll even throw in a couple of alternative ways of reusing code that are not quite custom tags but may be interesting to you.

For the Basics

In the February CFDJ (Vol. 2, issue 2), Ben Forta wrote a great article that included discussions of the basics of using and calling custom tags. He explained the important differences between using custom tags and CFINCLUDE and he alluded to a couple of advanced approaches to calling custom tags.

In case you're new to custom tags, let me simply restate that they're a great way to reuse code in ColdFusion applications. They're simply CFML files (C++ is supported as well) that perform some routine you may want to execute from within more than one template (.cfm file). The code in a custom tag is "called" using special CFML language syntax. I'll describe all those approaches as well as explain when and why you should use each approach. Location, Location, Location

Given that the key benefit of custom tags is reuse of code, a very important aspect of using them is paying attention to just where the custom tag is stored so it can be shared among multiple programs. Of course, like all CFML files, it's to be placed on the ColdFusion server. But beyond that, there are important considerations that affect the choice of location as well as the language syntax used to call them.

Some folks are under the mistaken presumption that there are only one or perhaps two places where they may be stored. In fact, you can place a custom tag anywhere on the ColdFusion server. This has tremendous and often untapped benefits for sharing a custom tag among everyone on the server, just those in a particular application directory or those within a logical group of applications. That last choice is the one that many may not know. Knock, Knock

The first matter to clarify concerns the many ways to call a custom tag. This is a quick reminder of the various approaches, with explanations of why you would use each approach. There are basically three approaches to calling them, with a couple of variations for spice.

CF_

Perhaps the most common approach that people will know is to refer to the custom tag by way of calling it with the "cf_" prefix. That is, if we had a custom tag called "format.cfm", we could call it via:

<cf_format>

Notice that the file name of the tag follows the "cf_", while the .cfm file extension is not used in the reference. When this code is encountered in a template, that first template is effectively placed on hold while the code in the custom tag is executed. The output of the custom tag, if any, is simply accumulated along with the output of the caller.

Again, just as a quick refresher, Figure 1 depicts a call to a very simple custom tag. What the custom tag does isn't really important (it simply causes text passed to it to be converted to uppercase and wrapped in HTML italics tags), but if you're new to using them, you can also see the means by which data can be passed to and used within a custom tag (providing parameter=value pairs after the call to the tag and use of the "attributes" prefix within the custom tag to refer to the passed parameters).

While this depicts the custom tag creating output that would simply be available as part of the output of the caller (when sent to the browser), it is possible — and sometimes highly desirable — to return data to the caller instead as a variable that can be processed entirely under the control of the caller. This involves using the "caller." prefix in the custom tag to create such a variable, which would be available in the calling template — a subject beyond the scope of this article, but hopefully this is enough to get you started if you're new to it.

So that's a quick reminder of how custom tags work. The important point we were stressing was the question of how it was called, using the "cf_" syntax. Going back to that broader subject, the next issue to consider is where the custom tag will be found when processing occurs.

Where's Waldo?

We mentioned earlier that tags placed in the \cfusion\customtags directory where CF server is installed would be shared, that is, every application on the server would have access to them. And this is true. A custom tag called using this syntax will find a custom tag of the given name, if it exists in the customtags directory.

But it will also look for the tag in any subdirectory of \cfusion\customtags, as well. That can be useful, though the more important benefit of such subdirectories will be explained in the next section.

We had also mentioned that a "local" custom tag could override the shared one, and this is true with this syntax, as well. That is, if the called custom tag is located in the same directory as the caller template, then the code in that local copy of the custom tag is what will be executed.

This can be very valuable when you're perhaps testing a new version of a custom tag. Rather than impact everyone on the server, you can have a testing directory in which your local copy is placed and only that code will see the local copy. Once it's ready for "prime time," it can be moved to the shared customtags directory.

These characteristics of how the custom tag can be found in either a local or shared location are handy, but there are times when either they will be inappropriate (you don't want to allow a local override), inaccurate (you want to execute a specific version of a custom tag in a particular subdirectory of \cfusion\customtags) or insufficient (you want to share a custom tag from templates in more than one directory, but you don't want to or can't place it in the shared customtags directory).

For these situations you may need to turn to yet another alternative for calling custom tags: the CFMODULE tag.

CFMODULE

CFMODULE is another way to call a custom tag that brings all the same benefits as the CF_ approach in terms of passing and returning data, as well as how processing in the caller is placed on hold and output of the custom tag is returned to the caller. The difference, besides the obvious change in tag syntax, is that CFMODULE allows much greater control over exactly where a custom tag is expected to be found. Sometimes you need that control, as we alluded to in the previous section. There are, in fact, two styles of CFMODULE: using the NAME or TEMPLATE attribute. Further, each of these two styles can be used two ways beyond their basic capability.

What's in a NAME?

We could also call our example "format.cfm" custom tag, above, using the following syntax: <CFMODULE NAME="format">

Note that we still refer only to the name of the custom tag, not the ".cfm" extension.

And we could also pass any expected attributes to the custom tag by simply listing them after the call, just as we did with the "cf_" approach. Continuing from our example above, we could call it as: <CFMODULE NAME="format" TEXT="#fname#">

On the surface it may seem as though there's no difference, even that it may take a little more typing to achieve the apparently same result. But there's more here than meets the eye.

A very important aspect of the CFMODULE NAME= approach is that it will not look first in the local directory — that is, the directory where the calling template is located — for the custom tag. It will only look for it in the shared customtags directory. That could seem to be a limitation, but it's actually a benefit if for some reason you want your code to be sure not to execute anything except the shared custom tag, perhaps for security reasons.

There's an interesting and possibly important twist to using the CFMODULE NAME= approach. Used as described, it will search for the tag in the shared customtags directory, and like the CF_ approach it will also look below that directory to any subdirectories under \cfusion\customtags. But one thing that's unique to this approach is that you can name the exact subdirectory of custom tags in which you mean to find the custom tag.

That is, if you prefix the name of the custom tag (within the NAME attribute) with the name of the subdirectory, then only that specific subdirectory's version of the custom tag will be executed. For example, if we had a "salesapps" subdirectory under custom tags, and we placed our format.cfm file in that directory, we could execute that specific version of the custom tag using the following:

<CFMODULE NAME="salesapps.format" TEXT="#fname#">

Notice that all that's changed is the prefix of "salesapps." before the custom tag name. This now forces the template to execute not any local version, not even the version in \cfusion\customtags or any random subdirectory of it, but only format.cfm as it exists in the \cfusion\customtags\salesapps directory.

This can be a useful feature once you understand its purpose. You might use it to organize shared custom tags to designate certain directories for certain departments or application groups (just to make a large number of tags more manageable and keep control under the right groups), or possibly to designate versions in a life cycle/change management paradigm with subdirectory names such as "dev", "qa" and "production".

(As an aside, there's no reason you couldn't use a global variable set in application.cfm to hold the name of the intended directory when executing in a particular mode, such as with <CFSET ctdir="dev">. Then you could use that variable in the call to the custom tag, as in <CFMODULE NAME="#ctdir#.format">. This is perfectly acceptable syntax.)

Finally, the NAME attribute can also specify not just a single subdirectory of the share customtags directory, but even "sub-subdirectories." In other words, if our format.cfm file were stored in a directory called \cfusion\customtags\salesapps\dev\, we could specify <CFMODULE NAME= "salesapp.dev.format">. Notice that each subdirectory is simply specified as another "directoryname." prefix, in just the same order as the directories appear under \cfusion\customtags.

Next time, I'll look at what you can do if you can't place files in the customtags directory, by introducing you to the TEMPLATE= approach.

Ed.Note: The following line under "So...Where to Search?" was left out of the bulleted list in last month's Journeyman column: "Search defusion, cfadvisor, cfdj and other sites."

More Stories By Charlie Arehart

A veteran ColdFusion developer since 1997, Charlie Arehart is a long-time contributor to the community and a recognized Adobe Community Expert. He's a certified Advanced CF Developer and Instructor for CF 4/5/6/7 and served as tech editor of CFDJ until 2003. Now an independent contractor (carehart.org) living in Alpharetta, GA, Charlie provides high-level troubleshooting/tuning assistance and training/mentoring for CF teams. He helps run the Online ColdFusion Meetup (coldfusionmeetup.com, an online CF user group), is a contributor to the CF8 WACK books by Ben Forta, and is frequently invited to speak at developer conferences and user groups worldwide.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


@ThingsExpo Stories
"Cloud Academy is an enterprise training platform for the cloud, specifically public clouds. We offer guided learning experiences on AWS, Azure, Google Cloud and all the surrounding methodologies and technologies that you need to know and your teams need to know in order to leverage the full benefits of the cloud," explained Alex Brower, VP of Marketing at Cloud Academy, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clar...
In his session at 21st Cloud Expo, Carl J. Levine, Senior Technical Evangelist for NS1, will objectively discuss how DNS is used to solve Digital Transformation challenges in large SaaS applications, CDNs, AdTech platforms, and other demanding use cases. Carl J. Levine is the Senior Technical Evangelist for NS1. A veteran of the Internet Infrastructure space, he has over a decade of experience with startups, networking protocols and Internet infrastructure, combined with the unique ability to it...
"IBM is really all in on blockchain. We take a look at sort of the history of blockchain ledger technologies. It started out with bitcoin, Ethereum, and IBM evaluated these particular blockchain technologies and found they were anonymous and permissionless and that many companies were looking for permissioned blockchain," stated René Bostic, Technical VP of the IBM Cloud Unit in North America, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Conventi...
Gemini is Yahoo’s native and search advertising platform. To ensure the quality of a complex distributed system that spans multiple products and components and across various desktop websites and mobile app and web experiences – both Yahoo owned and operated and third-party syndication (supply), with complex interaction with more than a billion users and numerous advertisers globally (demand) – it becomes imperative to automate a set of end-to-end tests 24x7 to detect bugs and regression. In th...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
"MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Large industrial manufacturing organizations are adopting the agile principles of cloud software companies. The industrial manufacturing development process has not scaled over time. Now that design CAD teams are geographically distributed, centralizing their work is key. With large multi-gigabyte projects, outdated tools have stifled industrial team agility, time-to-market milestones, and impacted P&L stakeholders.
"Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
"Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, discussed how from store operations and ...
"There's plenty of bandwidth out there but it's never in the right place. So what Cedexis does is uses data to work out the best pathways to get data from the origin to the person who wants to get it," explained Simon Jones, Evangelist and Head of Marketing at Cedexis, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
SYS-CON Events announced today that Telecom Reseller has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
It is of utmost importance for the future success of WebRTC to ensure that interoperability is operational between web browsers and any WebRTC-compliant client. To be guaranteed as operational and effective, interoperability must be tested extensively by establishing WebRTC data and media connections between different web browsers running on different devices and operating systems. In his session at WebRTC Summit at @ThingsExpo, Dr. Alex Gouaillard, CEO and Founder of CoSMo Software, presented ...
WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, introduced two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a multip...
A strange thing is happening along the way to the Internet of Things, namely far too many devices to work with and manage. It has become clear that we'll need much higher efficiency user experiences that can allow us to more easily and scalably work with the thousands of devices that will soon be in each of our lives. Enter the conversational interface revolution, combining bots we can literally talk with, gesture to, and even direct with our thoughts, with embedded artificial intelligence, whic...
SYS-CON Events announced today that Evatronix will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Evatronix SA offers comprehensive solutions in the design and implementation of electronic systems, in CAD / CAM deployment, and also is a designer and manufacturer of advanced 3D scanners for professional applications.
Leading companies, from the Global Fortune 500 to the smallest companies, are adopting hybrid cloud as the path to business advantage. Hybrid cloud depends on cloud services and on-premises infrastructure working in unison. Successful implementations require new levels of data mobility, enabled by an automated and seamless flow across on-premises and cloud resources. In his general session at 21st Cloud Expo, Greg Tevis, an IBM Storage Software Technical Strategist and Customer Solution Architec...
To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
An increasing number of companies are creating products that combine data with analytical capabilities. Running interactive queries on Big Data requires complex architectures to store and query data effectively, typically involving data streams, an choosing efficient file format/database and multiple independent systems that are tied together through custom-engineered pipelines. In his session at @BigDataExpo at @ThingsExpo, Tomer Levi, a senior software engineer at Intel’s Advanced Analytics gr...