PHP in Templates / Complex Templates
Author Message
This is a relatively small plugin, but seems that a number of people wanted such a thing, so... here it is.

This plugin will allow you to use:
  • PHP in templates, using <?php ... ?> tags
  • Shortcut template conditionals, using <if ... then>...<elseif ... then>...<else>...</if>
  • Some shortcut string functions (see below), eg <func htmlspecialchars>...</func>
  • Call another template, using <template ...>, eg <template header>

For those that do not wish to allow arbitrary PHP code execution that this plugin offers, take a look at the Template Conditionals plugin.

Here's an example of some of the functions that this can be used for - for example, you may use this code in your postbit:


<if $post['fid5'] then>
Your game tag is <func htmlspecialchars_uni>{$post['fid5']}</func>
<elseif $post['fid6'] and $mybb->user['cancp'] then>
This user's lucky number is <func intval>{$post['fid6']}</func>
<else />Some other profile field: {$post['fid7']}</if>

<?php echo "Hi from PHP"; ?>

Some notes:

  • PHP tags must be closed with ?>
  • PHP runs slower than conditionals, so unless you need to use PHP, I recommend the conditionals
  • PHP inside the <?php ... ?> tags must be well formed, ie the following will not work:

    PHP Code:
    <?php if(true) { ?> some stuff <?php } ?>

    I'm thinking about whether to add support for the above, but there are performance reasons for me choosing not to.

  • The template insertion function is really basic - it performs no caching, so you should not call a lot of templates this way (there really isn't any nice way to get around this) however should be fine for small things.  Also, as it is basic, ensure that you don't stuff it up with recursive calls, that is, it's possible to try to get a template to call itself, but that will obviously cause MyBB to stuff up
  • As of v1.5, I've included the ability to perform additional cache checks, and enabled this option by default.  If you wish to disable this feature, it can be done by modifying a define in the .php file, but this is not necessary for most people (see post below this one for more info).
  • As of v2.1, PHP 5.3 or later is required

The functions available, with the shortcut, are:
htmlspecialchars, htmlspecialchars_uni, intval, file_get_contents, floatval, urlencode, rawurlencode, addslashes, stripslashes, trim, crc32, ltrim, rtrim, chop, md5, nl2br, strrev, strtoupper, strtolower, my_strtoupper, my_strtolower, alt_trow, get_friendly_size, filesize, strlen, my_strlen, my_wordwrap, random_str, unicode_chr, bin2hex, str_rot13, str_shuffle, strip_tags, ucfirst, ucwords, basename, dirname, unhtmlentities
(This post was last modified: 03-01-2021 08:29 PM by ZiNgA BuRgA.)
Find all posts by this user
Quote this message in a reply
Download: phptpl-2.2.7z (3.08 KB)
Plugin Version: 2.2
Last Updated: 03-01-2021, 08:29 PM

Downloads: 14,207
MyBB Compatibility: 1.2.x, 1.4.x, 1.6.x, 1.8.x
Plugin License: GPLv3
Uploader: ZiNgA BuRgA
netllama Offline
Junior Member
Posts: 1
Joined: Mar 2021
Post: #231
RE: PHP in Templates / Complex Templates
Just wanted to express thanks & appreciation for this plugin.  It works perfectly for my needs in mybb-1.8.26.
03-15-2021 07:50 AM
Find all posts by this user Quote this message in a reply
c.widow Offline
Junior Member
Posts: 2
Joined: Mar 2022
Post: #232
RE: PHP in Templates / Complex Templates
This works beautifully in forumdisplay.php for the forumdisplay template.

<if in_array($fid, $wcc_icboards_array) then>

However, in order to use that same code on the index.php for forumbit templates I have to change $fid to $forum['fid'] and the code stops working. Inside forumbit_depth2_forum template.

<if in_array($forum['fid'], $wcc_icboards_array) then>

Any idea what I'm doing wrong? I even tried it with just $fid but that's not a variable in index.php file... so I'm... lost.

Any help is appreciated!

03-26-2022 06:32 AM
Find all posts by this user Quote this message in a reply
Gerti Offline
Junior Member
Posts: 6
Joined: Jun 2020
Post: #233
RE: PHP in Templates / Complex Templates
MyBB-Version: 1.8.30

If the plugin is used under PHP 8.1.2 and MySQLi 10.6.7, the error message is always:
/inc/plugins/phptpl.php 92 errorHandler->error_callback
/inc/plugins/phptpl.php 109 preg_replace_callback_array
Undefined array key 2 - Line: 92 - File: inc/plugins/phptpl.php PHP 8.x.x (Linux)
/inc/plugins/phptpl.php(70) : eval()'d code 19 phptpl_parsetpl
inc/plugins/phptpl.php 118 Warning Undefined array key 2

What needs to be adjusted in the plugin so that there are no more error messages under PHP 8.x?

Many thanks in advance for the information.

(Translated from German to English with Google Translator)
(This post was last modified: 05-05-2022 08:11 PM by Gerti.)
05-04-2022 06:25 PM
Find all posts by this user Quote this message in a reply
gudencsgo Offline
Junior Member
Posts: 1
Joined: Jun 2022
Post: #234
RE: PHP in Templates / Complex Templates
Hello, I am having some issues using this plugin. Whenever I try to use curly brackets inside of the php tags it throws me an error as such:

Parse error: syntax error, unexpected '}' in /home/hq/public_html/global.php(961) : eval()'d code(2) : eval()'d code on line 4

The code I am using is this:

if($mybb->user['uid'] > 0) {

However, whenever I remove the 2 curly brackets it works as expected. What could be causing this? Thanks.
06-19-2022 01:49 AM
Find all posts by this user Quote this message in a reply

Forum Jump: