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,871
MyBB Compatibility: 1.2.x, 1.4.x, 1.6.x, 1.8.x
Plugin License: GPLv3
Uploader: ZiNgA BuRgA
jim7777 Offline
Junior Member
**
Posts: 41
Joined: Nov 2010
Post: #51
RE: PHP in Templates / Complex Templates
I downloaded your plugin recently and after I added some codes it shows up this error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /*******/boards/inc/functions_post.php(714) : eval()'d code on line 8

and here is the code:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<br />{$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;
}
}
?>
)


from template "postbit_attachments_attachment"

i don't know why but there maybe something wrong with my code? i try using that code in a separate php and it shows no errors.. I also try using the global variables like $mybb and $db still same error occurs on the same line.

What might be the error?? or a mis code function maybe?

(This post was last modified: 11-16-2010 01:53 PM by jim7777.)
11-16-2010 01:45 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #52
RE: PHP in Templates / Complex Templates
I tried your exact code and I don't get that issue here. (you do need to global $db)

Can you try it on a clean/test forum? (you'll probably get a query error, but that's okay)

My Blog
11-16-2010 01:55 PM
Find all posts by this user Quote this message in a reply
jim7777 Offline
Junior Member
**
Posts: 41
Joined: Nov 2010
Post: #53
RE: PHP in Templates / Complex Templates
i try using it in a test forum

and MyBB Shows me this error:

Error Type:
Catchable Fatal Error (4096)
Error Message:
Object of class MyBB could not be converted to string
Location:
File: inc/functions_post.php(714) : eval()'d code
Line: 4

EDIT:
I try using
$db->simple_select() and the errors are remove but it does not output anything T_T (or the value what I want to be printed)

@__________@
(This post was last modified: 11-16-2010 03:10 PM by jim7777.)
11-16-2010 03:01 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #54
RE: PHP in Templates / Complex Templates
(11-16-2010 03:01 PM)jim7777 Wrote:  i try using it in a test forum

and MyBB Shows me this error:

Error Type:
Catchable Fatal Error (4096)
Error Message:
Object of class MyBB could not be converted to string
Location:
File: inc/functions_post.php(714) : eval()'d code
Line: 4
I suspect you've miscopied the above code...

(11-16-2010 03:01 PM)jim7777 Wrote:  EDIT:
I try using
$db->simple_select() and the errors are remove but it does not output anything T_T (or the value what I want to be printed)
No, your code doesn't output anything.
You need to use echo before you see any output.

My Blog
11-16-2010 04:16 PM
Find all posts by this user Quote this message in a reply
jim7777 Offline
Junior Member
**
Posts: 41
Joined: Nov 2010
Post: #55
RE: PHP in Templates / Complex Templates
i get this variable: $attachment_cost_points and nothing comes

anyways I managed to fix it by re-uploading it again

tnx!
11-16-2010 07:51 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #56
RE: PHP in Templates / Complex Templates
A variable is a variable.  It's just a piece of data unless you tell the system you want this piece of data displayed.

You probably want to add this somewhere.

PHP Code:
echo $attachment_cost_points;


My Blog
11-16-2010 08:44 PM
Find all posts by this user Quote this message in a reply
katib Offline
Junior Member
**
Posts: 20
Joined: Dec 2010
Post: #57
RE: PHP in Templates / Complex Templates
Abother usefull plugin
Thank so much
After activating plugin, I get fellowing error:

Code:
Fatal error: Cannot redeclare phptpl_parsetpl() (previously declared in H:\xampplite\htdocs\mybb\inc\plugins\pl9forumicons.php:74) in H:\xampplite\htdocs\mybb\inc\plugins\phptpl.php on line 102

12-11-2010 07:46 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #58
RE: PHP in Templates / Complex Templates
If you are using the original pl9forumicons plugin, maybe this post will help you:
http://mybbhacks.zingaburga.com/showthre...http://mybbhacks.zingaburga.com/showthread.php?tid=288&pid=20

12-11-2010 07:51 AM
Find all posts by this user Quote this message in a reply
katib Offline
Junior Member
**
Posts: 20
Joined: Dec 2010
Post: #59
RE: PHP in Templates / Complex Templates
Thans RateU
Worked fine !!!!
12-11-2010 08:30 AM
Find all posts by this user Quote this message in a reply
Imran Offline
Member
***
Posts: 204
Joined: Apr 2010
Post: #60
RE: PHP in Templates / Complex Templates
Does this plugin supports arrays Yumi ?? Like <if in_array($allowed,$denied)>THEN DO SOMETHING</if>

[Image: logo.png]

[Image: twitter.png]
12-13-2010 03:58 PM
Visit this user's website Find all posts by this user Quote this message in a reply


Forum Jump: