Using Silverstripe Templates outside Silverstripe

In a recent website-project i had a WordPress Blog running next to the main CMS Silverstripe, handling the Blog-part of the site.

Integrating the Blog in Silverstripe (which indeed would have made things simpler) was not  an option at the time. The usage of loads of WordPress plugins would’ve made a rewrite a major task, which was out of the budget.

The blog was integrated in the same page layout as the rest of the website. So ideally it would at least share the same Templates for header, footer etc. and would integrate the sites navigation built by Silverstripe.

But how to use the pre-rendered Silverstripe template .ss files in a WordPress theme?

The following worked fine for me, using Silverstripe 3.0:

In the functions.php of the wordpress theme i added this code to init a silverstripe controller:

//init silverstripe
require_once '[path-to-silverstripe-root-folder]/framework/core/Core.php';
require_once '[path-to-silverstripe-root-folder]/framework/model/DB.php';
//init the Database connection
DB::connect($databaseConfig);
//fetch some page, it that case i used the 404 ErrorPage
$SSPage = DataObject::get_by_id('Page', 4);
//init a the controller and cache it as a global variable
$SSctrl = new ContentController($SSPage);
//add the controller to the stack, so it becomes available as the current controller for the template
$SSctrl->pushCurrent();

and also some helper functions to get the actual templates

function get_Silverstripe_Header()
{
    global $SSctrl;
    //render the controller with the includes/Header.ss template
    return $SSctrl->renderWith('Header');
}

function get_Silverstripe_Footer()
{
    global $SSctrl;
    //..or any other template, like the footer
    return $SSctrl->renderWith('Footer');
}

…and then just use these in the templates:

<?php echo get_Silverstripe_Header() ?>

Of course this is no way limited to WordPress. It just an idea how to access Silverstripe Templates outside of Silverstripe code.