Template
Description
A very simplistic text-based template engine.
To use the template engine, do the following steps:
-
Write a template, which must be a text file. For this, define three tokens that will not interfere with the semantic of the text file format. These tokens will be used to mark template blocks and placeholders for values. The default tokens are:
-
'// BEGIN:'
Marks the beginning of a new template block (see BlockBegin), if the whitespace-trimmed line of the template text starts with this token. -
'// END:'
Marks the end of the current template block (see BlockEnd), if the whitespace-trimmed line of the template text starts with this token. -
'$#$'
Format pattern for placeholder values (see BlockValue).
Template blocks may be nested.
-
-
Create an instance of Template.
-
Configure the tokens (or use the default ones).
-
Call Initialize to start template processing.
-
Use Begin, End, Is, Rewind, RewindBegin, Skip, SkipRewind and SkipRewindBegin methods to iterate through the tree structure of the template blocks, using in-order traversal.
-
Use Value during traversal to specify block values.
-
Call End after processing all template blocks, to finish template processing.
-
Use Write to write the output file or object.ToString to get the output text.
-
To re-use the Template object for another processing cycle, jump to step 3.
Public / Constructors
File
Creates a Template instance for the given template file.
- IOException
-
If an I/O error has occurred.
Public / Methods
Begin
Moves to the next template block.
In the template text, the block begin token must be followed by a block declaration, which specifies the block name and its values, for example:
// BEGIN: my-block(a,b,c)
This will declare the block named 'my-block'
, having three values 'a'
, 'b'
and 'c'
. All values must be specified via Value. Any content on the same line that follows the block declaration is ignored.
- ValidatingException
-
If the template is malformed.
End
Finishes the current template block.
In the template text, the block end token must be followed by the block name:
// END: my-block
This will end the current block, which must be named 'my-block'
. Any content on the same line that follows the block name is ignored.
- ValidatingException
-
If the template is malformed.
- See also
Initialize
Initializes template processing.
The current values of BlockBegin, BlockEnd and BlockValue will be used for processing the template, subsequent changes will not be taken into account until template processing is initialized again.
An implicit block is created that contains the whole template and does not have any block values. This implicit block must be ended by calling End after all template blocks have been processed.
Rewind
Finishes the current template block and rewinds it.
Begin must be called next, passing the current block name, in order to continue template processing.
- ValidatingException
-
If the template is malformed.
- See also
RewindBegin
Finishes the current template block and rewinds it, leaving template processing in the state as if Begin had just been called.
- ValidatingException
-
If the template is malformed.
- See also
Skip
Cancels the current template block by skipping all remaining content.
- ValidatingException
-
If the template is malformed.
- See also
SkipRewind
Cancels the current template block by skipping all remaining content and rewinds it.
Begin must be called next, passing the current block name, in order to continue template processing.
- ValidatingException
-
If the template is malformed.
- See also
SkipRewindBegin
Cancels the current template block by skipping all remaining content and rewinds it, leaving template processing in the state as if Begin had just been called.
- ValidatingException
-
If the template is malformed.
- See also
Value
Specifies content for a block value placeholder.
Each template block has its own set of block values. Within a block, values defined in an ancestor block may be referenced.
- ValidatingException
-
If the template is malformed.
Public / Attributes
BlockBegin
The line token to use for the beginning of a new template block.
The default value is: '// BEGIN:'
BlockEnd
The line token to use for the end of the current template block.
The default value is: '// END:'