Tip: cfsavecontent and cfinclude within script based CFCs

Ok, so I know that I’ve said (many times) that including layout in a CFC is generally a bad idea. I still think it is. But like most rules there are always exceptions. Normally this wouldn’t be a big deal, but HTML and JavaScript within a script based component is - well - it’s ugly. Here is an example:

case "textbox": { return s & '<input type="text" name="#arguments.name#" value="#currentValue#">'; }

This simple example works, but more complex HTML gets messier. I could have switched the component over to tags. It’s not like that would be the end of the world! But then I remember - you can use savecontent within script based cfcs. So instead of the inline HTML you see above, I now use:

case "event date": { savecontent variable="s" { include "render/eventdate.cfm"; } return s; }

Woot. I wish I had remembered this when I began the project, but I’m guessing I’ll be getting used to ColdFusion 9 syntax until right around the release of ColdFusion 10.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments