Posted in ColdFusion | Posted on 12-22-2009 | 5,136 views
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.


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.
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.
[Add Comment] [Subscribe to Comments]