| By Steve Bryant | Article Rating: |
|
| January 25, 2006 06:15 PM EST | Reads: |
10,957 |
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!
Published January 25, 2006 Reads 10,957
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
![]() |
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. |
||||
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Reader Sued
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Cans Another 9% of its Workforce
- Adobe Betas Target RIAs and Cloud Computing
- Adobe MAX 2009 Online
- Thinking of Flex in London
- Moyea DVD4Web Converter V2.0 Converts DVD to FLV Fast and Synchronously with Watermarks
- Adobe & Salesforce Cut Cloud Deal
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Is Microsoft as Free as Open Source?
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Reader Sued
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Microsoft Expression Web Has Got Game
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Bruce Chizen Joins Voyager Capital as Venture Partner
- My Top Seven Wishes From Adobe MAX 2009
- Adobe Flex Developer Earns $100K in New York City
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Web Services Using ColdFusion and Apache CXF




































