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,885
MyBB Compatibility: 1.2.x, 1.4.x, 1.6.x, 1.8.x
Plugin License: GPLv3
Uploader: ZiNgA BuRgA
Gerti Offline
Junior Member
**
Posts: 7
Joined: Jun 2020
Post: #221
RE: PHP in Templates / Complex Templates
I create in "ACP > Configuration > Help Pages > a new help page" with the following content:

PHP Code:
1
2
3
4
5
6
7
8
Here comes the content of the test_page.php:
Test 1 = Start with Path:
<?php include ("./folder/test page.php"); ?>
End.
 ||| 
Test 2 = Start with URL/Path:
<?php include ("https://url/folder/test_page.php"); ?>
End.

The content of the test_page.php is not displayed. It will be displayed:
Here comes the content of the test_page.php: Test 1 = Start with Path: End. ||| Test 2 = Start with URL/Path: End.

The above page definition integrated in "ACP > Templates & Styles > any template" works.

What am I doing wrong?

(This post was last modified: 06-22-2020 05:53 PM by Gerti.)
06-22-2020 05:44 PM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #222
RE: PHP in Templates / Complex Templates
Oh... The plugin name is PHP in Templates, so it only works in templates.

06-23-2020 02:45 AM
Find all posts by this user Quote this message in a reply
Gerti Offline
Junior Member
**
Posts: 7
Joined: Jun 2020
Post: #223
RE: PHP in Templates / Complex Templates
so i told you! Jap

Is there (or would anyone have) a solution how to implement "php-include" in help_pages?
(This post was last modified: 06-23-2020 06:30 PM by Gerti.)
06-23-2020 05:16 PM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #224
RE: PHP in Templates / Complex Templates
You could achieve this with a simple plugin (I'd use something like Hooks or Patches to avoid a plugin for this). In misc.php find:

PHP Code:
$plugins->run_hooks("misc_help_helpdoc_end");


Add after:

PHP Code:
$templates->cache['helpdoc_'.$helpdoc['hid']] = $helpdoc['document'];

$document = eval($templates->render('helpdoc_'.$helpdoc['hid']));


Then, in your misc_help_helpdoc template search {$helpdoc['document']} and replace with {$document}.

BTW, nice to see you around @RateU!


Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 06-24-2020 11:38 AM by Sama34.)
06-24-2020 11:38 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Gerti Offline
Junior Member
**
Posts: 7
Joined: Jun 2020
Post: #225
RE: PHP in Templates / Complex Templates
It works as desired.
Many thanks for the solution.
06-24-2020 06:38 PM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #226
RE: PHP in Templates / Complex Templates
(06-24-2020 11:38 AM)Sama34 Wrote:  BTW, nice to see you around @RateU!

Nice to see you too Smile

06-25-2020 03:44 AM
Find all posts by this user Quote this message in a reply
Schnapsnase Offline
Junior Member
**
Posts: 1
Joined: Jul 2020
Post: #227
RE: PHP in Templates / Complex Templates
(06-23-2020 02:45 AM)RateU Wrote:  Oh... The plugin name is PHP in Templates, so it only works in templates.

I have integrated this code from Sama34 into the plugin.
Look here:
.zip  phptpl-2.1.1.zip (Size: 2.66 KB / Downloads: 321)

I think / hope it's a good idea.
If not, please delete my post. Wink

Nice to see you again Smile
07-17-2020 12:44 AM
Find all posts by this user Quote this message in a reply
leeloo Offline
Junior Member
**
Posts: 1
Joined: Oct 2020
Post: #228
RE: PHP in Templates / Complex Templates
I'm trying to show a specific userfield reflecting the lastposter on the forum index. When i try using the 'username' version it shows me the correct name for the lastposter. However, when I try the fid option this doesn't work. It only shows the logged in user's own userfields and ignores the lastposter's of everyone else.


<?php $user = get_user($lastpost_data['lastposteruid']); echo $user['username']; ?>
^ gives me the correct username of the lastposter

<?php $user = get_user($lastpost_data['lastposteruid']); echo $user['fid4']; ?>
^ however only gives the user logged in the user's lastpost userfield, hides others
(This post was last modified: 10-14-2020 12:38 AM by leeloo.)
10-14-2020 12:11 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #229
RE: PHP in Templates / Complex Templates
(11-17-2018 11:23 AM)own3mall Wrote:  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>

Urgh, PHP in a nested template is problematic - I'll work out a fix.


(12-28-2018 10:17 AM)ahmedwaill Wrote:  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 !!
The while statement must be complete within <?php ... ?> blocks, i.e. you can't ever have while(...) { ?> ... <?php }.

(10-14-2020 12:11 AM)leeloo Wrote:  <?php $user = get_user($lastpost_data['lastposteruid']); echo $user['fid4']; ?>
^ however only gives the user logged in the user's lastpost userfield, hides others
get_user doesn't pull userfields.

My Blog
02-28-2021 10:06 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #230
RE: PHP in Templates / Complex Templates
Updated plugin to hopefully fix <template> handling for templates with <?php snippets.

My Blog
03-01-2021 08:31 PM
Find all posts by this user Quote this message in a reply


Forum Jump: