CFTemplate: The core code
[UPDATE] Thanks to Brian Rinaldi who showed me what an enclosure was in BlogCFC - there is now a download link at the bottom of this entry as well as the (non obvious) link above!
CF Template is designed to generate and publish a single script file based on provided metadata and a provided CF Template. It just has a single public generateScript() method for generating a single script based on the provided metadata and template.
To use CF Template, you need to create a CF Template and get some metadata (a struct, a set of variables, a cfoutput, an xml file, an iterating business object or anything else you can come up with). Call the generateScript() method at it will (a) get the CF template based on the file path you provided, (b) transform the CF Template and metadata into the script you require, and (c) publish the script to the filepath you requested.
You need to provide CF Template with the file path to a scratchpad directory where it can publish transformed scripts for generation purposes (it creates the scripts using a UUID for the name and then deletes them after generation). It also needs an includepath to get to the scratchpad and the directory where you store your CF Templates. CF Template is designed to use LightWire, so if you don’t use LightWire you need to add an init() method with the following properties:
// CF Template variables.ScratchpadFilePath = "#application.DirectoryRoot#\scratchpad";
variables.ScratchpadIncludePath = "/#application.CFMapping#/scratchpad";
variables.CFTemplateFilePath = "/#application.DirectoryRoot#\cftemplate";
</cfscript>
Of course, if you do use lightwire, all you need is to put the properties above into your lightwire config file. You also need to add the pathing for cfTemplate to your LightWire config file. In mine it is:
variables.Object.Singleton.CFTemplate.Path = "lib";
</script>
And then to call cfTemplate, all you need to do is to create the singleton as follows:
Of course, if you are accessing cfTemplate from another LightWire created object, you don’t need to create cfTemplate at all – just add it to the list of dependencies for your object in the LightWire config file and it’ll be injected into the variables scope of your object automatically.
In my next posting I’ll extend the functionality of cfTemplate to handling the including of snippets which is really useful in practice and then I’ll start to wrap it with a generator.cfc that handles the orchestration of generation where you want to generate n-instances of m-different templates (such as when you’re generating an entire application). I’ll also post some sample cfts (CF Templates) along with their associated metadata to show how you’d use this in practice.





Sorry - it is pretty basic and I'm new to all this sharing code stuff. For now, the download link on this page gets you the single cfc that is cf template. I'll get this all packaged up and into svn on google code some time, but it may be a couple of weeks as I need to clean up LightWire and finish out my ORM base classes. I also need to add a couple more cfc's to cftemplate to make it more broadly useful. Let me know if you have any problems with the download.
This looks ready interesting. I have a problem imagining what a template should look like. Can you post an example please?
Sorry, got so caught up with LightBase haven't had a chance to play with this. Basically, just take your CF code you want to generate and wrap it with double << >> and ## for generation code.
Simple example. Lets say you would to create a cfset setting a property to a value based on a list of config properties in the database. You'd cfquery to get your data and then the code would be:
<<cfoutput name="ConfigData">>
<cfset ##PropertyName## = "##PropertyValue##">
<</cfoutput>>
Now lets assume your data was:
PROPERTY VALUE
Datasource mydb
FeatureList feature1,feature2
ServerName server6
Then if you ran the generator it would generate a file that said:
<cfset DataSource = "mydb">
<cfset FeatureList = "feature1,feature2">
<cfset ServerName = "server6">
Pretty trivial example, but I hope that gives the idea?
Can't wait to take a look. I just wish my clients would stop calling ;)
Know what you mean about clients ;-<