Aaron is using Posterous to post everything online. Shouldn't you?
Aqlong_pic_thumb
Aaron Longnion
owns this site
11
subscribers
 
 

Aaron Longnion’s ColdFusion Blog

ColdFusion, Web Architecture, and other techno babble

  • Raymond Camden Tip: cfsavecontent and cfinclude within script based CFCs

    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:

    1  case "textbox": {
    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:

    1  case "event date": {
    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.

    This entry was posted on Tuesday, December 22, 2009 at 9:29 PM. It was filed in the following categories: ColdFusion. It has been viewed 901 times and has 6 comments.

    TweetBacks

    There are no TweetBacks for this entry.

    Comments

    [Add Comment] [Subscribe to Comments]

    • Comment 1 written by Daniel Budde on 23 December 2009, at 8:09 AM

      avatar
      Although, I am not on CF9 yet, I have ran into this in the past when I wanted to e-mail template based reports from within my CFC. The reports already existed as HTML templates within the application, so the easy fix was to use <cfsavecontent> with a <cfinclude>.

      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

      avatar
      You'll want to be careful when using .cfm mixins inside singleton components to make sure any variables declared within the mixin are thread-safe.
    • Comment 3 written by Raymond Camden on 23 December 2009, at 9:54 AM

      avatar
      Agreed. I'm forcing myself to use local.x for all variables. I normally do NOT like local.foo, I just var scope, but for this component I'm using it as a way to ensure I'm always local.
    • Comment 4 written by Daniel Budde on 23 December 2009, at 9:59 AM

      avatar
      I do the same as well. All my variables used within the template are always located under (LOCAL.templateInfo).
    • Comment 5 written by Tony Nelson on 23 December 2009, at 10:31 AM

      avatar
      If you don't want to have to use local.x everywhere, you could create a small Include.cfc proxy for including templates.

      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

      avatar
      That's slick as crap. Thanks Tony.

    [Add Comment] [Subscribe to Comments]

    via coldfusionjedi.com

    read the comments on Ray's blog, too. Cool code there, too.

    Tags » ColdFusion
    • 28 December 2009
    • 4943 Views
    • 0 Comments
    • Permalink
    • Favorited 0 Times
    • Loading Retweet

    Comments 0 Comments

    Leave a Comment

     
    Got an account with one of these? Login here, or just enter your comment below.
    Posterous-login    Connect    twitter



     
    Loading...
  • Aaron Longnion's Posterous

    ColdFusion architect, American living & working in The Hague

  • About Aaron Longnion

    ColdFusion architect, American living & working in The Hague

  • Subscribe

    Subscribe to this posterous
    Follow this posterous RSS
  • Follow Me

             

Theme created for Posterous by Obox