I love epiphanies; for me they are a big part of what makes coding fun. Sure, one man's epiphany is another's old saw, but a personal discovery is still a reward in itself.
So, while this may be obvious to others (and its obviously been doable for ages), this little discovery has proven quite rewarding. Basically its using <cfmodule> as a scope-safe view for a content-type bean.
I'm building out the themes for our Mura Forums application, which has a lot (a lot, mind you) of nested data. When it comes to output, it is often difficult to "scope" the content because there's often no scope to isolate repeated blocks. Methods like using <cfsavecontent> inside a CFC work but seem to add a lot of clutter to my code, and I like having views as template-only pages separate from objects (and my designers like it even more). While fiddling around with how to lay this all out, I recalled that I'd been seeing a lot of talk about modules and custom tags lately. Frankly, I'd considered both of them relatively dead since the advent of CFCs and haven't used either in a long time. Whatever it was that sparked me putting the two together, I am certainly grateful.
Enough blather, here is the code:
Essentially I have a bean, "conferenceBean", that contains a conference for the Forums application. I want to dress up and display the bean's contents on a webpage, but there are many conferences (and forums within) to loop over and I don't want to have to worry about my variables traveling down the page in a loose scope and causing mayhem. By doing the above, I am essentially turning the <cfmodule> into a scoped version of my bean, and now have a container that I can safely output its contents from.
Inside the <cfmodule> I have (simplified version):
That's really it, content that is scoped and a template that is safely reusable throughout the rest of the display page.
Of all the bugs and errors that can creep into your code, unscoped variables dancing their way down the chain are perhaps the most difficult to debug. Hopefully this method will save you from having to hunt for them.

Recent Comments