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).
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.
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?
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">'));
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 ;)
精彩评论