Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-15-2007, 06:36 PM
Junior Member
 
Join Date: Sep 2007
Posts: 3
Default page layout

Hi all,

I have been woundering about something. Is it possible in zend to work with some sort of fix layout? With that I mean that the website that I have in mind has the same header and footer.

For the rest, there will be a left or right sub navigation bar (which will be filled dynamicaly according to some settings).

But the thing I would like to know if I can create a view file for the header and footer and just like "include" the rest of the contect views in it?

For exaple (sorry to bring up another programming framework, don't flame me about it, I'm just an eager web developer learning new thing) in Ruby on Rails you can have a reusable layout (verry often also the header and the footer) and then just "yield" your other views in it.

I have seen some solutions by just including the header and footer in every view page, but I don't know about that. That just seems a bit "over the edge" when using MVC.

I'm certain of it that other web developers here have had the same issue like me, so if you guys would mind sharing your thoughts with me.

Thank you in advance
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-07-2007, 06:06 AM
Junior Member
 
Join Date: Oct 2007
Posts: 2
Default

Yes you can. You will need the module which should be part of ZF 1.1.1. It's called Zend_Layout. Here is a link to an article: The basics of Zend_Layout (ahem, Xend_Layout)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-08-2007, 04:26 AM
creepy's Avatar
Junior Member
 
Join Date: Oct 2007
Posts: 1
Default

While waiting for their first release of zend_layout, I'm currently using some workaround. What I want for my layout is to be able to shift preferred layout per Module, Controller, or Action.


First I have created class Xin_Layout

PHP Code:
<?php
class Xin_Layout {
    
    const 
DEFAULT_PATH 'layouts';
    const 
DEFAULT_NAME 'default';
    
    protected 
$_hasLayout;
    
    protected 
$_name;
    
    protected 
$_path;
    
    public function 
__construct() {
        
$this->_path self::DEFAULT_PATH;
        
$this->_name self::DEFAULT_NAME;
        
$this->_hasLayout true;
    }
    
    public function 
disableLayout() {
        
$this->_hasLayout false;
    }
    
    public function 
enableLayout() {
        
$this->_hasLayout true;
    }
    
    public function 
hasLayout() {
        return 
$this->_hasLayout;
    }
    
    public function 
setName($name) {
        
$this->_name $name;
    }
    
    public function 
setPath($path) {
        
$this->_path $path;
    }
    
    public function 
getName() {
        return 
$this->_name;
    }
    
    public function 
getPath() {
        return 
$this->_path;
    }
    
}
?>
Then extend Zend_Controller_Action to use Xin_Layout


PHP Code:
<?php
require_once('Zend/Controller/Action.php');
require_once(
'Xin/Layout.php');

class 
Xin_Controller_Action extends Zend_Controller_Action {
    
    protected 
$_layout;
    
    public function 
__construct(Zend_Controller_Request_Abstract $requestZend_Controller_Response_Abstract $response, array $invokeArgs = array()) {
        
$this->_layout = new Xin_Layout();
        
parent::__construct($request$response$invokeArgs);
    }
    
    public function 
postDispatch() {
        
        if ( 
$this->_layout->hasLayout() ) {
            
            
$file $this->_request->getParam('controller') .'/'.
                
$this->_request->getParam('action').'.'.$this->viewSuffix;
                
                
            
$this->view->content_for_layout $this->view->render($file);
            
            
$layout $this->_layout->getName();
            
$path $this->_layout->getPath();
            
            
$this->renderScript($path.'/'.$layout.'.'.$this->viewSuffix);
            
        }
    }
}
?>

As you can see, layout path is based under controller base path.
Note that rendering was done under postDistpatch().


Sample Usage:

Directories:
File(s)

application-->controllers:
IndexController.php

application-->views-->scripts-->index:
index.pthml

application-->views-->scripts-->layouts:
default.phtml
customize.phtml

html:
index.php


application/controllers/IndexController.php file
PHP Code:
<?php
require_once 'Xin/Controller/Action.php';

class 
IndexController extends Xin_Controller_Action
{
    
// This will use default.phtml layout
    
public function indexAction() {        
        
$this->view->my_fruit "apple";
    }
}
?>
application/views/scripts/index/index.phtml file
PHP Code:
<h2>Index Controller Content</h2
application/views/scripts/layouts/default.phtml file
PHP Code:
<html>
<title>Default Layout</title>
<h1>HEADER</h1>
<body>
   <h2>Fruit = <?= $this->my_fruit ?></h2> 
   <?= $this->content_for_layout ?>
</body>
<h1>FOOTER</h1>
</html>
application/views/scripts/layouts/customize.phtml file
PHP Code:
<html>
<title>Custom Layout</title>
<h1>HEADER</h1>
<body>
   <h2>New Fruit = <?= $this->my_fruit ?></h2>
   <?= $his->content_for_layout ?>
</body>
<h1>FOOTER</h1>
</html>
html/index.php file
PHP Code:
<?php
require_once 'Zend/Controller/Front.php';

Zend_Controller_Front::run('../application/controllers');
?>
To use customize.phtml, your IndexController action index should be
Code:
public function indexAction() {
    $this->_layout->setName('customize');
    $this->view->my_fruit  = 'apple';
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-09-2007, 07:46 AM
Junior Member
 
Join Date: Sep 2007
Posts: 10
Default

Hi D@ Mick.

I had the same problem when using ZF. But I managed to get a solution: plugins. All you have to do is register some plugins. For example: a plugin that will show the header and footer; a plugin to show up a list of categories on the left; a plugin to show a dinamic calendar on the right etc.

The plugins offer enough flexibility for this.

If you have any further questions do not hesitate.

Adrian
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 01:55 AM.