Can you do a PHP Include in a template?

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>
 
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?
 
I have a code event called "phpincludeforheader" and then a listener named the same thing.
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. :)
 
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.
 
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.
 
PHP:
$params['customHeader'] = file_get_contents('http://url.to.file/navigation.php');
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.

did u close ur php tag

?>
It's better to leave it open in all cases. Otherwise you can get those spaces after it accidentally go into output damaging it.
 
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.

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. :)
 
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.
 
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.

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')
 
Dude, I'm not a coder and have no desire to be one, I just need results.
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.

Wouldn't output buffering have more overhead compared to an included file ?
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?
 
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.
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.

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.
I understand that, including a URL is expensive in terms of performance, like taking the scenic route to a destination.

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

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.
 
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!).
 
Top Bottom