• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Loading a Template Externally

#1
In my CakePHP application I would like to load the header template, with XenForo taking the current session into account. The template looks like this:

Code:
<xen:hook name="header">
<div id="header">
    <xen:include template="logo_block" />
    <xen:include template="navigation" />
    <xen:if is="{$canSearch}"><xen:include template="search_bar" /></xen:if>
</div>
</xen:hook>
What XenForo files should I include() with PHP? Currently I've experimented with including the Autoloader and initializing the application. Since there's no developer documentation available, I figure my best bet is to count on your knowledge.

What I've got:
Code:
        require_once($fileDir . '/library/XenForo/Autoloader.php');
        $xf = XenForo_Autoloader::getInstance();
        $xf->setupAutoloader($fileDir . '/library');
     
        XenForo_Application::initialize($fileDir . '/library', $fileDir);
What method/class should I invoke in order to load a specific template into my CakePHP application? Is there a hook I should call? (the "header" hook?)
 

xf_phantom

Well-known member
#2
PHP:
....
XenForo_Application::initialize($fileDir . '/library', $fileDir);

$dependencies = new XenForo_Dependencies_Public();
$dependencies->preLoadData();

XenForo_Template_Public::setStyleId(1);
XenForo_Template_Public::setLanguageId(1);


$template = new XenForo_Template_Public('header',$variables);

echo $template->render();
 
Last edited:
#3
Out of my head:
PHP:
$xf = XenForo_Autoloader::getInstance();
$xf->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);

$dependencies = new XenForo_Dependencies_Public();
$dependencies->preLoadData();

$template = $dependencies->createTemplateObject('header.css');
echo $template->render();
Thanks for your reply phantom!

This seems to be in the very right direction; since I don't need the CSS but the actual template, I've changed the createTemplateObject('header.css') to createTemplateObject('header'), assuming that'd be the right thing.
But doing so will echo nothing on my page. Even when I add an echo 'Hello World!'; before the code snippet, it shows me nothing. I suspect it has something to do with an ob_start(); not being closed somewhere (?).
Would you know more about this? It's good to note that when calling 'header.css' it actually shows me the css, but does not show any other echo statements I add:



the above is produced by the following code:

PHP:
echo '<div style="min-height: 300px; color: red;">'; // Not echo'd

    $xf = XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
    XenForo_Application::initialize($fileDir . '/library', $fileDir);

    $dependencies = new XenForo_Dependencies_Public();
    $dependencies->preLoadData();
    $template = $dependencies->createTemplateObject('header.css');

    echo 'This is before the rendered output <br/><br/>'; // Echo'd fine

    echo $template->render(); // Echo'd fine

    echo '<br/><br/> This is after the rendered output'; // Echo'd fine


echo '</div>'; // Echo'd fine
After debugging it appears that everything echo'd before XenForo_Application::initialize() is not shown. Commenting your code-snippet yields me this:


Maybe to render the header it needs a second parameter?

Update #1: I've updated the code snippet + the screenshot, it seems some of the echo statements AFTER the code snippet are actually printed, how odd...?
Update #2: After debugging it appears that everything echo'd before XenForo_Application::initialize() is not shown.
 
Last edited:

xf_phantom

Well-known member
#4
PHP:
<!DOCTYPE html>
<html>
<head>
  <title>test</title>

</head>
<body>
<h1>some content</h1>
<?php
$startTime = microtime(true);
$fileDir = dirname(__FILE__);

require_once($fileDir . '/library/XenForo/Autoloader.php');
$xf = XenForo_Autoloader::getInstance();
$xf->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir, true, array('resetOutputBuffering' => false));

$dependencies = new XenForo_Dependencies_Public();
$dependencies->preLoadData();

XenForo_Template_Public::setStyleId(1);
XenForo_Template_Public::setLanguageId(1);

$params = $dependencies->getEffectiveContainerParams(array(), new Zend_Controller_Request_Http());
$template =$dependencies->createTemplateObject('header', $params);

echo $template->render();
?>

</body>
</html>
There's an easier way, but don't have access to my projects code ATM :/
 

xf_phantom

Well-known member
#5
After debugging it appears that everything echo'd before XenForo_Application::initialize() is not shown. Commenting your code-snippet yields me this:


Update #1: I've updated the code snippet + the screenshot, it seems some of the echo statements AFTER the code snippet are actually printed, how odd...?
Update #2: After debugging it appears that everything echo'd before XenForo_Application::initialize() is not shown.
if something is cut off, it's http://php.net/manual/en/ref.outcontrol.php most of the time;)

Soooo, start debugging, and check what's happending inside XenForo_Application::initialize
thats why i had to add resetOutputBuffering ;)

hope this helps
 
#6
That's already a huge progression in my quest to solving this matter, @xf_phantom cheers!
I realized I needed the "navigation_visitor_tab" template, seeing as how I want to display the notifications to my users on my CakePHP application.
The HTML rendered through this method, however, does not seem to contain the user's information as seen in the graphic below:



Which should look something like (snapshot taken from XenForo app):


When the user's data is in here as well, my goal has been reached. Do you happen to know how one makes the render() function look at the current active session, and include that data?
 

xf_phantom

Well-known member
#7
then you should start the session and also include all the other data
PHP:
<!DOCTYPE html>
<html>
<head>
  <title>test</title>

</head>
<body>
<h1>some content</h1>
<?php
$startTime = microtime(true);
$fileDir = dirname(__FILE__);

require_once($fileDir . '/library/XenForo/Autoloader.php');
$xf = XenForo_Autoloader::getInstance();
$xf->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir, true, array('resetOutputBuffering' => false));

$dependencies = new XenForo_Dependencies_Public();
$dependencies->preLoadData();

XenForo_Template_Public::setStyleId(1);
XenForo_Template_Public::setLanguageId(1);

XenForo_Session::startPublicSession();


$dependencies->preRenderView();
$params = $dependencies->getEffectiveContainerParams(array(),new Zend_Controller_Request_Http());
$template =$dependencies->createTemplateObject('navigation', $params);

echo $template->render();
?>

</body>
</html>