New PHP experiment, inspired by ColdFusion
I’ve been reading up on ColdFusion MX recently, and I have to admit it looks like a really nice piece of technology. I’d previously written ColdFusion off as being too simplistic and primitive, but having seen how much its capable of I’m reconsidering my position.
If you’ve never seen ColdFusion before, it’s a server side scripting language/application server from Macromedia (who obtained it when they bought Allaire). MX is the latest version of the language, which sees it completely rewritten in Java to allow it to integrate with Java application servers and existing Java servlet applications. This is shrewd marketing on the part of Macromedia, and I’ve already seen them advertising it as something to make your existing Java web deployments easier to customise.
The thing that put me off ColdFusion originally is that it is a tag-based scripting language, specifically designed to make it easy for HTML developers with little or no previous programming experience to pick up. Here’s an example of a chunk of CF syntax:
<cfinclude template="some-other-file.cfm">
<cfif isdefined("form.name")>
<cfoutput>
Hi there, #form.name#
</cfoutput>
</cfif>
The thing that most surprised me about ColdFusion is that although the above syntax looks pretty verbose, it is actually designed in a way that means you can do an awful lot with very little code. The above in PHP would look like this:
<?php
include('some-other-file.php');
if (isset($form['name'])) {
print "Hi there, $form['name']";
}
?>
It’s less typing, but only by a bit. Where CF gets really clever though is with its handling of SQL. Here’s the code to run a SQL query and loop through outputting the results to a simple table:
<cfquery name="users" datasource="myDSN">
select name, email from users order by name
</cfquery>
<table>
<cfoutput query="users">
<tr>
<td>#name#</td><td>#email#</td>
</tr>
</cfoutput>
</table>
Here’s the same in PHP, assuming $db contains a connection to a MySQL database.
<?php
$result = mysql_query("select name, email from users order by name", $db);
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo "<tr>
<td>$row['name']</td><td>{$row['email']}</td>
</tr>";
}
echo '</table>';
?>
The ColdFusion example achieves the same effect but with less complicated code.
ColdFusion is not without its problems: it comes with a price tag, and it encourages mixing application logic with presentation code (although as with PHP this can be avoided through discipline and careful application design). Never the less, I found it interesting enough that last night I spent a few hours putting together a PHP implementation of a couple of ColdFusion concepts. As with many of the experiments I post here this is very much experimental code—I won’t be supporting it and I would not recommend using it for anything more than casual experimentation. Anyway, here it is:
- Recent Entries (the output of the example script)
- Example script’s source code
- TemplateParser class (does all the work)
- XML Template file used by the example script
With a bit more development, something like this could be a useful tool for quick-and-dirty PHP scripts that simply pull data out of MySQL and display it as HTML. I still prefer PHP, but ColdFusion has a lot of good ideas which are well worth knowing about.
Simon: "ColdFusion is not without its problems: it comes with a price tag..."
Bear in mind that NewAtlanta has decided to compete in the CFML space with their BlueDragon server. It isn't feature-comparable to Coldfusion MX (it's feature set is still one version back), but they're giving away a free, deployable version. It's missing some potentially important things (WDDX support), but there's enough there for many applications.
I'd rather spend the money for Macromedia's version, personally, but since CF is part of my hosting plan, I don't have to worry about it one way or the other.
Simon: "...it encourages mixing application logic with presentation code..."
Not so much with CFMX. CFCs (Coldfusion Components) go a long way toward encouraging code and content to exist in different places. And as a bonus, exposing a CFC's data as a SOAP web service is as simple as adding an access="remote" attribute to the function definitions in a component.
You'll also be able to do some cool stuff with XML-RPC very soon.
Roger Benningfield - 17th July 2003 11:39 - #
As a PHP person you probably havent looked much at ASP.Net, however it offers a similar templating system, with the use of code behind (where the code is kept in a seperate file) all that is listed on the HTML page is a couple of prefixed tags.
Coupling this with the object orientation available in the .net framework and you have a very powerful set of tools to work with. You can define your own components and integrate them with your HTML while keeping all the application code in a compiled library.
Ben Meadowcroft - 17th July 2003 12:08 - #
Simon Willison - 17th July 2003 12:35 - #
Hi Simon, I wrote a longer reply earlier today, unfortunately I only got a frustrating error message, and the whole message was gone when I had to hit the back button ( I did not enter an email address). So instead of retyping it all I'll only mention that Harry Fuechs (from phppatterns) has done some work regarding .net-like templates in PHP. There's a thread over here: Parsing ASP.NET templates with PHP (sitepointforums). Maybe it will be of interest to some people. Also searching for "templating" on there will bring some interesting topics.
On a sidenote: Maybe you can also do some fixing on the error message part on your commenting-part of the blogging-software (possibly redisplay the form with the errormessage; edit: I see that that's what you're usually doing. Somehow that's not what's happening when I leave the email-field blank). It's really frustrating, when the stuff disappears. Especially since this is the third this happened to me this month (not here, but overall). One persons blog would even swallow the message when hitting "preview", and return to the index-page... A tipp for all other bloggers: check your commenting-piece of your blog (i know I probably got to do it, too. I put it high on my priority list).
Sencer - 17th July 2003 18:50 - #
I've used CF in the past and, while I'm not a die-hard fan, many of the glaring weaknesses have been improved in the previous two versions. If you've dismissed it in the past (no user-defined functions in version 4.5!), it's definitely worth another look
I think that, compared with other web development languages such as ASP and PHP, it wins hands down, on these points:
Joe Grossberg - 17th July 2003 19:43 - #
I have been working with CF for quite sometime and find it to actually much more easy to work with then PHP. CF teamed with the Fusebox methodology or MVC arcitechure with the new CF Components give you all the tools you need to create a nice application where your programming logic is separate from your display code. The best part of CF is how fast you can develop some pretty nice apps. Yes it does cost money but I have Macromedia's products to be excellent.
Really when you get down to it PHP with a nice template and DB libraries can get your PHP development almost on par with CF. That is the combo I use when I need to produce an app for someone on a strict budget but who still wants a RAD app.
BTW, thanks for the great weblog!
Kurt Wiersma - 17th July 2003 20:12 - #
Hello also JSP world is going towards a tag-style code... give a look at the new JSTL (Standard tag library) and JSP 2.0 specification
JSP2.0: http://developer.java.sun.com/developer/technicalA rticles/javaserverpages/JSP20/
JSTL: http://java.sun.com/products/jsp/jstl/index.html
emi - 18th July 2003 09:15 - #
PHP developers looking to expand their resume into coldfusion might also want to look into using the cfscript tag. It allows for a friendly scripting style interface in regular coldfusion templates.
check this page, and the url mentioned at the bottom to read up on it.
http://livedocs.macromedia.com/cfmxdocs/Developing _ColdFusion_MX_Applications_with_CFML/CFScript.jsp
Nathan Strutz - 18th July 2003 17:44 - #
Bill Humphries - 19th July 2003 04:01 - #
Ben: quick question about ASP.Net, which I ought to take a look at.
Is the model there a parser that looks for events pertaining to executable code and feeds the data in the element to a handler?
In that case, is the right model to do things the ASP.Net way to use an XSLT engine that supports extensions aka EXSLT?
Bill Humphries - 19th July 2003 04:10 - #
"Im still stunned at the number of template libraries for PHP. However, Im still leaning towards using PHP to generate XML and rendering to the browser with XSLT. In places where load and speed matter, then a proxy chain of Apache 2.0 - Cocoon - Apache 2.0+PHP may be best since you gain Cocoon's caching of XSLT"
Bill - you may want to check out Krysalis which is a port of Cocoon to PHP
Harry Fuecks - 21st July 2003 10:46 - #
Mike Kozlowski - 21st July 2003 15:10 - #
Simon: Macromedia has released their Developers Resource Kit 4, which contains my XML-RPC Framework for CF.
Roger Benningfield - 26th July 2003 00:03 - #
Vicente Reig - 12th August 2003 17:48 - #
John di Stefano - 5th December 2003 09:24 - #
ramesh - 30th December 2003 19:01 - #
rmunoz - 14th July 2004 07:38 - #
ilghar - 12th February 2006 04:06 - #