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
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #31
RE: PHP in Templates / Complex Templates
Nice, thanks zinga!
08-15-2010 01:51 AM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #32
RE: PHP in Templates / Complex Templates
Thanks for updating this for MyNetwork users ZiNgA BuRgA, its really cool.


[Image: leelink.gif]
MYBB1.6 & XThreads
08-15-2010 02:21 AM
Visit this user's website Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #33
RE: PHP in Templates / Complex Templates
Hallo, and back again. I was not sure where to post this as its a question about a plugin file I got from another thread on this board. This is the thread and the plugin:

http://mybbhacks.zingaburga.com/showthre...http://mybbhacks.zingaburga.com/showthread.php?tid=415&pid=29

The plugin adds Xthreads images to profile - its super cool.

Here is a link to the plugin in action so its clearer what I am talking about:

http://www.leefish.nl/mybb/member.php?ac...http://www.leefish.nl/mybb/member.php?action=pro

I was trying to add a small icon above the image in the profile display. Ideally the icon of the game it is for based on the relevant xthreads field I chose for it. Smile

Anyway, that was a bit hard so I decided to try and make it have the word Download above the picture in those forums where I have downloads (forums 7 and 29) and Find in those areas I have finds. The reason I need to make it optional rather than just using the plug in is because I have other users who have contributed to the forum but don't make downloadable sims stuff.

I tried using php in templates in the mnxtigprofile_images_image to do this:

Code:
<if in_array($mnxtigps_forum->forum['fid'], array(7,29)) then>DOWNLOAD <elseif> in_array($mnxtigps_forum->forum['fid'], array(30)) then>FIND</if>



Here is the if function in my template:

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
<div style="float: left; height: 250px; padding: 1px; max-height: 250px;">
<table class="trow2" align="center" style="border: 1px solid;" width="185" height="248">
<tbody>
<tr>
<td align="center">
<table align="center">
<tbody>
<tr>
<td align="center">
	<span class="smalltext">
                 <div class="smalltext">
<if in_array($mnxtigps_forum->forum['fid'], array(7,29)) then>DOWNLOAD <elseif> in_array($mnxtigps_forum->forum['fid'], array(30)) then>FIND</if>
                 </div>
	<a href="{$mnxtigps_links}"><img src="{$mnxtigps_img}/thumb160x120" alt="{$mnxtigps_subject}" title="{$mnxtigps_subject_text}" /></a></div>
	<br />

        <a href="{$mnxtigps_links}">{$mnxtigps_subject}</a><br />
		In: {$mnxtigps_forum}<br />
                    {$mnxtigps_views} Views<br />
         </span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>


but that did not work .... I tried taking the <elseif> piece out to see if that was the problem but it still did not work.

Any idea what  am doing wrong?



[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 08-17-2010 02:39 AM by leefish.)
08-17-2010 02:35 AM
Visit this user's website Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #34
RE: PHP in Templates / Complex Templates
Maybe something like this:

Code:
1
2
3
4
5
6
7
<if in_array($mnxtigps['fid'], array(7,29)) then>
<div>Download</div>
<elseif $mnxtigps['fid'] == 30 then>
<div>Finds</div>
<else>
<div>Images Gallery</div>
</if>


Another solution maybe, you can use image, and rename the images based on the forum id. e.g: 7.png, 29.png and etc. Then put the image tag in the template.


08-17-2010 04:21 AM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #35
RE: PHP in Templates / Complex Templates
Hi RateU, thank you - I see I was overcomplicating things as usual. The images is a good idea - I shall try that.


[Image: leelink.gif]
MYBB1.6 & XThreads
08-17-2010 04:46 AM
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #36
RE: PHP in Templates / Complex Templates
Updated to v1.7, just adds a few shortcut functions.  This is mainly to prepare for the Template Conditionals plugin.

My Blog
09-15-2010 02:35 PM
Find all posts by this user Quote this message in a reply
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #37
RE: PHP in Templates / Complex Templates
Thanks for the update!
09-16-2010 12:44 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #38
RE: PHP in Templates / Complex Templates
Thanks you very much for the update, Yumi.

09-17-2010 04:46 AM
Find all posts by this user Quote this message in a reply
mike22 Offline
Junior Member
**
Posts: 1
Joined: Oct 2010
Post: #39
RE: PHP in Templates / Complex Templates
ZiNgA, this is brilliant... it really allows you to make your own customisation

But I've installed it, with no other plugins installed and I'm getting the error:
showthread.php(1195) : eval()'d code on line 60

This only happens on the showthread pages, and my version of MyBB is up to date (MyBB 1.6).  This happens without me even trying to change my templates, all I did was activate the plugin

EDIT: DOW!!!!    MY BAD... I had some rubbish < $stuff > in there that I was testing with.. sorry, work like a dream now, CHEERS!
(This post was last modified: 10-04-2010 03:56 AM by mike22.)
10-04-2010 03:36 AM
Find all posts by this user Quote this message in a reply
x2srj Offline
Junior Member
**
Posts: 1
Joined: Oct 2010
Post: #40
RE: PHP in Templates / Complex Templates
fixed issue! my fault sorry!
(This post was last modified: 10-07-2010 11:34 PM by x2srj.)
10-07-2010 11:23 PM
Find all posts by this user Quote this message in a reply


Forum Jump: