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,868
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: #61
RE: PHP in Templates / Complex Templates
Of course.

My Blog
12-13-2010 06:54 PM
Find all posts by this user Quote this message in a reply
Imran Offline
Member
***
Posts: 204
Joined: Apr 2010
Post: #62
RE: PHP in Templates / Complex Templates
wow, that's interesting.

[Image: logo.png]

[Image: twitter.png]
12-14-2010 05:10 AM
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #63
RE: PHP in Templates / Complex Templates
Updated to v1.8:
- adds the <?= ... ?> construct from Template Conditionals plugin; conditional isn't checked for safety
- adds <setvar ..>...</setvar> construct from Templates Conditionals plugin; these will still assign to the $GLOBALS['tplvars'] array
- adds phptpl_eval_expr and phptpl_eval_text from Template Conditionals; conditional isn't checked for safety

My Blog
12-16-2010 10:54 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #64
RE: PHP in Templates / Complex Templates
Upgraded.
Thank you very much, Yumi Smile

12-18-2010 04:03 AM
Find all posts by this user Quote this message in a reply
Imran Offline
Member
***
Posts: 204
Joined: Apr 2010
Post: #65
RE: PHP in Templates / Complex Templates
Brilliant. Thanks Yumi Smile

[Image: logo.png]

[Image: twitter.png]
12-18-2010 04:17 AM
Visit this user's website Find all posts by this user Quote this message in a reply
PK8 Offline
Junior Member
**
Posts: 13
Joined: Oct 2010
Post: #66
RE: PHP in Templates / Complex Templates
Heya! I'm a bit of a lurker and first time poster. Tongue This plugin's awesome and I'd really like to use it but I'm having a bit problem.

Alright, so I uploaded the plugin to where plugins usually get uploaded (inc/plugins) and activated it. So far so good. Then I took the plugin for a test spin and inserted <?php echo "Test"; ?> in a few templates. For some weird reason "Test" isn't being displayed. Oh

I did a fresh install of MyBB (1.6.1) in another location, uploaded the plugin, activated it, inserted <?php echo "Test"; ?> in the header template and took the plugin for a test run and "Test" gets displayed.

My problem's basically this: the plugin parses <?php echo "Test"; ?> in a clean install of MyBB but doesn't display anything in the... not so clean MyBB and I'm not sure why. XD
(This post was last modified: 12-19-2010 08:44 PM by PK8.)
12-19-2010 08:00 PM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #67
RE: PHP in Templates / Complex Templates
It does sound like you may have a plugin conflict. What you can do is upload your forum plugins one by one to the alternative location and see which one causes the "Test" to disappear. I realise that might be a bit faint-making if you have a lot of plugins, but that is a way to go.


[Image: leelink.gif]
MYBB1.6 & XThreads
12-19-2010 08:26 PM
Visit this user's website Find all posts by this user Quote this message in a reply
PK8 Offline
Junior Member
**
Posts: 13
Joined: Oct 2010
Post: #68
RE: PHP in Templates / Complex Templates
(12-19-2010 08:26 PM)leefish Wrote:  It does sound like you may have a plugin conflict. What you can do is upload your forum plugins one by one to the alternative location and see which one causes the "Test" to disappear. I realise that might be a bit faint-making if you have a lot of plugins, but that is a way to go.
Sorry for not responding sooner but thanks for the reply. Biggrin

I uploaded most of the plugins to the new location, activated them and not a single one managed to make the echoed "Test" disappear. I then decided to go compare the board settings between the two locations to see if there was a setting that was causing it. After a few minutes of comparing settings, I found what was causing the "Test" to not be displayed.

Turns out I had the Output template start/end comments? setting in the Server and Optimization Options setting group set to "No". I felt kind of dumbfounded after finding that out. XD

Thanks for the help, man. I can't wait to start making use of this plugin, thanks Zinga!
12-20-2010 05:09 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #69
RE: PHP in Templates / Complex Templates
Hmm, it _should_ work with HTML comments turned off though.  I wonder if MyBB changed something.
Thanks for finding that one and reporting, I'll take a look.

My Blog
12-20-2010 05:26 PM
Find all posts by this user Quote this message in a reply
Clientiesperti Offline
Junior Member
**
Posts: 7
Joined: Feb 2011
Post: #70
RE: PHP in Templates / Complex Templates
thanks for your plugin ...

I need to put an image with a link next to a forum ...
In forumbit_depth2_forum with the code <if ..... then>, how can I intercept the forum id 14 ? (or by forum name)
02-15-2011 01:45 AM
Find all posts by this user Quote this message in a reply


Forum Jump: