How to use a Zend Layout as WordPress Theme?

I have written a Zend application and have included WordPress for blogging. When I first installed WordPress I themed it so that it used the same header etc as the main application. I have since redone the main theme twice and had to redo the WordPress theme to match. Is there a way for WordPress to use my Zend layout? My first thoughts are to break up my layout into header/footer files and include them from WordPress using the full paths. Although it would work, it is far from ideal (I would prefer to keep the layout file in one piece).

Related posts

Leave a Reply

4 comments

  1. If you have a page class that the layout/view script uses or similar you could do something like this in the wordpress theme file:

    $page = new Page;
    $page->setTitle(get_the_title());
    
    $content = '';
    
    if (have_posts())
    {
       while (have_posts())
       {
           ob_start();
           the_post();
           $content .= ob_get_clean();
       }
    }
    
    $page->setContent($content);
    ...
    
    $view = new Zend_View();
    $view->page = $page;
    ...
    

    WordPress doesn’t make it easy with its functions that output instead of returning, hence the ob_start().

    I don’t know if this is the best way though, I’d be interested to see if there is a better way.

  2. I have separated my layout.phtml file into header and footer partials.

    In my WordPress theme files I am including my partials.

    A more elegant solution could be to write my own get_header/footer function that include these partials? I think this would go in the theme’s function.php file?

  3. For many websites you may need to install WordPress for blogging integration. When it comes to skinning the blog you are pretty much limited to copying the html from your Zend_Layout into the WordPress header.php and footer.php files. This is duplication, and if you make any changes to your layout, you will need to update the blog theme as well. Well, there is another way!

    Alter Your Zend Application

    Make a separate bootstrap file for your Zend application (for example by following this guide: Access Zend Application Resouces from Other Applications).

    <?php
    // Define path to application directory
    defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
    
    // Define application environment
    defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
    
    // Ensure library/ is on include_path
    set_include_path(implode(PATH_SEPARATOR, array(
            realpath(APPLICATION_PATH . '/../library'),
            get_include_path(),
        )));
    
    /** Zend_Application */
    require_once 'Zend/Application.php';
    
    // Create application, bootstrap, and run
    $application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
    );
    $application->bootstrap();
    

    In your index.php file, to run your application:

    include('bootstrap.php'); //where bootstrap.php is the file of the new bootstrap file
    $application->run();
    

    WordPress Magic

    Now comes the WordPress magic.

    Get Zend Layout

    In your new WordPress theme folder, create a file named ‘Zend_Layout.php’ and copy/paste this code into it:

    <?php
    //load Zend_Layout
    $layout = new Zend_Layout();
    
    //add the blog's stylesheet to the header
    $view = $layout->getView();
    $view->headLink()->appendStylesheet(get_bloginfo( 'stylesheet_url' ));
    
    // Set a layout script path:
    $layout->setLayoutPath(APPLICATION_PATH . "/modules/default/views/scripts");
    $layout = $layout->render();
    

    Header

    Change the header.php file to:

    <?php
    include('zend_layout.php');
    echo substr($layout, 0, strpos($layout, '<div id="container">'));
    ?>
        <div id="container">
    

    This will load the $layout variable from the previous script and echo everything up to your main container div.
    Footer

    The footer.php is similar:

        </div><!-- #main -->
    <?php
    
    include('zend_layout.php');
    echo substr($layout, strpos($layout, '<div id="footer">'));
    
  4. You just need passing view object vars (model from WordPress) to your Layout.

    This is a sort of example of how I use it in my projects (I use full Zend_Layout: headLink, headTitle, headStyle…), but the point 1 is the mixed example:

    <?php
    // Layout: New; or get from registry if it's stored in; 
    // or set your own new Layout with ->setLayout().
    $Layout = new Zend_Layout();
    $view = $Layout->getView(); ?>
    
    <!-- Header -->
    <?php echo $Layout->render('header.phtml'); ?>
    <!-- /Header -->
    
    <?php // 1. Now assume you have an usual WordPress loop: 
    // (but IMHO I prefer use $wp_query->posts and partialLoop)
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            $view->post = get_post(get_the_ID());
            $view->render('partials/article.phtml');
            // article.phtml works with $this->post as model.
        }
    }
    
    // 2. Example out of loop with $wp_query:
    if ( $wp_query instanceof WP_Query && $wp_query->have_posts() ) {
        // Now, we can render your partial article template 
        // passing the posts as model:
        $view->partialLoop()->setObjectKey('post'); // $this->post in article partial template.
        $view->posts        = $view->partialLoop(   'partials/article.phtml', 
                                $wp_query->posts);
        // You can render here your $view->posts var or passing anything.
    }
    ?>
    
    <!-- Sidebar -->
    <?php echo $Layout->render('sidebar.phtml');
    // or call your WordPress sidebars: get_sidebar('my_sidebar'); 
    // but, again, I prefer use placeholders for that. ?>
    <!-- /Sidebar -->
    
    <!-- Footer -->
    <?php echo $Layout->render('footer.phtml'); ?>
    <!-- /Footer -->
    

    Hope it helps 😉