views:

529

answers:

4

Is there an existing good example, or how should one approach creating a basic Template system (thinking MVC) that supports "Template Inheritance" in PHP5?

For an example of what I define as Template Inheritance, refer to the Django (a Python framework for web development) Templates documentation: http://docs.djangoproject.com/en/dev/topics/templates/#id1

I especially like the idea of PHP itself being the "template language", though it's not necessarily a requirement.

If listing existing solutions that implement "Template Inheritance", please try to form answers as individual systems, for the benefit of 'popular vote'.

+2  A: 

The PHP language itself was designed for just these tasks, [unfortunately] it became capable of more and more over time and people who had started out with that simple syntax started implementing more and more complex tasks with that strangely evolved language.

I think what you mean with template inheritance is best expressed as dynamic file inclusion in PHP, as simple as

<? require $content ?>
soulmerge
Using this solution is possible because all variables defined in a parent (or "controller") PHP script are available "globally" to all includes, and files included by those includes as well, correct?
anonymous coward
That is correct, the scope stays the same if you include a file. You can even access $this if you include a file from within a class method.
soulmerge
A: 

Assuming you are familiar with how class inheritance works, your answer is YES, PHP supports it. The django thing might be overkill, but I'll try and fill you in real quick on how to do it anyways.

Note: I'm not going into using a controller here. Obviously if the page is a blog, you are going to create a blogPage object rather than just a regular Page. ALSO, I wrote this up from scratch for you, so no guarantees on it working.. but hopefully it will give you some ideas.

<?php
class Page
{

    protected $content_main; // Your main page content.
    protected $content_leftbar; // Your left sidebar content.
    protected $content_title; // Your content title.
    protected $template; // Template data.

    Function getTemplate() {
     // Logic for determining the template to be used in here.
     // Let's say it returns the location of a cached version of the template.
            return $template_file_path;
    }

    Function populateContentBlocks() {
     // This populates the $content_BLOCK variables with data using some default
     // logic you have for determining where to grab that data from.
    }

    Function loadPage() {

     // Populates variables.
     $this->populateContentBlocks();

            // Fetches template
     include( $this->getTemplate() );
    }


} // END class


Class blogPage extends Page {
    Function getTemplate() {
     // Logic for determining the template to be used in here.
     // Let's say it returns the location of a cached version of the template.
     // OVERRIDE THE DEFAULT TEMPLATE LOGIC OF THE PAGE WITH WHAT IS RELEVENT TO
                // BLOGPAGE.
    }
}
?>

Template File Example:

<html>
  <head>
    <title><?php echo $this->content_title; ?></title>
  </head> 

  <body>
      <div class="sidebar"><?php echo $this->content_sidebar; ?></div>
      <div class="mainContent"><?php echo $this->content_main; ?></div>
  </body>
</html>
Joe Bubna
Yeah, loadpage should just be loadPage() { $this->populateBlocks(); include $this->templateFile; }
jmucchiello
Doh, too much c++, I haven't been doing php for a while. Thanks for the reminder.
Joe Bubna
+1  A: 

Hi,

Please do as soulmerge suggests.

BTW, template inheritance is actually reversed dynamic inclusion, the way i see (the pain of) it: is not about controlling which file to include in a template, it is about controlling which file should this template be included into + which parts of that file to overload.

If you really want gory details about template inheritance:

  • why it sucks,
  • how to implement it anyway (ie. you want to defy intelligence and experience),
  • how to suck less with it when you have to use it,

Again, please, pretty please, pretty please with a sugar on the top, sexy please with a sugar on the top and a strawberry: do not do this if you intend to open source templates (ie. as part of something ment to be reusable).

jpic
This should be a comment on the original post/question.
anonymous coward
Requires 50 points.
jpic
And the link in the question links to a mail i wrote about implementation of django's template inheritance with any template langage :)
jpic
Anyway, did you figure what template inheritance was about and how to implement it? cause if you didn't then maybe you didn't go through the answer correctly :)
jpic
A: 

Hello! Sorry to dig up an old thread, but I just released a library that does template inheritance in native PHP. Very inspired by Django. Please check it out and let me know what you think:

"PHP Template Inheritance"

http://phpti.com/

arshaw