Application Generation

Musings on DSLs, DSM, Agile, SPLs and CFML by Peter Bell

CF Template – CFML for Code Generation

with 4 comments

I’m doing a project right now which requires me to generate code for a Coldbox app from a collection of DSLs, so I’m updating/upgrading all of my code generation tools (for the last couple of years I’ve spent most of my time using runtime interpretation of the DSLs instead). I just wanted to point out that CF template is a really useful little project that might be helpful if you need to do any code generation using ColdFusion.

In short, CF Template allows you to use CFML to generate ColdFusion code, using – well – ColdFusion code!

When you think about it, CFML *is* a templating language. We usually use it to take data (often from a database) and to insert that into templates to generate HTML files. We usually just stream those files straight to the browser, but we could save them to the file system if we wanted. So, we’re already using CFML to generate (HTML) code. The only problem with using CFML to generate CFML is that you need to distinguish between generation time and runtime commands. When you have a <cfoutput query=”mydata”> are you using that to loop over metadata and generate (say) getters and setters for a business object, or do you actually want your generated file to contain a cfoutput? Same with variables. So, the only thing we need to do to allow the full power of CFML for our code generation is to have a way in our templates to distinguish between generation time and runtime code.

A good general rule of thumb with templating languages is that you want the templates to look as much like the code you’re going to generate as possible. The closer they are, the easier it is to debug your templates. So, we need to find a way to make a small change to our generation time tags and variables so they can be distinguished from the runtime code we want to put into our generated files.

In CF template, by default, we simply replace single angle brackets with double brackets and single #’s with single %’s to denote generation time tags and variables respectively. That’s it.

CF Template is not a full code generator. It simply takes metadata (of any kind), a template and a target file path and uses them to generate a file and put it in a target destination. It’s a great tool if you’re building a generator – why not check it out?

If you’re looking for a full stack code generator, have a look at CF Gen.


Written by peterbell

December 3, 2009 at 12:29 pm

4 Responses

Subscribe to comments with RSS.

  1. Hey Peter,

    It looks like the download link on http://cfgen.riaforge.org is broken, along with the SVN link, http://svn.riaforge.org/cfgen/

    DW

    Dan Wilson

    December 4, 2009 at 6:06 am

  2. @Peter,

    Looks really interesting, and I look forward to checking it all out. Drop a line sometime.

    Steve 'Cutter' Blades

    December 4, 2009 at 8:44 am

  3. Perhaps I missed something, but the link to CF Gen doesn’t seem to have anywhere to download the code.

    Dustin Martin

    December 4, 2009 at 10:01 am

  4. Yeah – sorry guys. I’m doing a rewrite on CF Gen – I’ll post next Tuesday when it’s up and working.

    peterbell

    December 4, 2009 at 12:00 pm


Leave a Reply