MFWork Template System
Other template systems
To explain one of the most important diferences between the Mason Framework template system and every single other template system I know, first I need to explain something I like to call directional processing.
Most template systems work one way only. Some, like Mason or Smarty work outside-in, and other, like catalyst do it inside-out. What does this mean?
Outside-in systems
This is, probably, the most common behaviour on template engines that can also process data. They start by processing a inicial template (or component), and that template execute other components to complete parts of it's own result.The main advantage of this system is that a page only processes the data it needs to be completed. It's main disadvantage is that and inside template/component can't change the content in the outside.
Inside-out systems
Inside-out systems are those where every single component is processed before the template that contain them. This is most usual in systems where the templates don't have what it takes to get the data, like Catalyst.The main advantage of this type of system is that every single component can set data on the main template. Their main disadvantage is that every single component is processed, even if they are not used in the template.
the MFWork System
With the Mason Framework I wanted a system that had the advantages of Outside-In solution, and the flexibility of Inside-Out.The main reason I wanted this was to have the hability to set template variables within the deepest module of a page. In this site, for example, that hability is used to set the title of every page from the page module, that is responsable only by the content of the content of the page.
How it works
How did I accomplished this? Let's see a small example.When the framework starts processing a page, It goes to the url2template table and finds out which is the main template to use to the current URL. Then we gets that template, lets assume:
<html> <head> <title><?=stitle?></title> </head> <body> <?main?> </body> </html>
Here we have to Mason Framework specific tags. The first is <?=stitle?> and the second <?main?>. The first one is a variable placeholder, and the second is a module placeholder.
So, how do we do our "Outside-In-Backout" template processing?
First we process the main module placeholder. Then we process anyother module placeholder, and only after that we will process all the variable placeholders.Why do we start with the main placeholder? Usually main is handled by the main core module. This module replaces its place with some template with a new placeholder, and sets two data variables. This module is called first so other modules can use this values.
The same way that main sets mainbaseuri and mainplaceholder, any module can set variables, that can later se used in the templates.
How it works?
The template engine looks for module placeholders (<?placeholdername?>), find out which module should be called to complete that place, execute it, and replace every ocurrence of <?placeholdername?> by the content that module generated and look for new module placeholders.When no more module placeholders are found the template engine look for variable placeholders (<?=variablename?>), and replace them with the value of that variable.
Where do the variable values come from?
Every variable present in a template comes from a internal data hash, that can be set using the Base Site Data and the Site Data in the administration menu, or using the $mf->set_data() call from within a module, or with the data or add_data parameters in the $mf->template() calls (see MFWork API for details on the Framework api).There is no support to to complex data structures, only scalars. And there are no intent of including it in the near future.