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,861
MyBB Compatibility: 1.2.x, 1.4.x, 1.6.x, 1.8.x
Plugin License: GPLv3
Uploader: ZiNgA BuRgA
terzier Offline
Member
***
Posts: 91
Joined: Feb 2012
Post: #111
RE: PHP in Templates / Complex Templates
same problem.

Code:
Parse error: syntax error, unexpected T_STRING in /home/*****/public_html/forums/global.php(523) : eval()'d code(1) : eval()'d code on line 1


(01-21-2012 08:14 AM)ZiNgA BuRgA Wrote:  Revert your headerinclude, gobutton, htmldoctype and header templates.

ask :
What template are you referring to? GoMobile or default template that is used?

Un-Official MyBB from Indonesia| Facebook Page|Twitter
GARUDA DIDADA KU
04-23-2012 10:50 PM
Visit this user's website Find all posts by this user Quote this message in a reply
adisp007 Offline
Junior Member
**
Posts: 26
Joined: Apr 2012
Post: #112
RE: PHP in Templates / Complex Templates
^@
In GoMobile template -> htmldoctype
Delete this code:

Code:
<?xml version="1.0" encoding="UTF-8"?>

Tongue


Best Regards,

Adi Sucipta Pratama
04-25-2012 01:53 AM
Visit this user's website Find all posts by this user Quote this message in a reply
terzier Offline
Member
***
Posts: 91
Joined: Feb 2012
Post: #113
RE: PHP in Templates / Complex Templates
@adisp007
thanks bro Biggrin
solved now Tongue

Un-Official MyBB from Indonesia| Facebook Page|Twitter
GARUDA DIDADA KU
04-25-2012 02:29 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Cyberuben Offline
Junior Member
**
Posts: 2
Joined: May 2012
Post: #114
RE: PHP in Templates / Complex Templates
I am trying to use PHP to call something from the database, find a SteamID, search this in another table and put it out.
This is my current code, template: (prefix)_postbit_first:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{$ignore_bit}
<a name="pid{$post['pid']}" id="pid{$post['pid']}"></a>
<table border="0" cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}" class="tborder" style="{$post_extra_style} {$post_visibility}" id="post_{$post['pid']}">
	<tbody>
		<tr>
			<td class="tcat">
				<div class="float_left smalltext">
					{$post['postdate']}, {$post['posttime']} <span id="edited_by_{$post['pid']}">{$post['editedmsg']}</span>
				</div>
				{$post['posturl']}
			</td>
		</tr>

		<tr>
			<td class="trow1 {$unapaproved_shade}">
				<table cellspacing="0" cellpadding="0" border="0" style="width: 100%;">
					<tr>
						<td class="post_avatar" width="1" style="{$post['avatar_padding']}">
							{$post['useravatar']}
						</td>
						<td class="post_author">
							<strong><span class="largetext">{$post['profilelink']}</span></strong> {$post['onlinestatus']}<br />
							<span class="smalltext">
								{$post['usertitle']}<br />
								{$post['userstars']}
								{$post['groupimage']}
							</span>
						</td>
						<td class="smalltext post_author_info" width="165">
							{$post['user_details']}
						</td>
					</tr>
				</table>
			</td>
		</tr>

		<tr>
			<td class="trow2 post_content {$unapproved_shade}">
				<span class="smalltext"><strong>{$post['subject']} | {$GLOBALS['threadfields']['b_reason_short']}<br />{$post['subject_extra']}</strong></span>

				<div class="post_body" id="pid_{$post['pid']}">
					<table cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}" class="tborder" style="{$post_extra_style} {$post_visibility}" id="post_{$post['pid']}">
						<tr>
							<td class="trow1" valign="top" width="25%"><strong>Name: </strong></td><td class="trow1" valign="top" width="75%">{$post['subject']}</td>
						</tr>
						<tr>
							<td class="trow2" valign="top" width="25%"><strong>SteamID: </strong></td><td class="trow2" valign="top" width="75%">{$GLOBALS['threadfields']['b_steamid']}</td>
					       </tr>
						<tr>
							<td class="trow1" valign="top" width="25%"><strong>Reason to be banned (short): </strong></td><td class="trow1" valign="top" width="75%">{$GLOBALS['threadfields']['b_reason_short']}</td>
						</tr>
						<tr>
							<td class="trow2" valign="top" width="25%"><strong>Reason to be banned: </strong></td><td class="trow2" valign="top" width="75%">{$GLOBALS['threadfields']['b_reason']}</td>
					       </tr>
					       <tr>
							<td class="trow1" valign="top" width="25%"><strong>Evidence: </strong></td><td class="trow1" valign="top" width="75%">{$GLOBALS['threadfields']['b_evidence']}</td>
						</tr>
						<tr>
							<td class="trow2" valign="top" width="25%"><strong>Note to moderator: </strong></td><td class="trow2" valign="top" width="75%">{$GLOBALS['threadfields']['b_note']}</td>
					       </tr>
					       <tr>
							<td class="trow1" valign="top" width="25%"><strong>Previous bans: </strong></td><td class="trow1" valign="top" width="75%">

	

<?php
$SQLquery =
"
SELECT
	b_steamid
FROM
	mybb_threadfields_date
WHERE
	tid = '" . mysql_real_escape_string($_GET['tid']) . "'
";

$resultaat = mysql_query($SQLquery) or die(mysql_error());

if(mysql_num_rows($resultaat) == 1)
	{
	$SQLquery = 
	"
	SELECT
		CONCAT('toggle[ban_', banid, ']') AS db_banid,
		name,
		steamid,
		banlength
	FROM
		excl_bans
	WHERE
		steamid	= '" . mysql_result($resultaat, 0, 'b_steam_id') . "'
	";
	
	$resultaat = mysql_query($SQLquery) or die(mysql_error());
	
	if(mysql_num_rows($resultaat) > 0)
		{
			
?>
			
			<div id="banlist">
       	<div>
            <table width="100%" cellspacing="0" cellpadding="0" align="center" class="listtable">
                <tr>
                    <td width="14%" height="16" class="listtable_top" align="center"><b>Date</b></td>
                    <td height="16" class="listtable_top"><b>Name</b></td>
                    <td width="20%" height="16" class="listtable_top"><b>Admin</b></td>
                    <td width="10%" height="16" class="listtable_top" align="center"><b>Length</b></td>  
                </tr>
            </table>
        </div>
        
 <?php
		while ($rows = mysql_fetch_assoc($resultaat))
			{
			echo "<a href=\"#\" rel=\"". $rows['db_banid'] . "\"><div>\n";
        	echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" class=\"listtable\">\n";
            echo "<tr class=\"tbl_out\" onmouseout=\"this.className='tbl_out'\" onmouseover=\"this.className='tbl_hover'\">\n";
            echo "<td width=\"14%\" height=\"16\" align=\"center\" class=\"listtable_1\">". $rows['banlength'] . "</td>\n";
            echo "<td height=\"16\" class=\"listtable_1\">". $rows['name'] . "</td>\n";
            echo "<td width=\"20%\" height=\"16\" class=\"listtable_1\">Excl</td>\n";
            echo "<td width=\"10%\" height=\"16\" align=\"center\" class=\"listtable_1\">". $rows['banlength'] . "</td>\n";
            echo "</tr>\n";
        	echo "</table>\n";
        echo "</div></a>\n";
        echo "<div id=\"ban_". $rows['banid'] . "\" align=\"center\">\n";
        	echo "<table width=\"80%\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" class=\"listtable\">\n";
            	echo "<tr>\n";
	              	echo "<td width=\"100%\" height=\"16\" class=\"listtable_top\" align=\"center\" colspan=\"2\"><strong>Ban Details</strong></td>\n";
                echo "</tr>\n";
            	echo "<tr class=\"tbl_out\" onmouseout=\"this.className='tbl_out'\" onmouseover=\"this.className='tbl_hover'\">\n";
                	echo "<td height=\"16\" width=\"20%\" class=\"listtable_1\">Name</td><td width=\"80%\" class=\"listtable_1\">". $rows['name'] . "</td>\n";
               echo " </tr>\n";
               echo " <tr class=\"tbl_out\" onmouseout=\"this.className='tbl_out'\" onmouseover=\"this.className='tbl_hover'\">\n";
                	echo "<td height=\"16\" width=\"20%\" class=\"listtable_1\">SteamID</td><td width=\"80%\" class=\"listtable_1\">". $rows['steamid'] . "</td>\n";
               echo " </tr>\n";
               echo " <tr class=\"tbl_out\" onmouseout=\"this.className='tbl_out'\" onmouseover=\"this.className='tbl_hover'\">\n";
                echo "	<td height=\"16\" width=\"20%\" class=\"listtable_1\">CommunityID</td><td width=\"80%\" class=\"listtable_1\">546354321354</td>\n";
               echo " </tr>\n";
               echo " <tr class=\"tbl_out\" onmouseout=\"this.className='tbl_out'\" onmouseover=\"this.className='tbl_hover'\">\n";
                	echo "<td height=\"16\" width=\"20%\" class=\"listtable_1\">Banlength</td><td width=\"80%\" class=\"listtable_1\">". $rows['banlength'] . "</td>\n";
               echo " </tr>\n";
        	echo "</table>\n";
		echo "</div>\n";
			}
		}
	}


?>



        


					       		</td>
					       </tr>
					</table>
				</div>

				<div class="post_meta" id="post_meta_{$post['pid']}">
				{$post['iplogged']}
				</div>
			</td>
		</tr>

		<tr>
			<td class="trow1 post_buttons {$unapproved_shade}">
				<div class="author_buttons float_left">
					{$post['button_email']}{$post['button_pm']}{$post['button_www']}{$post['button_find']}
				</div>
				<div class="post_management_buttons float_right">{$post['button_edit']}{$post['button_quickdelete']}{$post['button_quote']}{$post['button_multiquote']}{$post['button_report']}{$post['button_warn']}{$post['button_reply_pm']}{$post['button_replyall_pm']}{$post['button_forward_pm']}{$post['button_delete_pm']}
				</div>
			</td>
		</tr>
	</tbody>
</table>

For some reason the PHP doesn't work.
I get these errors:
Parse error: syntax error, unexpected $end in /home/casualbananas.com/public_html/inc/functions_post.php(645) : eval()'d code(102) : eval()'d code on line 34

Parse error: syntax error, unexpected '}' in /home/casualbananas.com/public_html/inc/functions_post.php(645) : eval()'d code(153) : eval()'d code on line 34

What am I doing wrong?

http://puu.sh/w1gJ
This is what SHOULD happen, one or multiple records (Previous bans field)


EDIT:

This problem has been solved. I accidently closed PHP after a {, because I wanted to include something that I didn't edit yet, so I wanted to place raw HTML there. Anyways, it works now!

(This post was last modified: 05-23-2012 02:53 AM by Cyberuben.)
05-22-2012 10:10 PM
Find all posts by this user Quote this message in a reply
Earl Grey Offline
Junior Member
**
Posts: 12
Joined: Nov 2011
Post: #115
RE: PHP in Templates / Complex Templates
Hey, it's me again Smile

I was wondering if you could help me out with two snippets of code as I have absolutely no idea where to start!

Basically in the postbit and the member profile page I'm looking to establish an if conditional which will look something like this:

<if the poster/member is in X usergroup>
Content specific to that usergroup here (this is not important, I can code this)
< if poster/member is in Y usergroup>
Content specific to that usergroup here (this is not important, I can code this)
</if>

So essentially I'm looking to establish conditionals in both the postbit and the member profile page that allows different content to be shown depending on whether the poster or the member's page we're visiting is in a certain usergroup.
06-04-2012 03:06 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #116
RE: PHP in Templates / Complex Templates
You can use $post['usergroup'] for postbit and $memprofile['usergroup'] for profile page.

06-04-2012 04:06 AM
Find all posts by this user Quote this message in a reply
Earl Grey Offline
Junior Member
**
Posts: 12
Joined: Nov 2011
Post: #117
RE: PHP in Templates / Complex Templates
Sorry, please excuse my ineptitude. Could you possibly help me formulate the code?

<if $mybb->$memprofile['usergroup'] == 'Administrators' then>
Content here
</if>

Have i got that way off?
(This post was last modified: 06-04-2012 04:47 AM by Earl Grey.)
06-04-2012 04:47 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #118
RE: PHP in Templates / Complex Templates
Something like this (for profile page):

Code:
<if $memprofile['usergroup'] == X then>
Content for user in usergroup 4 here
</if>

where X is a usergroup ID.


06-04-2012 04:59 AM
Find all posts by this user Quote this message in a reply
Earl Grey Offline
Junior Member
**
Posts: 12
Joined: Nov 2011
Post: #119
RE: PHP in Templates / Complex Templates
Worked perfectly, thank you Smile

Just a second question, I'm using the edit built on this mod which allows certain profile fields to be hidden to certain usergroups, below is the code:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php

$groups = array('4', '6'); // array of group ID's allowed to view this field
if ($profilefield['fid'] == FIELD_ID) // replace FIELD_ID with the fid of the field you want to hide
{
    if (in_array($mybb->user['usergroup'], $groups))
    {
        echo "
<tr>
<td><span class=\"smalltext\">
<a title=\"{$profilefield['description']}\" style=\"text-decoration: none;\">{$profilefield['name']}</a>:</span></td>
</tr>
<tr>
<td>{$code}</td>
</tr>
    ";
    }
}
else {
    echo "
<tr>
<td><span class=\"smalltext\">
<a title=\"{$profilefield['description']}\" style=\"text-decoration: none;\">{$profilefield['name']}</a>:</span></td>
</tr>
<tr>
<td>{$code}</td>
</tr> ";
}
?>


Now I have quite a lot of fields I'd like to use this on, however I'd have to repeat/else every single field. I was wondering if there was a way I could array the "if ($profilefield['fid'] == FIELD_ID)" (ie ['fid'] == 1, 2, 3, 4) to save myself a lot of hassle?

06-04-2012 09:24 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #120
RE: PHP in Templates / Complex Templates
Just look at the line with in_array

PHP Code:
if (in_array($profilefield['fid'], array(1,2,3,5,8))


My Blog
06-04-2012 10:28 AM
Find all posts by this user Quote this message in a reply


Forum Jump: