A class similar to
MGffTemplate is used in IDLdoc to produce all its output. When I changed from a bunch of print statements scattered over various methods of a class to templates where all the output is in a template file, it was a <em>lot</em> easier to focus on the content of the output. If you need to generate text-based reports (HTML, XML, LaTeX, DocBook, etc.) from IDL, I would suggest using this class.
Let’s take a look at a simple example. In this example,
mg_template_example.pro (docs) will use the template file
image-file.tt to produce this output (source code of output). The code in
mg_template_example queries an image file and gets a structure,
filename = filepath('people.jpg', subdir=['examples', 'data']) result = query_image(filename, info)
Then it simply creates a template from the template file, passes the
info structure and the name of output file to the
process method, and frees the template object.
otemplate = obj_new('MGffTemplate', 'image-file.tt') otemplate->process, info, 'image.html' obj_destroy, otemplate
This is easy, the real work for doing output is now in the template file. The template file is just a text file, most of which will be copied verbatim into the output. But there are directives enclosed in
%] that pass commands to the template object. Output in the directives will be processed in some way (depending on the directive) and then included in the output. The simplest directive simply inserts a variable into the output. For example,
Number of bands[% channels %]
channels was a field of the
info structure passed into the
process method. Also, any IDL expression that returns a string (or can be converted to a string) can be used, like
Dimensions[% strjoin(strtrim(dimensions, 2), ', ') %]
dimensions was a field of the
There are other directives for
IF statements, and several ways to include other files. More on these features later; I hoping to create a more complicated example that uses some of these features soon.