![]() | Chapter 25: Extensions | ![]() ![]() |
25.25. The template layer |
When Go is clicked, Inform translates the I7 source text into I6 code, but the directly translated code could not survive on its own: it needs a large body of supporting code, also written in I6, to sustain it. (Just as a program like iTunes or Firefox cannot run on a bare machine, but needs an operating system already up and running to support it.) Until 2008, this supporting code was provided by the I6 library, that is, the standard distribution of useful I6 code supplied with distributions of Inform 6.
However, the supporting code is now generated from a collection of about 35 "segments" of I6 code which together make up "the template layer". The reason for the term "template" is that the segments are not quite directly copied into I7's output, but instead act as templates from which I7 generates code - the final output contains variations according to what the original source text needs. (For instance, if the original source text never uses indexed text, lists or stored actions, then the code and arrays needed to maintain the memory heap are omitted, making for a smaller final story file which will run in smaller interpreters.)
Each segment has its own name, which looks like a leafname plus the ".i6t" filename extension (which stands for "I6 template"). Internally, a segment is itself divided up into named parts. For instance, the segment "Relations.i6t" contains a part called "Symmetric One To One Relations" which provides I6 routines for changing and testing such relations. There are more than 600 named parts across the template as a whole; it is quite a large program. An annotated, typeset version of the template - amounting to a roughly 500-page book - is available for download from the Inform website.
The most powerful use of "Include" allows code to included before, instead of or after any named part or segment in the template. For example:
Include (- ... -) before "Relations.i6t".
Include (- ... -) instead of "Relations.i6t".
Include (- ... -) after "Symmetric One To One Relations" in "Relations.i6t".
Multiple such inclusions can be made for the same segment or part. If so, all will take effect in the case of "before" or "after", but for "instead of" only the most recent one takes effect. Inclusions requested before, or after, a segment or part which has been replaced with "instead of" will take effect and appear before or after the code which appears instead of it.
The pre-2008 syntax
Include (- ... -) before the library.
has been withdrawn; the new syntax
Include (- ... -) after "Definitions.i6t".
should have the same effect.
Template files are not written in literal I6, but in a marked-up, annotated form of I6 which has special transcription commands embedded into it. These commands should absolutely not be used except in the built-in template files, with one exception:
{-segment:Flowers.i6t}
places the whole of the template file "Flowers.i6t" in this position. The built-in template does not of course contain "Flowers.i6t", but Inform allows the optional "I6T" subfolder of the "Materials" folder of a project to hold additional or replacement template files. Thus the project "Botanic Gardens.inform" might store:
Botanic Gardens Materials/I6T/Flowers.i6t
It could even contain:
Botanic Gardens Materials/I6T/Relations.i6t
in which case this would automatically be used instead of the built-in copy of "Relations.i6t", without any change to the I7 source text being needed. In this way, projects can (if they need to) use partly or entirely customised templates.
One application of this might allow for chunks of I6 code generated by external utilities - Perl scripts, lex and yacc, or other code generators - by compiling those to suitable template files in Materials/I6T and then using an inclusion like
Include (- {-segment:MyStuff.i6t} -).
in the I7 source text.
Template hacking, as it's called, is a last resort. If there is any way to achieve the same ends by writing ordinary I7 source text, then that will always be better. If it is possible to write "Include (- ... -)" without mentioning any segment or part, that's much to be preferred, because it has more chance of continuing to work into the future when the template might have been rewritten.
Previous | Contents | Next |