Page 1 of 1

Unlimited levels of template nesting...

Posted: Sat Feb 03, 2007 10:12 am
by alex.barylski
Anyone who has ever used a template engine (Smarty, etc) will almost always do so because of the desire to keep a consistent master navigation/layout.

Knowing that, we can assume almost all site which use a template engine will have at least 2 templates:

1) Master template(s)
2) Page template(s)

When an application gets complicated one might even modularize their presentations into three or more layers - sub page templates.

Assuming we are using a MVC architecture (Zend) and we initialize the master template inside the front controller and inject each controllers(view) output into the master template. How would you typically handle arbitrary level of template nesting?

Sample code, comments, disscussion, etc???

Posted: Sat Feb 03, 2007 12:37 pm
by Christopher
I don't usually do anything hierarchical in the template system. Template blocks, which are really just multiple templates in a single file, are as far as I go. For hierarchy For me templates are just for tag replacement.

I use a structure of View or Response objects for hierarchies. They allow me to attach either a string or an object with a render() method to a tag in a template.

Posted: Sat Feb 03, 2007 1:07 pm
by alex.barylski
I should have mentioned in my original message...I have learned to distinguish between two types of templates:

1) Push - A template with holes which are plugged by pushing in content
2) Pull - A template with includes, which are pulled into the template

These techniques are not mutually exclusive, both can be used in a single template.

By this definition, it sounds like your templates are simply static HTML pages with holes, so I assume your using a push system?
I use a structure of View or Response objects for hierarchies. They allow me to attach either a string or an object with a render() method to a tag in a template.
I'm not sure I totally follow...can you demonstrate with some psuedo-code?

Posted: Sat Feb 03, 2007 1:21 pm
by feyd
Sounds like arborint's is pretty similar to mine.

Basically how it works is I have a large tree of renderable objects. Each object understands how it is supposed to be represented via very simple templates each is given that wraps around the information they carry.

Posted: Sat Feb 03, 2007 2:20 pm
by alex.barylski
feyd wrote:Sounds like arborint's is pretty similar to mine.

Basically how it works is I have a large tree of renderable objects. Each object understands how it is supposed to be represented via very simple templates each is given that wraps around the information they carry.
I'm not sure if you guys use a similar method to my own, but...here is a simplified version of how I might use multiple templates:

Code: Select all

$smarty->assign('title', 'Master template TITLE');

switch($page){
  case 'home':
    $buff = $smarty->fetch('home.tpl');
    break;
  case 'about';
    $buff = $smarty->fetch('about.tpl');
    break;
}

$smarty->assign('page', $buff);
echo $smarty->fetch('master.tpl');
See the workflow or process? You render inner most templates first and pass their content into the parent/wrapping template which continues until you reach the master template?

Is this essentially the technique you use - ignoring the specifics of a switch above or a front controller, etc...

If you use Zend and it's FC implementation...how do you specifically pass content from a controller buffer to eventually interpolated into the master template inside the FC???