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
techu Offline
Member
***
Posts: 154
Joined: Sep 2010
Post: #41
RE: PHP in Templates / Complex Templates

Code:
<if $mybb->user['usergroup'] == 3 || $mybb->user['usergroup'] == 4 || $mybb->user['usergroup'] == 6 then>
something
</if>

The above code works but when I use not equal to it does not work.

Code:
<if $mybb->user['usergroup'] != 3 || $mybb->user['usergroup'] != 4 || $mybb->user['usergroup'] != 6 then>
something
</if>

The above does not work.

But again

Code:
<if $mybb->user['usergroup'] != 3 then>
something
</if>


The above code works..

Is it something wrong with the code or a bug or something?


[Image: banner.png]
10-09-2010 12:37 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Online
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #42
RE: PHP in Templates / Complex Templates
(10-09-2010 12:37 AM)techu Wrote:  The above code works but when I use not equal to it does not work.

Code:
<if $mybb->user['usergroup'] != 3 || $mybb->user['usergroup'] != 4 || $mybb->user['usergroup'] != 6 then>
something
</if>

The above does not work.

Obviously, when can that statement ever be false?

Think about it:

PHP Code:
if($x != 1 || $x != 2)

There is no value of $x which can make the above statement false.


My Blog
(This post was last modified: 10-09-2010 10:18 AM by ZiNgA BuRgA.)
10-09-2010 10:17 AM
Find all posts by this user Quote this message in a reply
techu Offline
Member
***
Posts: 154
Joined: Sep 2010
Post: #43
RE: PHP in Templates / Complex Templates
/\oh ok.


basically what I was trying to do is that I want to show some html only for normal user, I dont want that part of html to admin, mod and super mod so how can I do that?

[Image: banner.png]
10-09-2010 12:24 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Online
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #44
RE: PHP in Templates / Complex Templates
You're asking for two different things.

(10-09-2010 12:24 PM)techu Wrote:  I want to show some html only for normal user

Code:
<if $mybb->user['usergroup'] == 2 then>
...
</if>



(10-09-2010 12:24 PM)techu Wrote:  I dont want that part of html to admin, mod and super mod so how can I do that?

Code:
<if $mybb->user['usergroup'] != 3 && $mybb->user['usergroup'] != 4 && $mybb->user['usergroup'] != 6 then>
...
</if>

(you need to use AND, not OR)
Or

Code:
<if !in_array($mybb->user['usergroup'], array(3,4,6)) then>
...
</if>


My Blog
10-09-2010 01:55 PM
Find all posts by this user Quote this message in a reply
techu Offline
Member
***
Posts: 154
Joined: Sep 2010
Post: #45
RE: PHP in Templates / Complex Templates
spot on...
Thanks Smile

[Image: banner.png]
10-09-2010 03:44 PM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #46
RE: PHP in Templates / Complex Templates
Hi, I would like to be able to unlock some forums/extra privileges etc on my site based on which FORUM a user has started a thread in. Is there a php in templates snippet I could put in the templates for that? I would like it to be automatic and as the user promotion system does not support creating threads as a criteria for promotion I don't really want to use GIDs as that would mean I have to move the users to a specific group manually.

Can anyone help?


[Image: leelink.gif]
MYBB1.6 & XThreads
11-05-2010 05:32 AM
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Online
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #47
RE: PHP in Templates / Complex Templates
Probably try something like this - note, it won't do a promotion as you've said, PHP in Templates is only really good at manipulating what's displayed:

PHP Code:
1
2
3
4
5
6
<?php
if($mybb->user['uid'])
$has_made_thread_in_forum = $db->fetch_array($db->simple_select('threads', 'tid', 'uid='.$mybb->user['uid'].' AND fid='.$foruminfo['fid'], array('limit' => 1)));

if($has_made_thread_in_forum) ...
?>


My Blog
(This post was last modified: 11-05-2010 09:03 AM by ZiNgA BuRgA.)
11-05-2010 09:02 AM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #48
RE: PHP in Templates / Complex Templates
Thank you Zinga - I shall go and try that Smile


[Image: leelink.gif]
MYBB1.6 & XThreads
11-05-2010 09:38 AM
Visit this user's website Find all posts by this user Quote this message in a reply
1master1 Offline
Member
***
Posts: 232
Joined: Oct 2010
Post: #49
RE: PHP in Templates / Complex Templates
have anyone encountered this? the if conditions are ignored in Internet Explorer. Can anyone please check and update here?
11-11-2010 02:20 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Online
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #50
RE: PHP in Templates / Complex Templates
That shouldn't even be possible as this doesn't have any behavioural relationship with browsers.  This is purely a server side plugin.
Unless, of course, you're explicitly causing something to break on the browser side, or you're explicitly doing something browser dependent.

My Blog
11-11-2010 02:38 PM
Find all posts by this user Quote this message in a reply


Forum Jump: