tag.cfc 0.1: 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.

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 to 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 follows:
<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 much 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 crosss-database solution.

You can download tag.cfc from my site. I also have to 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!

ColdFusion in Tulsa

I am looking for companies or individuals in Tulsa, Oklahoma that use ColdFusion. I run the Tulsa Macromedia User's Group and we are thinking about switching to a ColdFusion Users Group, to better focus on that which interests the attending memebers the most. I would like to find out how much interest there is in the community for a ColdFusion group and when would work best for those interested.

No, this doesn't have anything to do with the Adobe buyout of Macromedia. Given that we will have to change our name anyway, however, the timing is convenient.

Anyway, while I know of several companies and individuals in Tulsa that use ColdFusion, I would like to learn of any of which I am unaware. I have done a Google search, but many of those I know using ColdFusion don't turn up in a Google search for such, so I imagine others using ColdFusion in Tulsa wouldn't turn up in a Google search as well.

If you (or someone you know) are in (or near) Tulsa and use ColdFusion, let me know. You can either leave me a comment or contact me directly.

While I am on the topic of ColdFusion, a new site was just brought to my attention: ColdFusion Gear sells T-Shirts (and soon more) for ColdFusion developers. I haven't gotten any yet, but I will soon.

Thanks!

BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.