5.5.1. Compiler overview

An instruction is an exact template compiler plugin, so first we should know, how it works. The OPT compiler changes the template source into a PHP code, which is stored in the compilation cache directory. The inspiration for it was the XML parser, which generates a tree of nodes, and allows to process it by our application. OPT also generates such tree, but it touches only the OPT tags and keeps away of any other XML or HTML-style data. This means you may use the compiler for every text content you are able to invent. Moreover, there are some differences between the OPT and XML connected with the alternative tags and the parameter handling.

Let's consider the following example:

Example 5.3. A sample template

static text
{tag}
	static text enclosed between the tags and the block: {$block} .
{tagelse}
	alternative static text
{/tag}
static text

The question is, what tree is generated from this simple template.

The blue elements are simply called nodes, but they contain yellow blocks. As you can see, blocks are used to implement the alternative tags. Moreover, they physically represent every tag in the template, whereas the nodes are a skeleton of this structure. Depending on the node type, the compiler selects the processor to process the node, and the processor decides, how to process the blocks. To sum up: the compilation algorithm is very simple. The compiler generates the tree, and runs the generic processor on the root node. The processor generates the PHP code for all the block, and if they contain some lower-level nodes, runs another processors for them, that generate more and more code. As a result, we get a fully compiled template.

There are seven types of nodes in OPT:

  1. Root node (OPT_ROOT) - the base of every tree. Its task is to run lower-level nodes.
  2. Text node (OPT_TEXT) - represents static text. It does not contain blocks.
  3. Instruction node (OPT_INSTRUCTION) - represents an instruction. The blocks inside represent the tags to process and their content.
  4. Expression node (OPT_EXPRESSION) - represents an expression. They always contain one block with the expression source as an attribute.
  5. Component node (OPT_COMPONENT) - represents a component. It is parsed by the component processor.
  6. Unknown node (OPT_UNKNOWN) - generated, if OPT does not know, how to process the tag. However, it is included in the tree, because maybe some instructions will know, what to do with it. If not, its content and children are lost - that is why OPT does not report the mistakes in the instruction names.
  7. Attribute node (OPT_ATTRIBUTE) - represents the OPT attribute. It contains one block with the attribute value.

There are five types of blocks in OPT:

  1. OPT_MASTER - a beginning tag: {tag}
  2. OPT_ALT - an alternative tag: {tagelse}
  3. OPT_ENDER - a closing tag: {/tag}
  4. OPT_COMMAND - a single tag: {tag /}
  5. OPT_ATTRIBUTE - an OPT attribute: opt:attribute="value"

The instruction processor gives hints, what instructions and attributes it can process, so that they could be redirected there by the compiler. The processor generates a PHP code then. The hints are very exact, so the compiler does not have to worry so much about the syntax. It is even possible to handle the {/tag} as the beginning tag, if the processor wants it.

The blocks provide also an attribute string, which can be splitted into the instruction parameters. To improve this, OPT provides a built-in parameter parser, which must be only configured.