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,661
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: #131
RE: PHP in Templates / Complex Templates
https://duckduckgo.com/?q=7z+osx

My Blog
08-19-2012 02:52 PM
Find all posts by this user Quote this message in a reply
enmanuel99 Offline
Junior Member
**
Posts: 1
Joined: Oct 2012
Post: #132
RE: PHP in Templates / Complex Templates
Hi all, sorry for my english, I speak spanish.. I want to do some like this in the menu of header template:

PHP Code:
1
2
3
4
5
6
7
8
 <div class="menu">
	<ul>
		<li><a href="index.php" <?php if ($_SERVER['PHP_SELF'] == '/index.php') { ?>class="active"<?php }?>>Foro</a></li>
		<li><a href="pagina2" <?php if ($_SERVER['PHP_SELF'] == '/portal.php') { ?>class="active"<?php } ?>>{$lang->toplinks_portal}</a></li>
		<li><a href="pagina3.php" <?php if ($_SERVER['PHP_SELF'] == '/private.php') { ?>class="active"<?php } ?>>Mensajes</a></li>								
	</ul>
</div>
 


I try to put the part of the conditional so, but it not work

PHP Code:
<?php<if $_SERVER['PHP_SELF'] == '/portal.php' then> ?>class="active"<?php </if>?>


How should be placed?
Help me please

(This post was last modified: 10-04-2012 04:01 AM by enmanuel99.)
10-04-2012 03:31 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #133
RE: PHP in Templates / Complex Templates
Remove the "<?php" and "?>" parts in the second piece of code above.

My Blog
10-04-2012 08:04 AM
Find all posts by this user Quote this message in a reply
Lilly Offline
Junior Member
**
Posts: 1
Joined: Oct 2012
Post: #134
RE: PHP in Templates / Complex Templates
Is there a way to allow php scripts to be ran in only one template only?

..for this plugin, upon activation, how does it allow php to be run in all templates?

And can we use the same method to only allow php to be run in one template only?
10-07-2012 03:18 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #135
RE: PHP in Templates / Complex Templates
(10-07-2012 03:18 AM)Lilly Wrote:  Is there a way to allow php scripts to be ran in only one template only?
What's the point in doing that?

My Blog
10-07-2012 11:47 AM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #136
RE: PHP in Templates / Complex Templates
Probably the same as here.

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 10-08-2012 04:41 AM by Sama34.)
10-08-2012 04:40 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: #137
RE: PHP in Templates / Complex Templates
If so, I fail to understand the "obvious reason".
I might guess that the person thinks it increases security or something?  It most certainly doesn't.

My Blog
10-08-2012 08:17 AM
Find all posts by this user Quote this message in a reply
someoneee Offline
Junior Member
**
Posts: 1
Joined: Dec 2012
Post: #138
RE: PHP in Templates / Complex Templates
Hello,I am using 1.9 version of this plugin,as I can see there is a change here like <if , <elseif , </func> etc.
And on 1.9 I use simple PHP syntax,if I will upgrade will I have to manually find my php codes and change them?
12-15-2012 09: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: #139
RE: PHP in Templates / Complex Templates
I don't believe so.

My Blog
12-15-2012 10:04 PM
Find all posts by this user Quote this message in a reply
Seabody Offline
Member
***
Posts: 54
Joined: Aug 2012
Post: #140
RE: PHP in Templates / Complex Templates
PHP in Templates randomly started throwing this error at me.

   

The most recent thing I did was add this code to my header_welcomeblock_moderator template:

PHP Code:
1
2
3
4
5
6
<?php
require_once("inc/functions_indicators.php");
$staff_unread = fetch_unread_count(16);
$contus_unread = fetch_unread_count(24);
$unreadmod = $staff_unread + $contus_unread;
?>


Though I fail to see how that could cause this issue, especially since I deleted it afterwards. The issue vanishes when PHP in Templates is deactivated, however doing so causes my index page to be blank.

(This post was last modified: 01-06-2013 09:27 AM by Seabody.)
01-06-2013 09:26 AM
Find all posts by this user Quote this message in a reply


Forum Jump: