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: 12-22-2015 09:33 PM by ZiNgA BuRgA.)
Find all posts by this user
Quote this message in a reply
Download: phptpl-2.1.7z (1.95 KB)
Plugin Version: 2.1
Last Updated: 12-22-2015, 09:33 PM

Downloads: 12,010
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,338
Joined: Jan 2008
Post: #201
RE: PHP in Templates / Complex Templates
Variables are written in PHP code so, unfortunately, there's no general rule other than to look through the code itself.

My Blog
04-02-2017 05:43 PM
Find all posts by this user Quote this message in a reply
L Moody Offline
Junior Member
**
Posts: 3
Joined: Jun 2017
Post: #202
RE: PHP in Templates / Complex Templates
In the EDITPOST template on a MyBB 1.8.12 board, I'm trying to create a condition where:

IF, the post being edited is the first post in the thread, the subject field can be edited

ELSE, (the post is not the first post,) the subject field cannot be edited

I've been playing around with mixed results, but not having any luck.

#################################################

This BELOW allows all posts in the thread to have the subject field edited. - the logic seems to flow down the IF path...

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
<if $thread['pid'] = 1 then>
<tr>
<td class="trow2" style="color:red; background-color: #fbe3e4"><strong>Edit Your Subject ?</strong></td>
<td class="trow2" style="background-color: #fbe3e4">{$prefixselect}<input type="text" class="textbox" name="subject" size="60" maxlength="85" value="{$subject}" tabindex="1" /></td>
</tr>

<else />
<tr>
<td class="trow2" style="color:red; background-color: #fbe3e4"><strong>Thread Subject:</strong></td>
<td class="trow2" style="background-color: #fbe3e4">{$subject}</td>
</tr>

</if>


This BELOW allows all posts in the thread to have subject field edited. - again, logic seems to follow the IF path...

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
<if $post['pid'] = 1 then>
<tr>
<td class="trow2" style="color:red; background-color: #fbe3e4"><strong>Edit Your Subject ?</strong></td>
<td class="trow2" style="background-color: #fbe3e4">{$prefixselect}<input type="text" class="textbox" name="subject" size="60" maxlength="85" value="{$subject}" tabindex="1" /></td>
</tr>

<else />
<tr>
<td class="trow2" style="color:red; background-color: #fbe3e4"><strong>Thread Subject:</strong></td>
<td class="trow2" style="background-color: #fbe3e4">{$subject}</td>
</tr>

</if>


This BELOW does not allow editing of the first or any subsequent posts. - the logic seems to follow the ELSE path...

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
<if $post['pid1'] then>
<tr>
<td class="trow2" style="color:red; background-color: #fbe3e4"><strong>Edit Your Subject ?</strong></td>
<td class="trow2" style="background-color: #fbe3e4">{$prefixselect}<input type="text" class="textbox" name="subject" size="60" maxlength="85" value="{$subject}" tabindex="1" /></td>
</tr>

<else />
<tr>
<td class="trow2" style="color:red; background-color: #fbe3e4"><strong>Thread Subject:</strong></td>
<td class="trow2" style="background-color: #fbe3e4">{$subject}</td>
</tr>

</if>


Lastly, same result here where the logic appears to follow the ELSE path...

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
<if $thread['pid1'] then>
<tr>
<td class="trow2" style="color:red; background-color: #fbe3e4"><strong>Edit Your Subject ?</strong></td>
<td class="trow2" style="background-color: #fbe3e4">{$prefixselect}<input type="text" class="textbox" name="subject" size="60" maxlength="85" value="{$subject}" tabindex="1" /></td>
</tr>

<else />
<tr>
<td class="trow2" style="color:red; background-color: #fbe3e4"><strong>Thread Subject:</strong></td>
<td class="trow2" style="background-color: #fbe3e4">{$subject}</td>
</tr>

</if>


Clearly, your plugin is functional as I can observe the logic flowing either down the IF or down the ELSE paths.

I'm just misapplying it.

Any help?

(This post was last modified: 06-30-2017 05:00 PM by L Moody.)
06-30-2017 05:16 AM
Find all posts by this user Quote this message in a reply
L Moody Offline
Junior Member
**
Posts: 3
Joined: Jun 2017
Post: #203
RE: PHP in Templates / Complex Templates
^ I updated my previous post so it's less vague. If anybody wants to take a second look, please do.

Thanks Smile
07-05-2017 06:48 AM
Find all posts by this user Quote this message in a reply
L Moody Offline
Junior Member
**
Posts: 3
Joined: Jun 2017
Post: #204
RE: PHP in Templates / Complex Templates
Bump Beg Tongue
07-28-2017 09:41 PM
Find all posts by this user Quote this message in a reply
Ashley Offline
Junior Member
**
Posts: 2
Joined: Jul 2017
Post: #205
RE: PHP in Templates / Complex Templates
Hi

If i wanted to run a script in headerinclude template, but wanted to exclude it from executing on say showthread.php, what would be the best / correct php syntax to use ?

<if !showthread.php then>
<script src="{$mybb->asset_url}/jscripts/somescript.js"></script>
</if>
07-30-2017 03:51 AM
Find all posts by this user Quote this message in a reply
nier3 Offline
Member
***
Posts: 124
Joined: Jul 2012
Post: #206
RE: PHP in Templates / Complex Templates
(07-30-2017 03:51 AM)Ashley Wrote:  Hi

If i wanted to run a script in headerinclude template, but wanted to exclude it from executing on say showthread.php, what would be the best / correct php syntax to use ?

<if !showthread.php then>
<script src="{$mybb->asset_url}/jscripts/somescript.js"></script>
</if>

Hi, I think it's:

Code:
<if THIS_SCRIPT != 'showthread.php' then><script src="{$mybb->asset_url}/jscripts/somescript.js"></script></if>

(This post was last modified: 07-31-2017 04:10 AM by nier3.)
07-31-2017 03:54 AM
Find all posts by this user Quote this message in a reply
Ashley Offline
Junior Member
**
Posts: 2
Joined: Jul 2017
Post: #207
RE: PHP in Templates / Complex Templates
^^ Thanks.
07-31-2017 04:00 AM
Find all posts by this user Quote this message in a reply
nier3 Offline
Member
***
Posts: 124
Joined: Jul 2012
Post: #208
RE: PHP in Templates / Complex Templates
I'm using "PHP and Template Conditionals" plugin but there is always an error where I add <if> conditional, for example:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /srv/users/serverpilot/apps/(appname)/public/forum/global.php(527) : eval()'d code on line 2

I'm using also PHP 7.0, maybe it isn't compatible with PHP 7.0? Frown

EDIT: nothing, sorry, I have uploaded from 2.0 to 2.1 and now it works without problems, I didn't see the update sorry Frown Frown Frown
(This post was last modified: 07-31-2017 05:40 AM by nier3.)
07-31-2017 04:10 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,338
Joined: Jan 2008
Post: #209
RE: PHP in Templates / Complex Templates
(06-30-2017 05:16 AM)L Moody Wrote:  In the EDITPOST template on a MyBB 1.8.12 board, I'm trying to create a condition where:

IF, the post being edited is the first post in the thread, the subject field can be edited

ELSE, (the post is not the first post,) the subject field cannot be edited
Unfortunately this plugin won't really enforce that.  At most, you could hide the inputs, but it won't actually stop someone editing a subject.  You'd need something more complex, like a plugin, for that.
If you don't actually care about post subjects though, you could just hide them in posts instead (or display the thread subject there instead).

My Blog
02-10-2018 10:30 PM
Find all posts by this user Quote this message in a reply
MrOSX Offline
Junior Member
**
Posts: 1
Joined: Feb 2018
Post: #210
RE: PHP in Templates / Complex Templates
Were is the download link to this?
02-17-2018 12:19 AM
Find all posts by this user Quote this message in a reply


Forum Jump: