Zend_View - layout like django

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Zend_View - layout like django

Post by Luke »

I have come to really like how django's template engine works and would like to try and implement it in PHP / Zend Framework. The way it works is you render a template that "extends" the base layout and then define each block inside your template. A typical template might look like this (if we were to pull it off in PHP):

Code: Select all

<?php $this->extends('base.html'); ?>

<?php $this->startBlock('head'); ?>

  <link rel="stylesheet" href="<?php echo $this->stylesheet; ?>" type="text/css" media="screen">

<?php $this->endBlock(); ?>

<?php $this->startBlock('content'); ?>

  <h2>Welcome to my page!</h2>

  <p>Mauris mi. Duis congue varius eros. Mauris odio erat, facilisis non, gravida nec, sollicitudin vitae, mi. Sed dapibus ipsum tempor lacus. Mauris dapibus rhoncus purus. Nam ut lorem et velit malesuada venenatis. Suspendisse tincidunt. Fusce posuere tortor sit amet est. Etiam vel nibh in lectus gravida sagittis. Praesent a magna at odio luctus vulputate.</p>

<?php $this->endBlock(); ?>
The base template would look something like...

Code: Select all

<?php echo $this->config->doctype; ?>


<html>

  <head>

    <meta http-equiv="Content-Type" content="<?php echo $this->config->content_type; ?>; charset=<?php echo $this->config->charset; ?>" />

    <title><?php if ($this->title) { echo $this->title; } else { echo $this->config->name; } ?></title>
    
    <link rel="stylesheet" href="<?php echo $this->MEDIA; ?>styles/screen.base.css" media="screen" type="text/css">
    
    <?php $this->startBlock('content'); ?> Default head stuff goes here <?php $this->endBlock(); ?>

  </head>

  <body>

    <h1><?php echo $this->config->name; ?></h1>
    
    <?php echo $this->render('menu.phtml'); ?>
    
    <?php $this->startBlock('content'); ?> Default content goes here <?php $this->endBlock(); ?>

  </body>
  
</html>
The problem is that I can't think of any way to do this. I've been racking my brain and trying things out... nothing seems to work. It seems like I'd need to implement some type of output buffering so that I could capture text in between startBlock and endBlock but even then... how would I know where to save it to? Anybody know how I could go about this? Impossible?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I think that simple Composite/Component attachment of renderers is the best overall solution. It is essentially hierarchical templates, but you can attach an object with a render() method at any point and it will work just like attaching a scalar. It is a little more work for simple things, but the complexity does not increase no matter how complex the hierarchy becomes. And it makes tooling simple.
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Ninja, check the Zend Framework Incubator on subversion - it probably will not be exactly like Django (or anything else) but Zend_View Enhanced/Zend_Layout was approved for development. I will refer you here just in case you're curious why I'm not actually committing any of it personally ;). Nothing I have proposed to ZF so far has come close to working out as planned.

The Block extract from Django will be implemented as a Placeholder helper in Zend_View. General placeholders for transporting data between templates and a parent Layout will also be made available. Look up the Zend_View Enhanced proposal for what else is incoming. I think most of the proposal text is still mine...:roll:.

If you have any comments at all about what's managed to make it to Incubator I can pass them on to Matthew.
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post by kyberfabrikken »

Syntactically different, but take a look at: http://www.tagarga.com/blok/makrell
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

arborint - I have seen systems like that... in fact it's pretty much how mine works as is.

Maugrim - thanks... I'll take a look

kyberfabrikken, that is exactly what I was looking to do. Like you said the syntax isn't the same... but I like it. I'll have to give it a try. Thank you very much.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Maugrim_The_Reaper wrote:I will refer you here just in case you're curious why I'm not actually committing any of it personally ;). Nothing I have proposed to ZF so far has come close to working out as planned.
As you know, I bowed out several months back after pushing a lot of ideas into the Core and MVC. The process is definitely not one of engagement or letting people develop their ideas -- and it shows. The View especially should have been redesigned and an adapter done for BC.

But then the problem might just be that we are a couple of bastards! ;)
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

LOL... you two are a good portion of the reason why I'm a half-way decent developer. Thanks.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

LOL! Maybe we are ;). Just a little.

I prefer to think the ZF Proposal Process is itself bastardized though.
The View especially should have been redesigned and an adapter done for BC.
Never going to happen. It's taken months just to get some simple changes added from ZVE; anything more than a few lines would be killed on the spot. You'd have to time travel to when the ZF adopted the Solar View class to stop it.

@Ninja: 5% portion? 10%? ;)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Maugrim_The_Reaper wrote:You'd have to time travel to when the ZF adopted the Solar View class to stop it.
Now where did I put that flux capacitor?
Maugrim_The_Reaper wrote:@Ninja: 5% portion? 10%? ;)
Ninja, once the Whipped/Shaving Cream was wiped of your head --- IT WAS ALL YOU BABY!!!!! ;)
(#10850)
Post Reply