Encapsulating Front-End Logic with Tag Libraries
Creating modular display logic has always been somewhat of a challenge... there are many different approaches to take, and it's hard to determine which will be the most flexible and maintainable in the long run. I've always been accustomed to coding all my display templates so that all variables are within the attributes scope. This allows any template to be called using <cfmodule>, allowing it to either run on its own, or allowing required parameters to be passed in.
In addition, (following the FuseBox practice of copying all URL and Form variables to the attributes scope so that you have a universal variable scope. For me, this makes sense because it ensures consistency; no matter how my front-end templates are called, the proper variables can be available.
One approach to creating re-usable front-end logic is to import a folder of tags as a library... I've tried to mostly stay away from includes and stick to calling templates using cfmodule instead. For reusable display elements, more and more I've been using <cfimport> to reference a folder of tags. This allows me to set up a tag pre-fix for a library of custom tags. Each tag has a very specific purpose... Here's a real-world example:
<!--- import widget library --->
<cfimport taglib="views/widgets" prefix="widget">
...
...
<!--- display comments --->
<widget:comments
type="Article"
itemID="#attributes.article.article_id#"
itemName="#attributes.article.title#"/>
...
In the above example, I have a folder of tags (views/widgets) that will be referenced using the prefix keyword "widget". I call the "comments.cfm" tag and pass in the required attributes.
The actual display templates are all coded as if they were custom tags (they could alternately be called that way as well). This has become a really good way for me to separate display from business logic... the tag library references my components which provide the data access and complex business rules.