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
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: PHP in Templates / Complex Templates
Updated this to v1.5.  The change is an additional cache checking mechanism which is enabled by default.  This additional cache check reduces performance, but increases compatibility with other plugins.

I've seen a number of plugins (including ones I've written myself) which modify the template cache to get things done.  This plugin has its own cache for performance reasons, but unfortunately, it's possible for other plugins' modifications not to get through to PHP in Templates' cache system.  This update mitigates the issue by refreshing the PHP in Templates' cache if the underlying template cache is modified (by another plugin).
This does come at a cost of performance, but as this plugin isn't intended for high performance (a plugin will almost always execute faster at implementing something than using this plugin), rather, more of a convenience plugin, I think it justifies the change.

My Blog
(This post was last modified: 03-15-2010 08:43 PM by ZiNgA BuRgA.)
03-15-2010 08:39 PM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #3
RE: PHP in Templates / Complex Templates
Thank you very much for the update, Yumi. This is an amazing plugin.

03-16-2010 08:17 AM
Find all posts by this user Quote this message in a reply
Robin Offline
Junior Member
**
Posts: 4
Joined: Jun 2008
Post: #4
RE: PHP in Templates / Complex Templates
I have encountered a problem when pulling data from a language file.

<if $GLOBALS['mybb']->user['usergroup'] == 1 then>{$lang->online_note}<else>{$lang->online_note2}</if>

In the example above, all "asterix" types of code (such as {1} for a number) is shown as it's shown in the language file rather than it's practical counter-part (a number from the database in the case of {1}), but only for the first hook. The hook after <else> converts {1} etc as it should when viewing the page in a browser. I suspect this has something to do with language files already using PHP, but why is then only the hook before <else> affected and not the one after?

Edit: I'm not sure, but could this problem be due to the fact that i simply copied the online_note hook and made a second one just below it in the language file in question? Do i have to do something more than copy and change the name of a hook in a language file in MyBB for it to properly convert asterisk code?

Edit 2: As i feared, this seems to be an issue with MyBB itself. Apparently, you cannot simply copy an entry in a language file and expect the copy to convert asterisks. I found this code in index.php:

$lang->online_note = $lang->sprintf($lang->online_note, my_number_format($onlinecount), $onlinebit, $mybb->settings['wolcutoffmins'], my_number_format($membercount), $memberbit, my_number_format($anoncount), $anonbit, my_number_format($guestcount), $guestbit);
eval("\$whosonline = \"".$templates->get("index_whosonline")."\";");

Copying this code and changing the first part from online_note to online_note2 did however cause another problem. They both convert asterisks properly, but the copy looks exactly like the original; as if you they were using the same text in the language file. I have no idea how to fix this problem or if the author of this plugin does either.
(This post was last modified: 04-04-2010 09:25 AM by Robin.)
04-04-2010 08:19 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #5
RE: PHP in Templates / Complex Templates
Yes, you'll need to use the $lang->sprintf function to convert the {1}, {2} wildcards.

If you're making a copy, make sure you change both instances correctly, eg:

$lang->online_note2 = $lang->sprintf($lang->online_note2, my_number_format($onlinecount), $onlinebit, $mybb->settings['wolcutoffmins'], my_number_format($membercount), $memberbit, my_number_format($anoncount), $anonbit, my_number_format($guestcount), $guestbit);

I'm assuming you forgot to change the second instance above.  Hope that helps.

My Blog
(This post was last modified: 04-04-2010 11:26 AM by ZiNgA BuRgA.)
04-04-2010 11:25 AM
Find all posts by this user Quote this message in a reply
Robin Offline
Junior Member
**
Posts: 4
Joined: Jun 2008
Post: #6
RE: PHP in Templates / Complex Templates
Ah, of course. Makes sense that i would miss something like that. Wink Thank you Burga!
04-04-2010 11:14 PM
Find all posts by this user Quote this message in a reply
siopk Offline
Junior Member
**
Posts: 11
Joined: Feb 2008
Post: #7
RE: PHP in Templates / Complex Templates
very very useful, I have been using this since the beginning of its first release
thanks very much

Yeah, I am good
04-19-2010 12:25 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #8
RE: PHP in Templates / Complex Templates
Thanks for the nice comment Smile

My Blog
04-19-2010 02:14 PM
Find all posts by this user Quote this message in a reply
ahstanford Offline
Junior Member
**
Posts: 5
Joined: Apr 2010
Post: #9
RE: PHP in Templates / Complex Templates
I just got this plugin installed (or so I assume since it doesn't have ANY installation instructions) and I still can't seem to even get an echo statement working from a template. After trying many different conditionals and php statements - it seems the plugin isn't working.

It only comes with one file, I uploaded that to inc/plugins and activated the plugin from Admin CP - but it doesn't seem to be functional.

Any advice?
04-21-2010 12:42 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #10
RE: PHP in Templates / Complex Templates
May I know what actually do you want to do?

04-21-2010 01:10 AM
Find all posts by this user Quote this message in a reply


Forum Jump: