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,867
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: #151
RE: PHP in Templates / Complex Templates
You would need a dedicated plugin for that unfortunately.

My Blog
05-27-2013 02:16 PM
Find all posts by this user Quote this message in a reply
Jonatthu Offline
Junior Member
**
Posts: 34
Joined: Mar 2013
Post: #152
RE: PHP in Templates / Complex Templates
I have an idea for that using Rate'U´s plugin xtlastest_videos_on_profile.php

I´m not sure but if you too think that, it will be possible
05-27-2013 03:25 PM
Find all posts by this user Quote this message in a reply
golden Offline
Junior Member
**
Posts: 4
Joined: Nov 2013
Post: #153
RE: PHP in Templates / Complex Templates
Hello,
Dear ZiNgA BuRgA,

i have idea to use some code with this great plugin, but i need your help.


so:
i want to create one custom field for users , custom field type most be check box.
for example : i want to have this words for chose:
----------------------------------------
phone
car
home
shop
bike
plane
---------------------------------------
so i want upload a photo for any of them and when users chose one OR have Multiple chose . the code get info and show that photos in post-bit like there users chose.

i think its possible with getting status of any chose in that custom field, from data base. but i need help and complete code ?

Thanks
11-24-2013 12:07 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #154
RE: PHP in Templates / Complex Templates
You're not making much sense.
If English is difficult for you, please try asking someone more familiar with it to translate for you.

My Blog
11-24-2013 02:37 PM
Find all posts by this user Quote this message in a reply
golden Offline
Junior Member
**
Posts: 4
Joined: Nov 2013
Post: #155
RE: PHP in Templates / Complex Templates
maybe some, but that post have the mean of what i want...
look i want show the "my things" for any user on there user post-bit , { LIKE in VB script } OK ?

see this pictures :
http://upload7.ir/images/60509601434328168415.jpg
http://upload7.ir/images/57042321471189846513.jpg

but on mybb we can't do that easily. if we want to show any user Choice we have to ad this code in post-bit template :  

Code:
<img src="images/platforms/{$post['fidX']}.png"/>

or

Code:
<if $post['fidx'] then>
<img src="images/platforms/{$post['fidX']}.png"/>
</if>

in this case that can just show one photo and one Choice , but i need to show Multiple Choice photos

11-24-2013 06:38 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #156
RE: PHP in Templates / Complex Templates
I dunno, maybe this might work

PHP Code:
<?php
foreach(explode("\n", $post['fidX']) as $val)
  echo '<img src="images/platforms/'.$val.'.png"/>';
?>


My Blog
11-25-2013 08:16 PM
Find all posts by this user Quote this message in a reply
golden Offline
Junior Member
**
Posts: 4
Joined: Nov 2013
Post: #157
RE: PHP in Templates / Complex Templates
Thanks for Reply
that work now.
----------------------------------------------------
if possible give some style:

if there is no any Choice form user >>> so style and images all get hide :
for example:

Code:
<fieldset class="postbit">
<img src="images/platforms/'.$val.'.png"/>
</fieldset>

(This post was last modified: 11-25-2013 08:41 PM by golden.)
11-25-2013 08:39 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #158
RE: PHP in Templates / Complex Templates
HTML is something for you to figure out.

My Blog
11-26-2013 10:20 AM
Find all posts by this user Quote this message in a reply
darkhorus Offline
Junior Member
**
Posts: 1
Joined: Dec 2013
Post: #159
RE: PHP in Templates / Complex Templates
I'm a bit confused. I don't fully understand the license.

The answer is most likely no, but am I allowed to use and include this plugin into a theme I'm designing and planning to release?

Just asking, to be certain.

Regards,
darkhorus

EDIT:

So apparantly I can http://www.gnu.org/licenses/gpl-faq
(This post was last modified: 12-13-2013 05:41 AM by darkhorus.)
12-11-2013 07:29 AM
Find all posts by this user Quote this message in a reply
salar521 Offline
Junior Member
**
Posts: 1
Joined: Apr 2014
Post: #160
RE: PHP in Templates / Complex Templates
Thank you so much for taking the time to do this, I'm sure it'll greatly benefit the community.
04-04-2014 10:15 PM
Find all posts by this user Quote this message in a reply


Forum Jump: