03 Template File - oldgreydog/CodeGenerator GitHub Wiki

Template files are obviously going to be the most complicated part of code generation. As you can see from the examples, they can be pretty hard to decipher on the first look (or fifth for that matter). But they don't have to start that way.

Most of my templates started off as hand-coded files that I had copied and altered at least two or three times to make other instances (database access classes being a perfect example). After the second or third time, I had tweaked and fixed enough that I had a reliable pattern that I could be confident that I wanted to templatize. Then I could make a new copy and start adding template tags, at each step running a generation to compare to the originals. That lets you build up the complexity instead of trying to create the template from scratch.

While the example templates are going to be your best resource for seeing how to use the various tags, I've tried to give some useful documentation for them in the javadocs included in the releases. In this wiki, I'll try to cover some higher-level concepts that don't necessarily fit in the javadocs.

Probably the most important one is the one required element for a template: the header record. It must be the first line in the template file. Here's the one in all of the examples:

%%HEADER%% openingDelimiter=<% closingDelimiter=%>

NOTE! - Unlike the other tags, the format of this line is strict! No spaces are allowed around the '=' and whatever is after the '=' is included in the delimiter, so quotes should not be used (unless you want them as part of the delimiters).

The header record lets you tell the parser what delimiters to look for in the rest of the template. Every tag needs an opening and closing delimiter so that the parser can separate them from other text in the template. But since one of my major goals for this generator was that it not be (computer) language specific, I had to have a way to specify delimiters that might need to be different for each language that a template might be written for. And it couldn't be limited to just one character each, or even two, because different characters or character sequences might be part of the target language. You have to be able to create delimiters that won't ever be in valid code. And the opening and closing delimiters don't have to be like each other in any way if you don't want, or if there are conflicts with the language.