Unlimited levels of template nesting...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Unlimited levels of template nesting...

Post 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???
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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???
Post Reply