Script-based controllers and models, tag-based views

October 22, 2009 · Chris Peters

One of the areas of ColdFusion 9 that interests me in particular is the ability to write full CFCs in CFScript syntax.

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 CFWheels:

  • 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 CFWheels 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);
}
}
view raw Model.cfc hosted with ❤ by GitHub

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");
}
}
}
view raw Controller.cfc hosted with ❤ by GitHub

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

<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>
view raw view.cfm hosted with ❤ by GitHub

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.

About Chris Peters

With over 20 years of experience, I help plan, execute, and optimize digital experiences.

Leave a comment