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,659
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: #141
RE: PHP in Templates / Complex Templates
I'm not sure how you managed to get that to show up.
It may have helped if you posted a full template too, but it doesn't look like header_welcomeblock_moderator is the problem.  Open up your global.php file and look at line 527 - what template is referred to there?

If you could post up the full template it would be helpful.  It's likely something to do with <if> tags.

My Blog
01-06-2013 09:56 AM
Find all posts by this user Quote this message in a reply
Seabody Offline
Member
***
Posts: 54
Joined: Aug 2012
Post: #142
RE: PHP in Templates / Complex Templates
I did check that, it's the header template:

PHP 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
30
31
32
33
34
35
36
37
38
39
40
<script>
function blankit() {
    document.getElementById("search_box").value="";
}
function unblankit() {
    var doc = document.getElementById("search_box").value;
    if (doc == "") {
    document.getElementById("search_box").value="Enter a Search Term, then press Enter";
    }
}
</script>
<div class="menu">
	<br />
			<div id="panel">
				{$welcomeblock}
			</div>
</div>
<div id="container">
		<a name="top" id="top"></a>
		<div id="header">
			<br />
			<div class="logo"><a href="{$mybb->settings['bburl']}/index.php"><img src="http://i1081.photobucket.com/albums/j348/Seabody/seabtechlogo.png" class="logo" border="0" alt="Seabody Technologies" style="padding:0px;" /></a></div>
			<div id="search">
                                <form method="post" action="{$mybb->settings['bburl']}/search.php" style="float:right;">
                                        <input type="text" id="search_box" name="keywords" style="width:325px;" value="Enter a Search Term, then press Enter" onfocus="blankit()" onblur="unblankit()">
                                        <input type="hidden" value="do_search" name="action">
                                 </form>
                        </div>
                        <hr class="hidden" />
		</div>
		<hr class="hidden" />
		<br class="clear" />
		<div id="content">
			{$pm_notice}
			{$bannedwarning}
			{$bbclosedwarning}
			{$unreadreports}
			{$pending_joinrequests}
			<br />
			<if THIS_SCRIPT != 'index.php'><navigation><else></if><br />


I figured it was the <else> clause, so I removed it and the error vanished - but the entire header vanished.

WTF? I removed the entire <if> clause, and the board functionality resumed. I mean, the header appeared, and the navigation didn't appear on the index.

Did a bit of poking and I had that same <if> clause located within the nav template. So I assume that PHP in Templates (likely) doesn't support nested PHP, is this correct?

(This post was last modified: 01-06-2013 10:37 AM by Seabody.)
01-06-2013 10:32 AM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #143
RE: PHP in Templates / Complex Templates

PHP Code:
<if THIS_SCRIPT != 'index.php'>


Shouldn't be as follows?

PHP Code:
<if THIS_SCRIPT != 'index.php' then>


I think you can remove the <else> part lke this:

PHP Code:
<if !(THIS_SCRIPT == 'index.php') then><navigation></if>


Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
01-06-2013 11:23 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Seabody Offline
Member
***
Posts: 54
Joined: Aug 2012
Post: #144
RE: PHP in Templates / Complex Templates
(01-06-2013 11:23 AM)Sama34 Wrote:  

PHP Code:
<if THIS_SCRIPT != 'index.php'>


Shouldn't be as follows?

PHP Code:
<if THIS_SCRIPT != 'index.php' then>


I think you can remove the <else> part lke this:

PHP Code:
<if !(THIS_SCRIPT == 'index.php') then><navigation></if>


-.- Of course. I must've accidentally removed it when I was failing to add an additional clause in there.
01-06-2013 12:06 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #145
RE: PHP in Templates / Complex Templates
Ah, I see how you did it.  Thanks for posting the template.

My Blog
01-06-2013 01:30 PM
Find all posts by this user Quote this message in a reply
XPEric Offline
Junior Member
**
Posts: 1
Joined: Apr 2013
Post: #146
RE: PHP in Templates / Complex Templates
How would I go about hiding something if the user is logged in?
04-12-2013 12:17 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #147
RE: PHP in Templates / Complex Templates

Code:
<if !$mybb->user['uid'] then>
stuff
</if>


My Blog
04-12-2013 02:55 PM
Find all posts by this user Quote this message in a reply
Jonatthu Offline
Junior Member
**
Posts: 34
Joined: Mar 2013
Post: #148
RE: PHP in Templates / Complex Templates
How can I put a recount of threads in a "x" forum in users profile?

for example:

"Profile of some user"

Videos: 15 (The videos forum is fid=4)
Photos: 13 (The pictures forum is fid=3)
Posts: 169 (Fid= 5,6,7,7,8,8,9,3)
05-26-2013 08:42 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #149
RE: PHP in Templates / Complex Templates
It's not really something for PHP in Templates to do unfortunately.

My Blog
05-27-2013 09:28 AM
Find all posts by this user Quote this message in a reply
Jonatthu Offline
Junior Member
**
Posts: 34
Joined: Mar 2013
Post: #150
RE: PHP in Templates / Complex Templates
It will be great if that were possible :/ Is there another method?
05-27-2013 01:41 PM
Find all posts by this user Quote this message in a reply


Forum Jump: