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
d4ni3e Offline
Junior Member
**
Posts: 2
Joined: Aug 2011
Post: #81
RE: PHP in Templates / Complex Templates
Okay it works very nice i tried a php code and works it the Theme ZINK but
i get an error at global.php that i can't solve and i tried him it other theme(facebook) and it does not show the error.
check it out...

Code:
Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /home/hitcj/public_html/forum/global.php(497) : eval()'d code(32) : eval()'d code on line 2

Thank'[s.

(This post was last modified: 08-01-2011 09:38 AM by d4ni3e.)
08-01-2011 09:35 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #82
RE: PHP in Templates / Complex Templates
Probably an issue with one or more of the following templates:
global_pm_alert, headerinclude, gobutton, htmldoctype, header

Try reverting them (save a backup beforehand) to see if any of them are causing the problem.

My Blog
08-01-2011 09:48 AM
Find all posts by this user Quote this message in a reply
d4ni3e Offline
Junior Member
**
Posts: 2
Joined: Aug 2011
Post: #83
RE: PHP in Templates / Complex Templates
(08-01-2011 09:48 AM)ZiNgA BuRgA Wrote:  Probably an issue with one or more of the following templates:
global_pm_alert, headerinclude, gobutton, htmldoctype, header

Try reverting them (save a backup beforehand) to see if any of them are causing the problem.
Thanks the issue has at headerinclude .
Thank you so much for the help.
08-01-2011 09:59 AM
Find all posts by this user Quote this message in a reply
Rukbat Offline
Junior Member
**
Posts: 1
Joined: Sep 2011
Post: #84
RE: PHP in Templates / Complex Templates
Add one more heartfelt "thank you".  I needed to add some PHP somewhere (for a link exchange) and this saved me from having to learn how the core handles templates.  (I'll figure it out eventually, but I wanted to get a few things actually done.)

And I'm sure that reading your code is going to help me learn how MyBB works.  (Until 2.0 - then we all start from zero, I guess.)

Thanks again.
09-25-2011 10:28 AM
Find all posts by this user Quote this message in a reply
Earl Grey Offline
Junior Member
**
Posts: 12
Joined: Nov 2011
Post: #85
RE: PHP in Templates / Complex Templates
Hey there, was just wanting to make sure this is accomplishable before I download this mod.

Basically I'm looking to create the effect like the following two sites:

site one and site two

Do you see how their first categories and forums look different to later ones? IE: On site two the first category has horizontally stacked forums whilst the second category stacks three side by side and includes images? Essentially that's what I'm trying to achieve.

I think I'd use some sort of conditional in the templates and use forum IDS:

For example:

<if category=1> Template here </else> Other template here </if>

I hope I've explained myself! Do I make any sense?
11-23-2011 08:42 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #86
RE: PHP in Templates / Complex Templates
Try it and post back.

My Blog
11-23-2011 04:36 PM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #87
RE: PHP in Templates / Complex Templates
Maybe you are lookinf for something like this Earl Grey http://community.mybb.com/thread-102695.html

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
11-23-2011 06:35 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Earl Grey Offline
Junior Member
**
Posts: 12
Joined: Nov 2011
Post: #88
RE: PHP in Templates / Complex Templates
(11-23-2011 04:36 PM)ZiNgA BuRgA Wrote:  Try it and post back.

Well I don't know what to try, that's why I was asking. Does anybody know how I'd go about it?

(11-23-2011 06:35 PM)Sama34 Wrote:  Maybe you are lookinf for something like this Earl Grey http://community.mybb.com/thread-102695.html

Nope, definitely not. But thanks for the suggestion.
11-25-2011 06:43 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #89
RE: PHP in Templates / Complex Templates
You can try to check by using $forum['fid']. Something like this:

Code:
<if $forum['fid'] == x then>
Codes for forum id = x here
<else>
Codes for other forum ids here
</if>


If it's a complex modification, personally, I prefer using XThreads for that.


11-25-2011 07:17 AM
Find all posts by this user Quote this message in a reply
Earl Grey Offline
Junior Member
**
Posts: 12
Joined: Nov 2011
Post: #90
RE: PHP in Templates / Complex Templates
Okay I will give that a go thank you!

I've downloaded it and tried to unzip the package but it isn't working! Is there a different file format or a recommended unzipper (for mac).
11-25-2011 09:10 AM
Find all posts by this user Quote this message in a reply


Forum Jump: