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,864
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: #91
RE: PHP in Templates / Complex Templates
http://mybbhacks.zingaburga.com/announcements.php?aid=1

My Blog
11-25-2011 11:33 AM
Find all posts by this user Quote this message in a reply
Earl Grey Offline
Junior Member
**
Posts: 12
Joined: Nov 2011
Post: #92
RE: PHP in Templates / Complex Templates
(11-25-2011 07:17 AM)RateU Wrote:  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.


Got it working, used this and worked a treat! Thanks!

I'm a bit of a novice and I was wondering how to use the conditionals to create more than two different codings?

IE for the moment I can have:
<if id = 1>
First template
</else>
Second template
</if>

But what if I wanted to have eight different templates? How would I go about doing that? I've tried just adding more if conditionals within it but it doesn't work.
11-25-2011 11:36 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #93
RE: PHP in Templates / Complex Templates
Nesting?

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
<if $forum['fid'] == 1 then>
 ID 1
<else>
  <if $forum['fid'] == 2 then>
   ID 2
  <else>
    <if $forum['fid'] == 3 then>
     ID 3
    <else>
     something else
    </if>
  </if>
</if>

Or reduce the clutter with elseif's

Code:
1
2
3
4
5
6
7
8
9
<if $forum['fid'] == 1 then>
 ID 1
<elseif $forum['fid'] == 2 then>
 ID 2
<elseif $forum['fid'] == 3 then>
 ID 3
<else>
 no, I'm not going to put any more IDs
</if>


My Blog
11-25-2011 03:45 PM
Find all posts by this user Quote this message in a reply
Earl Grey Offline
Junior Member
**
Posts: 12
Joined: Nov 2011
Post: #94
RE: PHP in Templates / Complex Templates
(11-25-2011 03:45 PM)ZiNgA BuRgA Wrote:  Nesting?

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
<if $forum['fid'] == 1 then>
 ID 1
<else>
  <if $forum['fid'] == 2 then>
   ID 2
  <else>
    <if $forum['fid'] == 3 then>
     ID 3
    <else>
     something else
    </if>
  </if>
</if>

Or reduce the clutter with elseif's

Code:
1
2
3
4
5
6
7
8
9
<if $forum['fid'] == 1 then>
 ID 1
<elseif $forum['fid'] == 2 then>
 ID 2
<elseif $forum['fid'] == 3 then>
 ID 3
<else>
 no, I'm not going to put any more IDs
</if>


Perfect! Thanks so much Smile
11-25-2011 11:33 PM
Find all posts by this user Quote this message in a reply
FabrizioCo Offline
Junior Member
**
Posts: 2
Joined: Dec 2011
Post: #95
RE: PHP in Templates / Complex Templates
Hello Zinga, really good job. Thanks!
I read that the code to extract data from the database is the follow. How do I set it to extract all rows from db?

Quote:{$attachment['icon']}&nbsp;&nbsp;<a href="attachment.php?aid={$attachment['aid']}" target="_blank">{$attachment['filename']}</a> ({$lang->postbit_attachment_size} {$attachment['filesize']} / {$lang->postbit_attachment_downloads} {$attachment['downloads']}
<?php

   $affected_fids = explode(',', $mybb->settings['newpoints_atcost_forums']);
   if (in_array($fid, $affected_fids)){

   $attach_id = $attachment['aid'];
   $attach_check = $db->query("SELECT * FROM mybb_newpoints_attachments WHERE AID='$attach_id'");
   $rows = $db->num_rows($attach_check);
   if ($rows > 0){
    $attach_cost = $db->fetch_field($attach_check, "cost");
    $attachment_cost_points = "/ <font color='red'>Cost:</font> ".$attach_cost;
}
}
?>
)

(This post was last modified: 12-12-2011 05:10 AM by FabrizioCo.)
12-12-2011 05:07 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #96
RE: PHP in Templates / Complex Templates
(12-12-2011 05:07 AM)FabrizioCo Wrote:  How do I set it to extract all rows from db?
Use looping, like while?

12-13-2011 01:13 AM
Find all posts by this user Quote this message in a reply
FabrizioCo Offline
Junior Member
**
Posts: 2
Joined: Dec 2011
Post: #97
RE: PHP in Templates / Complex Templates
(12-13-2011 01:13 AM)RateU Wrote:  
(12-12-2011 05:07 AM)FabrizioCo Wrote:  How do I set it to extract all rows from db?
Use looping, like while?

I used this code, but it doesn't work. I'm not good in object-oriented programming.

Quote:$query_2 = $db->query("SELECT * FROM voto_voti WHERE id_user = '$userid'");
$number_2 = $db->num_rows($query_2);

$i = 0;

if ($number_2 > 0){          
while ($number_2 > $i){          
$id_domanda = $db->fetch_field($query_2, "id_domanda");
$i++;
}
}
}
12-13-2011 04:06 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #98
RE: PHP in Templates / Complex Templates
If you are really sure that it is rows, not columns in one row only, maybe something like this:

PHP Code:
while ($mydata = $db->fetch_array($query_2)){
// $mydata array here
}


(This post was last modified: 12-13-2011 05:56 AM by RateU.)
12-13-2011 05:54 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #99
RE: PHP in Templates / Complex Templates
(12-13-2011 04:06 AM)FabrizioCo Wrote:  I used this code, but it doesn't work. I'm not good in object-oriented programming.
MyBB is pretty much all procedural.  The only link with OO is a few classes, which basically just act as fancy functions.

Although your code should work (as far as the loop is concerned), assuming you're using the data somewhere.

My Blog
12-13-2011 09:43 AM
Find all posts by this user Quote this message in a reply
dexon95 Offline
Junior Member
**
Posts: 1
Joined: Jan 2012
Post: #100
RE: PHP in Templates / Complex Templates
Hi, i need help with this code :

PHP Code:
$balance = mysql_query("SELECT `balance` FROM iconomy WHERE `username` = '{$mybb->user[\'fid4\']}'");
echo mysql_real_escape_string($balance); 

Cause MyBB is telling me it have a security issue :/
And i don't really see how i can fix it Frown

Can you help me please ?

01-09-2012 11:19 AM
Find all posts by this user Quote this message in a reply


Forum Jump: