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:
2 return s & '<input type="text" name="#arguments.name#" value="#currentValue#">';
3 }
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:
2 savecontent variable="s" {
3 include "render/eventdate.cfm";
4 }
5 return s;
6 }
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.


Comment 1 written by Daniel Budde on 23 December 2009, at 8:09 AM
It always makes me feel a little bad to have to break encapsulation, but as you say, there are those exceptions to the rule. I always just comment them well and since they are few and far between, they have just never caused any trouble.
Comment 2 written by Tony Nelson on 23 December 2009, at 9:34 AM
Comment 3 written by Raymond Camden on 23 December 2009, at 9:54 AM
Comment 4 written by Daniel Budde on 23 December 2009, at 9:59 AM
Comment 5 written by Tony Nelson on 23 December 2009, at 10:31 AM
case "event date": {
return new Include("render/eventdate.cfm",arguments);
}
Include.cfc:
component {
public string function init(template, params) {
structDelete(variables, "init");
structDelete(variables, "this");
structAppend(variables, arguments.params);
savecontent variable="local.html" {
include arguments.template;
}
return local.html;
}
}
Now any variables declared inside render/eventdate.cfm won't bleed into your component.
Comment 6 written by Raymond Camden on 23 December 2009, at 10:32 AM
[Add Comment] [Subscribe to Comments]