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

Loading a Template Externally

Discussion in 'XenForo Development Discussions' started by Claiver, Aug 13, 2013.

  1. Claiver

    Claiver Member

    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?)
     
  2. xf_phantom

    xf_phantom Well-Known Member

    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: Aug 13, 2013
    Claiver likes this.
  3. Claiver

    Claiver Member

    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:

    [​IMG]

    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:
    [​IMG]

    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: Aug 13, 2013
  4. xf_phantom

    xf_phantom Well-Known Member

    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'$fileDirtrue, 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 :/
     
    Claiver likes this.
  5. xf_phantom

    xf_phantom Well-Known Member

    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. Claiver

    Claiver Member

    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:

    [​IMG]

    Which should look something like (snapshot taken from XenForo app):
    [​IMG]

    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?
     
  7. xf_phantom

    xf_phantom Well-Known Member

    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'$fileDirtrue, 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>




     
  8. Claiver

    Claiver Member

    Thanks a bunch @xf_phantom! This definitely got me started :) !
     
  9. Claiver

    Claiver Member

    @xf_phantom or to whom may know: Currently when it generates the HTML for the loaded templates, all of the URLs are relative. Since my board is located at http://mysite.com/community/, and my main page at http://mysite.com/, I need to somehow make the renderer/parser append "/community/" to the URLs. Any idea where I can change this setting?
     

Share This Page