SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Zend Framework and include files in views

    What path do you use for including common files in a ZF view?

    An example would be a logo that is going to go on every page. Is there an easy way to include a header.php file in the index.php for a given view? I can't get it to work at all.

    my application structure looks like:

    Code:
    /application
      /controllers
        IndexController.php
      /models
      /views
        /home
           index.php
        /includes
           header.php
    /library
      /Zend
    /www
      .htaccess
      index.php
      /images
    So in the views/home/index.php file, I want to include header.php and I'll be rendering that page from the IndexController indexAction.

    How do I accomplish that?

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At a guess (and loads left out..)

    PHP Code:
    <?php

    class someController
    {
        
    pageAction()
        {
            
    $view = new Zend_View;
            
    $indexController = new indexController;
            
    $view->header $indexController->indexAction(true);
            
    // blah blah
            
            
    $view->render('home/index.php');
        }
    }

    ?>
    (and change your indexAction($return = false) method to use: "if ($return) return $output;")

    or better yet.. redesign it so you are not dependant on a controller to create the header, but place the output generation separately.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I'm gonna run the risk of sounding stupid here...but I don't get your code at all.

    Here's my index controller:
    Code:
    <?php
                    
    require_once 'Zend/Controller/Action.php';
    
    class IndexController extends Zend_Controller_Action 
    {
        public function indexAction()
        {
            $view = Zend::registry('view');
    	echo $view->render('/home/index.php');
        }
    
        public function noRouteAction()
        {
            $this->_redirect('/');
        }
    }
    
    ?>
    Now, here's my index.php in the home directory:
    Code:
    <?php
    	include 'header.php';
    ?>
    <h4>Home Page</h4>
    
    <?php
    	include 'footer.php';
    ?>
    I had to add my include path (/application/controllers/views/includes) to my PHP path for the header/footer to resolve correctly, but it does work.

    Is this the correct technique to use when using the Zend framework?

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can change to relative includes.

    Going by your directory stucture, you can change the includes in index.php to:
    PHP Code:
    include '../includes/header.php'
    and like wise for footer

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by progrmr
    Is this the correct technique to use when using the Zend framework?
    Haven't toyed with Zend_View, but you should be able to call render() from within a template file, relative to your base path:
    PHP Code:
    <?=$this->render('header.php')?>
     ...
     <?=$this->render('footer.php')?>
    BTW, it would be nice to have a configurable template file extension which can be omitted when calling render().

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jenk
    you can change to relative includes.

    Going by your directory stucture, you can change the includes in index.php to:
    PHP Code:
    include '../includes/header.php'
    and like wise for footer
    That did not work. I thought it would too, but PHP couldn't find the file.

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by michel
    Haven't toyed with Zend_View, but you should be able to call render() from within a template file, relative to your base path:
    PHP Code:
    <?=$this->render('header.php')?>
     ...
     <?=$this->render('footer.php')?>
    BTW, it would be nice to have a configurable template file extension which can be omitted when calling render().
    This didn't work either. I think that in order for Zend_View to find it, I've got to set a script path. I'm going to mess around with it a bit, but I'll just do it the way I got it working before if I have to.

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by progrmr
    That did not work. I thought it would too, but PHP couldn't find the file.
    Change it to relative from your bootstrap file (i.e. your www/index.php file)

    PHP Code:
    include '../application/views/includes/header.php'
    Or to use the render method:

    PHP Code:
    echo $this->render('includes/header.php'); 

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Jenk -

    thanks! Very nice. Both of those worked. I opted for the latter, but again they both work.

    I swear I tried that first one! Oh well, live and learn.

    The Zend Framework is exactly what I was looking for in a PHP framework, so its nice to find solutions to these everyday type of problems.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •