Script-based controllers and models, tag-based views

Thursday, October 22nd, 2009

I’m looking forward to playing around with ColdFusion 9 when I get some more time here in a month or so. One of the areas that interests me in particular is the ability to write full CFCs in CFScript syntax.

Think about this scenario in ColdFusion on Wheels:

  • Controllers in script syntax
  • Models in script syntax
  • Views in tag syntax

I like the idea of this, though I’ll admit that I haven’t tried it yet. The more data- and logic-heavy parts of the app can be script-driven, while the tag-based syntax can be used with HTML.

Imagine this scenario for a Wheels model class:

component extends="Model" {
 
    /**
      @hint Initializes associations and validations
    */
    public function init() {
        // Associations
        hasMany("chirps");
        // Validations
        validatesPresenceOf("firstName,lastName,email,gender,urlId");
        validatesUniquenessOf("email,urlId");
        validatesLengthOf(properties="firstName,lastName", maximum=50);
    }
 
}

And this for a Wheels controller:

component extends="Controller" {
 
    /**
      @hint Shows form for registering for social network.
    */
    public function register() {
        user = model("user").new();
    }
 
    /**
      @hint Saves registration submission.
    */
    public function create() {
        user = model("user").new(params.user);
        user.save();
 
        if(user.hasErrors()) {
            renderPage(action="register");
        }
        else {
            flashInsert(success="Thank you for registering for Social Network.");
            redirectTo(controller="home");
        }
    }
 
}

And then any view logic can be mixed in via tags with all the other HTML tags (right where it belongs).

<cfsetting enablecfoutputonly="true">
 
<cfparam name="user">
 
<cfoutput>
 
<h1>Register for Social Network</h1>
 
#startFormTag(action="create")#
 
#textField(label="First Name", objectName="user", property="firstName")#
#textField(label="Last Name", objectName="user", property="lastName")#
#textField(label="Email", objectName="user", property="email")#
 
<fieldset>
    <legend>Gender</legend>
    #radioButton(label="Male", objectName="user", property="gender", tagValue="M")#
    #radioButton(label="Female", objectName="user", property="gender", tagValue="F")#
</fieldset>
 
<fieldset>
    <legend>Profile Address</legend>
    #textField(label="http://#cgi.script_name#/profile/", objectName="user", property="urlId")#
</fieldset>
 
<div>
    #submitTag(value="Register Now")#
</div>
 
#endFormTag()#
 
</cfoutput>
 
<cfsetting enablecfoutputonly="false">

This makes me excited for Railo adoption of script-based CFCs and for time to warp 5 years into the future when script-based CFCs are more widely used in general.

Technorati Tags: , , , , , , , , , , , , , ,

4 Responses to “Script-based controllers and models, tag-based views”

  1. tony petruzzi Says:

    call me an old timer, but i like my tags.
    the biggest gotcha with doing cfcs in pure cfscript is that not many tags have been ported over to pure cfscript. granted the most widely used ones have, but not all of them.

  2. Chris Peters Says:

    I thought I had seen something where in script a developer was tapping into the com.adobe.coldfusion packages to get some of the tags like cfquery. I’ll have to look into that more later.

  3. walt Says:

    this is kind of cheesy, but I’ll repeat what I said before: using scripting in CFCs instead of tags makes me feel a little more grown up. :D

  4. Chris Peters Says:

    Haha, Walt. :)

    It’s indisputable that CFML makes for a great templating language, so the combination of language elements in my post above makes me happy.

Leave a Reply