The UDF framework introduced in Chapter 22 is probably the easiest and most straightforward way to extend the language. That said, the most flexible way to extend ColdFusion is by creating your own tags. Like UDFs, custom tags let you add your own tags to the CFML language, for whatever purpose you want. Unlike UDFs, the custom tag framework has been around since ColdFusion 3.0, and has become a rich and mature part of the product. As of this writing, there are many more custom tags than UDFs. Custom tags excel at encapsulating concepts and processes and are the bedrock of many existing ColdFusion applications. You can solve many problems using either framework. And you can write extensions in ColdFusion's native language, CFML, regardless of which framework you choose for a particular task. This means you already know most of what you need, and can get started right away. The Basic Custom Tag IdeaThe idea behind custom tags is simple: to enable ColdFusion developers like you to package chunks of ordinary CFML code into reusable modules. From that point on, you can refer to the modules by name, using ColdFusion's familiar tag-based syntax. You get to define attributes for your tags, just as for regular CFML tags. Your tags can run queries, generate HTML, and perform calculations. They have almost no special requirements or limitations. Generally, custom tags are self-contained and goal-oriented. They take care of whatever processing is necessary to perform a particular task or set of related tasks. In short, custom tags can be many different things, depending on your needs. Why Modularity Is a Good ThingAs you will soon see, custom tags are easy to write, but you need to think about exactly how you want them to work. A process that might take half an hour to code as a normal ColdFusion template might require an extra 10 or 15 minutes to implement as a custom tag, because you will need to put some extra thought into how to modularize your code. If it takes extra time, why bother with all this modularity business? Because breaking your code into independent, manageable chunks has a number of significant advantages. Modularity Means Having More FunLet's face it: the idea of creating your own tagsyour very own extensions to the hip and stylish CFML languageis cool! And the fact that you get to write them using the ordinary ColdFusion syntax you already know and love makes it easy to get excited about writing them. When you're excited as a programmer, you're naturally more creative, more ambitious, and more productive. There's nothing like a burst of enthusiasm to boost your productivity. It's as close to an adrenaline rush as many of us coders are going to get, at least at our day jobs. Modularity Means Being Self-ContainedBecause custom tags are modular, they usually end up being entirely self-contained. For instance, you might create a custom tag called <cf_PlaceOrder> that takes care of all aspects of placing an order, whatever that means in practice for your application. Because this custom tag is self-contained, other developers can place the tag in their templates wherever they need to, without worrying about what your custom tag actually does internally. Modules Are Easy to MaintainWhen you're using a custom tag, you can make any changes in just one place. Again, consider the hypothetical custom tag called <cf_PlaceOrder>. If you need to add some new step to the actual processing of each order, you can update the custom tag without having to touch each template that uses the tag. Additionally, because custom tags generally represent self-contained, well-defined chunks of code that do just one thing and do it well, they are usually easier for various members of a team to maintain. Their single-mindedness and sense of purpose make them more self-documenting and easier to understand than ordinary templates. Modularity Encourages Code ReuseWhy reinvent the wheel? If someone else already has written code that gets a particular task done, it's almost always easier and more efficient to simply reuse that code, and get on to the next item in your schedule. Conversely, if you write some code that solves a problem, why not package it in such a way that you can easily use it again later? Modules Can Be Traded or SoldYou don't even have to know how to write a custom tag to take advantage of them. Hundreds of custom tags are availablemost of them freethat you can download and use in your own applications. What's more, most publicly available custom tags are unencrypted, so you can adapt them to your needs if they do almost what you need them to do, but not quite. Of course, you can share your custom tags with others if they help solve a common problem. And if one of your custom tags is particularly great, others will likely be glad to buy it from you. |