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,867
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: #71
RE: PHP in Templates / Complex Templates

Code:
<if $forum['fid'] != 14 then>
Anything here won't be applied for forum id 14
</if>


(This post was last modified: 02-15-2011 02:18 AM by RateU.)
02-15-2011 02:17 AM
Find all posts by this user Quote this message in a reply
Clientiesperti Offline
Junior Member
**
Posts: 7
Joined: Feb 2011
Post: #72
RE: PHP in Templates / Complex Templates
Thanks
02-15-2011 04:44 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #73
RE: PHP in Templates / Complex Templates
Updated to v1.9:
- fix bug with this plugin not working if HTML Comments is disabled

My Blog
02-15-2011 09:28 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #74
RE: PHP in Templates / Complex Templates
Thanks for the update, Yumi.

02-16-2011 01:44 AM
Find all posts by this user Quote this message in a reply
AglioEoliO Offline
Junior Member
**
Posts: 39
Joined: Jan 2008
Post: #75
RE: PHP in Templates / Complex Templates
Thanks for updating, Yumi Wink
04-12-2011 02:40 AM
Find all posts by this user Quote this message in a reply
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #76
RE: PHP in Templates / Complex Templates
Hey Zinga, any idea why the following error appears when MyBB Go Mobile is Installed (if it's deactivated, it still appears):

Code:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /PATH_TO/inc/functions_post.php(511) : eval()'d code(10) : eval()'d code on line 6


It appears on pages with PHP enabled. I can't seem to figure out the problem.

06-03-2011 08:08 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #77
RE: PHP in Templates / Complex Templates
The GoMobile theme is still installed even if it's deactived I believe.  Are you using that theme?
What's the code in the related template anyway?

My Blog
06-03-2011 11:58 AM
Find all posts by this user Quote this message in a reply
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #78
RE: PHP in Templates / Complex Templates
No, the error happens with my default theme and most likely every theme.

Here's the PHP code in the template 'postbit_posturl':

PHP Code:
1
2
3
4
5
6
7
8
<?php
if ($postcounter == 1)
{
global $mybb;

echo '<div style="float: left; margin-bottom: 10px; text-shadow: 0 0px 0 #303030"><span class="st_facebook_button" displayText="Facebook"></span><span class="st_twitter_button" displayText="Tweet"></span><span class="st_email_button" displayText="Email"></span></div><br class="clear" />';
}
 ?>


It's nothing special really. If I take it out, the error no longer displays.

I've even stripped out nearly most of the code lines and the error still happens. I've done the same for the language files. I've also created new files because it could be some hidden character and also converted to UTF-8 without BOM and still no luck. I can't find the solution for this problem in the GoMobile plugin so I thought that perhaps the problem would be with this one. global_start hook doesn't seem to be a problem either, I've changed the priority of the GM's one to one higher than the PHP in Templates' one and still no luck.

/edit don't ask me why that $mybb is there, I had something else there before probably and removed it and global $mybb; was left there but I doubt that's the problem

(This post was last modified: 06-04-2011 09:10 PM by Pirata Nervo.)
06-04-2011 09:01 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #79
RE: PHP in Templates / Complex Templates
I can't see the problem with that code - tried it and works fine for me.
All I can suggest doing is modifying your functions_post.php and, on the line specified, replace eval() with die() or var_dump() (if it doesn't do it on the first post) and see what it's trying to execute.

My Blog
(This post was last modified: 06-06-2011 06:43 PM by ZiNgA BuRgA.)
06-06-2011 06:42 PM
Find all posts by this user Quote this message in a reply
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #80
RE: PHP in Templates / Complex Templates
It works fine together with MyBB GoMobile installed? That's really weird. I guess I'll have to look at functions_post.php then
06-07-2011 02:23 AM
Find all posts by this user Quote this message in a reply


Forum Jump: