Welcome!

ColdFusion Authors: Yakov Fain, Pat Romanski, Liz McMillan, Maureen O'Gara, Greg Ness

Related Topics: ColdFusion

ColdFusion: Article

tag.cfc 0.1: Write Your Own Code Generator

Write your own code generator

Back when I had some free time, I started working on my own code generator, partly because other code generators create code slightly different from my preferences and partly for the challenge.

I have since gotten too busy to finish it, but I went ahead and finished the kernel component of the system - the one that actually generates the code. To download the component go to www.bryantwebconsulting.com/cfcs.

One advantage of ColdFusion being a tag-based language is that generating code for ColdFusion largely means writing tags.

The best way that I can think to show how tag.cfc works is by example. cf_sebForm.cfc and cf_sebField.cfc are both components that are inherited from tag.cfc. They are each used to generate the code for my cf_sebForm and cf_sebField custom tags.

These components are actually very simple. They extend tag.cfc and each have two methods: vtml() and schema(), which return the VTML for the tag and the XML Schema for the tag, respectively.

Here is some example code using these components to output the code needed for these custom tags:

<cfset sebForm = CreateObject("component","cf_sebForm").init()>
<cfset fields = ArrayNew(1)>

<cfset sebForm.setAttribute("formname","myform")>
<cfset sebForm.setAttribute("librarypath","/lib/")>

<cfset fields[1] = CreateObject("component","cf_sebField").init()>
<cfset fields[1].setAttribute("fieldname","blah")>
<cfset fields[1].setAttribute("label","Hello")>
<cfset fields[1].setAttribute("type","text")>
<cfset fields[2] = CreateObject("component","cf_sebField").init()>
<cfset fields[2].setAttribute("label","Submit")>
<cfset fields[2].setAttribute("type","submit")>
<cfset sebForm.addTag(fields[1])>
<cfset sebForm.addTag(fields[2])>
<cfoutput>
<pre>#HTMLEditFormat(sebForm.write())#</pre>
</cfoutput>

The code that this code would output is the following:

<cf_sebForm formname="myform" librarypath="/lib/">
    <cf_sebField fieldname="blah" type="text" label="Hello" />
    <cf_sebField type="submit" label="Submit" />
</cf_sebForm>

In order for the previous code to be useful, of course, you wouldn't want the values to be hard-coded. You would need to get those values from a form or from data in a database, for example. While tag.cfc doesn't handle that part for you, it will handle most of the last step of code generation once you have gathered that information.

Incidentally, when I was working on my own code generator (a task I hope to return to someday), I used my DataMgr component to get the structure of the database. Since it works the same across multiple databases, it makes for a nice cross-database solution.

You can download tag.cfc from my site. I also have two sets of tag CFCs that inherit from tag.cfc. The first, "CFCs", includes components to generate cfcomponent,cffunction and cfargument. The second, "sebtags", is a set of CFCs used to generate my custom tags.

This code illustrates my best understanding of good OO code, so it should be a good example of such. That being said, if someone can show why it isn't a good example of such, let me know.

Good luck!

More Stories By Steve Bryant

Steve Bryant is the founder and CEO of Bryant Web Consulting LLC (www.bryantwebconsulting.com) and teaches ColdFusion at Breakaway Interactive (www.breakawayinteractive.com). He got his BA in philosophy at Oklahoma State University. Steve, one of the top ColdFusion developers in the country, still has no idea how that led to a career in Web development. Steve blogs at steve.coldfusionjournal.com as one of CFDJ's published bloggers.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
SYS-CON Italy News Desk 01/25/06 07:28:57 PM EST

Back when I had some free time, I started working on my own code generator, partly because other code generators create code slightly different from my preferences and partly for the challenge.

SYS-CON India News Desk 01/25/06 06:24:31 PM EST

Back when I had some free time, I started working on my own code generator, partly because other code generators create code slightly different from my preferences and partly for the challenge.