Back to vBulletin 4 Articles

Add PHP to your Templates (Trick)
by TheAdminMarket 02 Oct 2014
Rating: (5 votes - 5.00 average)

First of all, let's make clear something. Yes, (unfortunatelly) vBulletin does not allows PHP in templates. But don't stuck on this. Extend your imagination. "If you can't go left, go right. Just don't stand"

The solution for this problem is much more easier than you can think. "If you can't add PHP in the templates, add templates to PHP" You just need a "echo" command in your PHP file. Yes it's so simple, the result will be exactly what you want, and most important, you can use vboptions, vbphrases, datamanager and everything you want.

1.- Create a template for Header:

HTML Code:
<template name="php_header" templatetype="template" date="" username="" version="">
<![CDATA[
{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
<head>
    <title>Your Title</title>
    {vb:raw headinclude}
     <vb:if condition="$vboptions['storecssasfile']">
        {vb:cssfile forumhome.css, options.css, vbcms.css, widgets.css, sidebar.css, forumhome-rollup.css, usercp.css, additional.css}
    <vb:else />
        {vb:cssfile forumhome.css, options.css, vbcms.css, widgets.css, sidebar.css, forumhome-rollup.css, usercp.css, additional.css}
    </vb:if>    
</head>
<body>
{vb:raw header}
{vb:raw navbar}
<div class="blockbody">
]]></template>


Remark:
If you want to make use of vB sidebar in your page, you must add within head section:

HTML Code:
    <script type="text/javascript">
    <!--
        document.write('<script type="text/javascript" src="' + yuipath + '/animation/animation-min.js?v={vb:raw vboptions.simpleversion}"></script>');
        var sidebar_align = 'right';
        var content_container_margin = parseInt('{vb:math {vb:stylevar forum_sidebar_width}+{vb:math {vb:stylevar padding}*2}}');
        var sidebar_width = parseInt('{vb:stylevar forum_sidebar_width}');
    //-->
    </script>    
    <script type="text/javascript" src="{vb:raw vboptions.bburl}/clientscript/vbulletin-sidebar.js?v={vb:raw vboptions.simpleversion}"></script> 


2.- Create a template for Footer:


HTML Code:
<template name="php_footer" templatetype="template" date="" username="" version="">
<![CDATA[
</div>
{vb:raw footer}
</body>
</html>
]]></template>
3.- In your PHP file (assuming that is a file which loads all vB functions that you need), add (again is an example):

Code:
// Header Block
$templater = vB_Template::create('php_header');
$templater->register_page_templates();
$navbits = construct_navbits(array("my_php_file.php?" . $vbulletin->session->vars['sessionurl'] . ""=> "My Page"));
$navbar = render_navbar_template($navbits);
$templater->register('navbar', $navbar);
$header_block = $templater->render();
echo $header_block;
............Here you can add any PHP code that you like..............

Code:
// Footer Block
$templater = vB_Template::create(php_footer');
$templater->register_page_templates();
$footer_block = $templater->render();
echo $footer_block;
As you can see you can use ANY vB template in the PHP file. Just assign it to a variable and then use echo to show it in the screen.

Hope that this short tutorial will be useful to those who have stuck with this vB limitation. I love to use the Smarty template engine with PHP.

Similar Mods

Advanced Templates: Dynamic Templates Changing according to different conditions vBulletin 2.x Full Releases

vblts.ru supports vBulletin®, 2022-2024