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
holy Offline
Junior Member
**
Posts: 1
Joined: May 2015
Post: #161
RE: PHP in Templates / Complex Templates
This is a really dumb question...

How to make message like this

[Image: ehHXef9.png]

using this plugin Ouch

Code:
<if last post in thread older than one month then>
Some random message here 
</if>

05-22-2015 10:40 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #162
RE: PHP in Templates / Complex Templates
Might not work, but perhaps something to get you started:

Code:
<if $thread['lastpost']+30*86400 < time() then>
derp
</if>


My Blog
05-23-2015 10:31 AM
Find all posts by this user Quote this message in a reply
fearlefish Offline
Junior Member
**
Posts: 1
Joined: Jun 2015
Post: #163
RE: PHP in Templates / Complex Templates
hey there,

I tried to make this work in my new 1.8.5 (it worked on my 1.6.13 version, yesterday I upgraded to 1.8.5) but the main page doesn't load anymore since I've activated this plugin. It shows only a blank page, when I try to open my mybb.

Any idea where this comes from?

I use php in my header.tpl with:

Code:
	
<?php
include("{$theme['imgdir']}/header.html");
?>


thank you

(This post was last modified: 06-14-2015 02:06 AM by fearlefish.)
06-14-2015 01:48 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #164
RE: PHP in Templates / Complex Templates
You'll need to check your error logs, or enable PHP errors to be displayed.
If you don't know how to do this, you'll need to ask your host in regards to how they've set up PHP error logging / handling.

My Blog
06-14-2015 12:10 PM
Find all posts by this user Quote this message in a reply
Light-Angel Offline
Junior Member
**
Posts: 2
Joined: Jun 2015
Post: #165
RE: PHP in Templates / Complex Templates
hi i have got half of this to work but i cant figger out why i cant get this rest to go thought

this part works

Code:
<if $mybb->user['usergroup'] == 4 then>
Admin private links
	</if>


but when i get down to the second one it wont show or it wont if i use from group 10 up but anything under 9 it will i have about 40 groups and at sometime they will get their own links so need to find out a way to do this

Code:
	<if $mybb->user['additionalgroups'] == 22 then>
TVM Ops Links
</if>



ok got it to work but the number has to be the first one in the list in additionalgroups is their anyway to look thought them all

(This post was last modified: 06-20-2015 03:52 AM by Light-Angel.)
06-20-2015 12:03 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #166
RE: PHP in Templates / Complex Templates

Code:
<if strpos(','.$mybb->user['additionalgroups'].',', ',22,') !== false then>


My Blog
06-20-2015 12:18 PM
Find all posts by this user Quote this message in a reply
Light-Angel Offline
Junior Member
**
Posts: 2
Joined: Jun 2015
Post: #167
RE: PHP in Templates / Complex Templates
OMG thank you so much that has just put me about 2 weeks ahead now agen thank you
06-20-2015 06:24 PM
Find all posts by this user Quote this message in a reply
hkkp Offline
Junior Member
**
Posts: 1
Joined: Apr 2015
Post: #168
RE: PHP in Templates / Complex Templates
[01-Sep-2015 18:04:56 Europe/Berlin] PHP Deprecated:  preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /www/htdocs/xxx/inc/plugins/phptpl.php on line 99

php 5.6 from Provider, please make a update. thanks
09-02-2015 02:32 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #169
RE: PHP in Templates / Complex Templates
Despite what the text says, preg_replace_callback cannot cover all the functionality offered by preg_replace/e

PHP7 does have a proper replacement, but you're probably not using it.

My Blog
09-03-2015 09:57 AM
Find all posts by this user Quote this message in a reply
golden Offline
Junior Member
**
Posts: 4
Joined: Nov 2013
Post: #170
RE: PHP in Templates / Complex Templates
hi,

any time we have to add this code after our <if ... then>...<elseif ... then>...<else>...</if> code?

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



------

and is this possible to use MYCODE system and this plugin (PHP in Template) to
hide content for some user groups? for user group 1,5
content which inside [hide] tags

Thanks

09-20-2015 07:09 PM
Find all posts by this user Quote this message in a reply


Forum Jump: