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:

HTML Code
1
2
3
4
5
6
7
8
9
{$post['user_details']}

<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: 06-26-2023 10:42 PM by ZiNgA BuRgA.)
Find all posts by this user
Quote this message in a reply
Download: phptpl-2.3.7z (2.62 KB)
Plugin Version: 2.3
Last Updated: 06-26-2023, 10:42 PM

Downloads: 14,862
MyBB Compatibility: 1.2.x, 1.4.x, 1.6.x, 1.8.x
Plugin License: GPLv3
Uploader: ZiNgA BuRgA
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #211
RE: PHP in Templates / Complex Templates
At the top right of the first post.

02-17-2018 01:39 AM
Find all posts by this user Quote this message in a reply
Kiddo Offline
Junior Member
**
Posts: 1
Joined: Apr 2018
Post: #212
RE: PHP in Templates / Complex Templates
How can I display Usergroups Owner, Leaders, Number of Members and Join ?
04-25-2018 12:57 PM
Find all posts by this user Quote this message in a reply
own3mall Offline
Junior Member
**
Posts: 1
Joined: Nov 2018
Post: #213
RE: PHP in Templates / Complex Templates
Hi All,

I'm new to using this plugin.  When I use this plugin on the latest version of MyBB, I get this error when trying to use a custom global template I've created:

Parse error: syntax error, unexpected 'ob_start' (T_STRING) in /{PATH_REMOVED}/global.php(872) : eval()'d code on line 9

In the header template for one of my themes, I have the following:

Code:
<template random_logo>


I created a global template called random_logo with the following contents:

Code:
1
2
3
4
5
6
<?php 
     $randomInt=rand(1,3); 
     $imgToUse="images/logo_" . $randomInt . ".png"; 
     // echo $imgToUse; 
?>
<a href="{$mybb->settings['bburl']}/index.php"><img src="{$imgToUse}>" alt="{$mybb->settings['bbname']}" title="{$mybb->settings['bbname']}" /></a>


What am I doing wrong?

(This post was last modified: 11-17-2018 11:24 AM by own3mall.)
11-17-2018 11:23 AM
Find all posts by this user Quote this message in a reply
ahmedwaill Offline
Junior Member
**
Posts: 2
Joined: Dec 2018
Post: #214
RE: PHP in Templates / Complex Templates
hello , i don't know whether this Plugin still supported and updating or not , but please if you can make it able to use while statement it will be amazing , tried using while statement but failed with many things the only thing that will work if i use the code i want bbut instead of while i use "if" but it won't do things looped
here is the code i wanna use !!

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  <tbody>
    <tr>
	<?php
       while($r = mysqli_fetch_assoc($result)){ 
     ?>
      <th scope="row">{$r['id']}</th>
      <td>{$r['name']}</td>
      <td>{$r['size']}</td>
      <td>{$r['type']}</td>
      <td><a href="{$r['location']}">View</a></td>
      <td><a href="#" onclick="deleteItem({$r['id']})">Delete</a></td>
<?php } ?>

    </tr>
  </tbody>


(11-17-2018 11:23 AM)own3mall Wrote:  Hi All,

I'm new to using this plugin.  When I use this plugin on the latest version of MyBB, I get this error when trying to use a custom global template I've created:

Parse error: syntax error, unexpected 'ob_start' (T_STRING) in /{PATH_REMOVED}/global.php(872) : eval()'d code on line 9

In the header template for one of my themes, I have the following:

Code:
<template random_logo>


I created a global template called random_logo with the following contents:

Code:
1
2
3
4
5
6
<?php 
     $randomInt=rand(1,3); 
     $imgToUse="images/logo_" . $randomInt . ".png"; 
     // echo $imgToUse; 
?>
<a href="{$mybb->settings['bburl']}/index.php"><img src="{$imgToUse}>" alt="{$mybb->settings['bbname']}" title="{$mybb->settings['bbname']}" /></a>


What am I doing wrong?


i guess it doesn't support using the codes
<?php
?>
try the codes you want without those it will work , tried many times
(This post was last modified: 12-28-2018 10:19 AM by ahmedwaill.)
12-28-2018 10:17 AM
Find all posts by this user Quote this message in a reply
.mouni Offline
Junior Member
**
Posts: 2
Joined: Feb 2015
Post: #215
RE: PHP in Templates / Complex Templates
looks like while already works with the plugin (eg.)
12-29-2018 01:48 PM
Find all posts by this user Quote this message in a reply
ahmedwaill Offline
Junior Member
**
Posts: 2
Joined: Dec 2018
Post: #216
RE: PHP in Templates / Complex Templates
(12-29-2018 01:48 PM).mouni Wrote:  looks like while already works with the plugin (eg.)

i've tried it , but not working
12-29-2018 08:32 PM
Find all posts by this user Quote this message in a reply
Gerti Offline
Junior Member
**
Posts: 7
Joined: Jun 2020
Post: #217
RE: PHP in Templates / Complex Templates
German:
Hätte mir jemand eine Lösung, wie ich in den Hilfedateien von MyBB V1.8.22 auch PHP wie z.B.:

PHP Code:
<?php include ("dateiname.php"); ?>

einbinden kann?

English.
If someone would have given me a solution, as I did in the help files of MyBB V1.8.22 also PHP like :

PHP Code:
<?php include ("filename.php"); ?>

(This post was last modified: 06-16-2020 09:47 PM by Gerti.)
06-16-2020 09:46 PM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #218
RE: PHP in Templates / Complex Templates
As long as your syntax is correct, it should works.

06-21-2020 06:14 AM
Find all posts by this user Quote this message in a reply
Gerti Offline
Junior Member
**
Posts: 7
Joined: Jun 2020
Post: #219
RE: PHP in Templates / Complex Templates
The syntax works correctly in the templates. Not in the help pages (misc). But in the help pages isn't work. What can/must i do?
06-22-2020 01:38 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #220
RE: PHP in Templates / Complex Templates
I've tried it in misc_help template, it works.

06-22-2020 02:05 AM
Find all posts by this user Quote this message in a reply


Forum Jump: