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,863
MyBB Compatibility: 1.2.x, 1.4.x, 1.6.x, 1.8.x
Plugin License: GPLv3
Uploader: ZiNgA BuRgA
Jem991 Offline
Junior Member
**
Posts: 3
Joined: Feb 2016
Post: #181
RE: PHP in Templates / Complex Templates
hi. I want to portal page is last treaht big order is small
I'm trying tish but don't work
<if $postcounter == 1 then>
<else>
</if>

I want this type
http://www.mangavadisi.org/demo/index.html
02-25-2016 04:05 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #182
RE: PHP in Templates / Complex Templates
Sorry, I can't understand what you're saying there.

My Blog
02-25-2016 03:18 PM
Find all posts by this user Quote this message in a reply
Jem991 Offline
Junior Member
**
Posts: 3
Joined: Feb 2016
Post: #183
RE: PHP in Templates / Complex Templates
Sorry. my english is very bad Biggrin

http://www.mangavadisi.org/demo/index.html
I want to my portal page like this but how make like this. last post big and older post little.
02-25-2016 08:58 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #184
RE: PHP in Templates / Complex Templates
I don't believe that $postcounter is available in the portal page.
You could do something like this:

Code:
1
2
3
4
5
6
<if $tplvars['not_first_ann'] then>
...
<else>
...
</if>
<setvar not_first_ann>1</setvar>


My Blog
02-29-2016 11:27 AM
Find all posts by this user Quote this message in a reply
Jem991 Offline
Junior Member
**
Posts: 3
Joined: Feb 2016
Post: #185
RE: PHP in Templates / Complex Templates
It's work thank you so much
03-02-2016 05:48 AM
Find all posts by this user Quote this message in a reply
DrXotick Offline
Junior Member
**
Posts: 2
Joined: Jul 2015
Post: #186
RE: PHP in Templates / Complex Templates
Hello, thanks for your excellent plugin. It has been an amazing addition to our forum.

1. I'm using xthreads and only want to show the extra xthread variables on pid bigger than a certain number (because the values will all be blank on older threads...

And...

Even though have selected only one specific forum for xthreads, xthread fields are showing in all forums.

We are showing xthread fields in the postbit template and within an <if> statement (see problem #1 above)

So we are now using this:

Code:
1
2
3
4
5
6
<if ($postcounter == 1) && in_array($fid, array(68)) && ($post['pid'] > 53109) then>
<hr class="style-four">
<strong>Budget: </strong>{$GLOBALS['threadfields']['budget']}<br />
<strong>Countries: </strong>{$GLOBALS['threadfields']['country']}<br />
<hr class="style-four">
</if>


And it works... Is this okay?? Or will cause some problem?

(This post was last modified: 03-28-2016 02:20 AM by DrXotick.)
03-28-2016 01:22 AM
Find all posts by this user Quote this message in a reply
TurelHD Offline
Junior Member
**
Posts: 1
Joined: Apr 2016
Post: #187
RE: PHP in Templates / Complex Templates
I am not sure if this is a good thread to write this.

How to add a myCode MyBB WYSIWYG editor to custom profile field edition page in ucp (custom profile field type text field). In MyBB 1.8.x?
04-13-2016 06:30 AM
Find all posts by this user Quote this message in a reply
Marisa Offline
Junior Member
**
Posts: 6
Joined: May 2015
Post: #188
RE: PHP in Templates / Complex Templates
Having an issue with the following code, IP simply doesn't display and code itself appears greyed out in the editor.

PHP Code:
<span>Connected via:</span> <?php echo $_SERVER['HTTP_X_FORWARDED_FOR']; ?>


Nginx and PHP7.0 aren't throwing any errors whatsoever in the logs either... Erf

(This post was last modified: 05-12-2016 09:05 PM by Marisa.)
05-12-2016 12:04 PM
Find all posts by this user Quote this message in a reply
Wage Offline
Junior Member
**
Posts: 3
Joined: Dec 2015
Post: #189
RE: PHP in Templates / Complex Templates
(05-12-2016 12:04 PM)Marisa Wrote:  Having an issue with the following code, IP simply doesn't display and code itself appears greyed out in the editor.

PHP Code:
<span>Connected via:</span> <?php echo $_SERVER['HTTP_X_FORWARDED_FOR']; ?>


Nginx and PHP7.0 aren't throwing any errors whatsoever in the logs either... Erf


PHP being grayed out in the MyBB Editor is normal behavior.
05-12-2016 09:13 PM
Find all posts by this user Quote this message in a reply
Salakiss Offline
Junior Member
**
Posts: 1
Joined: May 2016
Post: #190
RE: PHP in Templates / Complex Templates
Hello !

I'm trying to hide a custom profile field of the user profile, so i just look for help.

fid7 = is a select box required on registration but i don't want to be changed on user profile

Can anyone help me please ?



Sorry for my english, i'm french.
05-13-2016 07:02 AM
Find all posts by this user Quote this message in a reply


Forum Jump: