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

Can you do a PHP Include in a template?

Discussion in 'XenForo Development Discussions' started by ZippySLC, Nov 5, 2010.

  1. ZippySLC

    ZippySLC Member

    Hey -

    So I have a common header design that I use for my forums, CMS, gallery - whatever. To make my life simple I've made the header design in an external PHP file that whatever software that I am using can just include it.

    Is it possible to do a PHP include inside xenForo's template system? I know with a certain other forum software you had to basically write a custom product to do it. This is all I am looking to accomplish in the header template:

    Code:
    <div id="header">
    
    <?php $a = file_get_contents("http://url.to.file/navigation.php");
    echo ($a); ?>
            <xen:include template="logo_block" />
    	<xen:include template="navigation" />
    	<xen:if is="{$canSearch}"><xen:include template="search_bar" /></xen:if>
    </div>
    
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  3. ZippySLC

    ZippySLC Member

    Ahh, many thanks! Looks to be just what I needed!
     
  4. ZippySLC

    ZippySLC Member

    Argh, not working for me.

    So, I've made a new addon: "PHP Include for Header." I have a code event called "phpincludeforheader" and then a listener named the same thing. The parameters for the listener are:

    XenForo_Pages_Banner::includeFile

    The php file (Banner.php) in the library/XenForo/Pages directory looks like this:

    Code:
    <?php
    class XenForo_Pages_Banner
    {
         public static function includeFile(XenForo_ControllerPublic_Abstract  $controller, XenForo_ControllerResponse_Abstract &$response)
        {
         	ob_start();
            require('/var/www/html/common.njpinebarrens.com/navigation-new.php');
            $myContent = ob_get_contents();
            ob_end_clean();
    
            $params = array(
                'myContent'  => $myContent
            );
    
            $response->params = array_merge(
                $response->params,
                $params
            );
        }
    }
    
    and the template looks like this:

    Code:
    <div id="header">
    
    {xen:raw $myContent}
    
            <xen:include template="logo_block" />
    	<xen:include template="navigation" />
    	<xen:if is="{$canSearch}"><xen:include template="search_bar" /></xen:if>
    </div>
    
    What am I doing wrong?
     
  5. Nasr

    Nasr Well-Known Member

    did u close ur php tag

    ?>
     
  6. ZippySLC

    ZippySLC Member

    No, but I just did and it didn't make a difference.
     
  7. Shadab

    Shadab Well-Known Member

    You have to hook into an existing code event, instead of creating your own. The most appropriate event, I think, would be container_public_params. Take a look at this thread: How to access container_public_params in templates?

    Once you confirm that your dummy text is indeed being printed in the header, replace the text with your actual call to fetch the external file; for example:
    PHP:
    $params['customHeader'] = file_get_contents('http://url.to.file/navigation.php');

    /Slightly OT

    Majority of the time you wouldn't need to create your own code event. Hooking into existing code events provided by XenForo is all that's needed. The only time you would need to create a code event is when you are authoring a rather complex modification and you want to give other modification authors the ability to extend some functionality that is off-limits to existing code events. :)
     
    Umit and Jake Bunce like this.
  8. Dean

    Dean Well-Known Member

    It has been suggested that the developer questions forum might be a better place for this thread, and I agree. I will move it over there, please let me know if there is any problem with that.

    general support -> Development Questions.
     
  9. ZippySLC

    ZippySLC Member

    Alright, that seems to be what I need. But the problem is that I'm not a vB/Xen/PHP developer, so I'm a bit lost on how to get this all working right. Can someone give me some baby steps? I just have no background in writing add-ons.
     
  10. ragtek

    ragtek Guest

    That's not necesseray, if there's nothing else in the php file
     
    Kier and twhiting9275 like this.
  11. Vladislav Rastrusny

    Vladislav Rastrusny Active Member

    I think URL wrapper here is quite slow. It's better to use ob_XXX functions to get output from the file. Remember also, that return() on the top level on the included file ends execution and after it include() or require() returns the content, that was in that return() statement.

    It's better to leave it open in all cases. Otherwise you can get those spaces after it accidentally go into output damaging it.
     
  12. gordy

    gordy Well-Known Member

    Can anyone better explain what this means? I'm having the same dilemma, I have a site-wide php header file with nav links,etc that I need to include via php. This include file needs to be at the very top of everything as it includes the logo et al.

    A code sample maybe?

    Nothing is more annoying to have a website where everything is consistent and then you click on the forums link and the layout bounces around due to CSS differences and templating.

    I want my forums integrated into my site, not my site integrated into my forums. :)
     
  13. Vladislav Rastrusny

    Vladislav Rastrusny Active Member

    Well, using an url wrapper like this
    Code:
    file_get_contents('http://url.to.file/navigation.php')
    requires http request to server like is done by regular user. And this is quite slow and not performance-wise. If you need to catch an output of the PHP script on the same server, do like ob_start() says.
     
  14. gordy

    gordy Well-Known Member

    How would the syntax for using ob_start look? that's what I'm looking for ;) Like this?
    Code:
    ob_start('/some/path/to/file.php')
     
  15. Vladislav Rastrusny

    Vladislav Rastrusny Active Member

  16. gordy

    gordy Well-Known Member

    Dude, I'm not a coder and have no desire to be one, I just need results.

    Wouldn't output buffering have more overhead compared to an included file ?
     
  17. Vladislav Rastrusny

    Vladislav Rastrusny Active Member

    I understand, but I am not going to quote documentation here. I did all I could for you and don't tell me you are too lazy to just click a link and see an example for yourself.

    Yes, it would, unless included file is fetched via url wrapper. If you do something like
    PHP:
    include('/home/test.php');
    it will be very fast. But if you do something like
    PHP:
    include('http://example.com/test.php');
    it will be very slow let alone unsafe. But, in the latter case output buffering will not help you to speed up because http request will be issued in any case.

    Still, I don't understand what you are trying to achieve. Can you explain?
     
  18. gordy

    gordy Well-Known Member

    Oh,I read the link. However, how it is applied to xenforo and implementation is where the confusion is - it is common for people that code, to not comprehend how folks that don't code how to grasp things. The doc link to me is like how to construct a carburetor, but doesn't tell me how to install it into the car, much less a specific car.

    I understand that, including a URL is expensive in terms of performance, like taking the scenic route to a destination.

    I simply have this:

    --docroot/
    --docroot/src/header.php
    --docroot/fileA.php
    --docroot/fileB.php
    --docroot/fileC.php
    --docroot/dir/fileD.php
    --docroot/ (many more )
    on all the files, ( fileA.php fileB.php fileC.php fileD.php ) I have included the header.php to the top, after the </head> to have a consistent header (logo, nav links , etc)

    All I want is to include that same header.php to XF to match the rest of my site.
     
  19. Allpar Dave

    Allpar Dave Member

    I realize it is three years later, but I have a similar question and would like to ask if a different strategy is possible.

    I have a file which is created via php (essentially any news story posted in WordPress creates a file which includes both the latest three news articles, and the past four features which are not done in WordPress.)

    I was thinking that rather than saving it as a text file to be swept up into xenforo each time, that I could save the output directly into MySQL as a Xenforo template. That could be a bit more efficient, maybe?

    I am curious as to whether there are “gotchas” and “NO!! DON’T DO THAT!”s involved. I also noticed that there are “blobs” in the database which I can't see and which appear to be binary. Otherwise I'd think that maybe I could take an existing template and update it from outside (rebuilding would be another question!).
     
  20. Chris D

    Chris D XenForo Developer Staff Member

Share This Page