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
ahstanford Offline
Junior Member
**
Posts: 5
Joined: Apr 2010
Post: #11
RE: PHP in Templates / Complex Templates
(04-21-2010 01:10 AM)Rateu Wrote:  May I know what actually do you want to do?

I'm trying to use the <if $CONDITION then></if> conditionals to display certain links in the header/navigation to only members of certain usergroups.

All of my <if></if> and PHP code is being output to the browser - it's obvious the mod is not functional and is not parsing the code...
04-21-2010 01:27 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #12
RE: PHP in Templates / Complex Templates
Did you try this condition:

Code:
<if $mybb->user['usergroup'] == x then>
Your link here
</if>


Change the x with user group id.


(This post was last modified: 04-21-2010 01:40 AM by RateU.)
04-21-2010 01:39 AM
Find all posts by this user Quote this message in a reply
ahstanford Offline
Junior Member
**
Posts: 5
Joined: Apr 2010
Post: #13
RE: PHP in Templates / Complex Templates
(04-21-2010 01:39 AM)Rateu Wrote:  Did you try this condition:

Code:
<if $mybb->user['usergroup'] == x then>
Your link here
</if>


Change the x with user group id.


Yes.  This is not a syntax error.

If I put that in a template with 'x' replace with '1' (Guests) and view my forum logged in as Admin (shouldn't display) it still displays. If you were to view source you will see this in the source:

Code:
<if Array['usergroup'] == 1 then>
Your link here
</if>


The plugin seems to be installed wrong (maybe because it doesn't include any instructions) or not functional - for whatever reason.



EDIT: I've posted a detailed update of this issue here: http://community.mybboard.net/thread-318...http://community.mybboard.net/thread-31860-post-492659.html
(This post was last modified: 04-21-2010 03:27 AM by ahstanford.)
04-21-2010 01:48 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #14
RE: PHP in Templates / Complex Templates
Did you try it in a fresh MyBB installation?

04-21-2010 03: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: #15
RE: PHP in Templates / Complex Templates
Instructions are here - I'm just too lazy to redocument everything for all these plugins.
But from your descriptions, it does sound like the plugin isn't activated at all, although you seem to be doing the right things.  As suggested, try it on a clean install of MyBB 1.4.11-13 (haven't tested it on .13, though it shouldn't be different there) and see if it works.

My Blog
04-21-2010 12:53 PM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #16
RE: PHP in Templates / Complex Templates
I've tested in MyBB 1.4.13. It works perfectly.

04-22-2010 07:54 AM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #17
template conditionals HTML
I am trying to build a set up for a book review forum and I thought it would be cute if the replies to the book review post had the users avatar as an icon for "who posted".

I have this working fine for posts by users, but I have forum settings set so that guests can post. Of course, guests have no avatar, so I wanted to use the php/templates Conditionals plug in to put an IF statement around the avatar image - something along the lines of

<if {post is by a guest} then <show this image> else {show posters avatar}.

This is the HTML I am using to get the small avatar to show (miniav is a .css class I made for the borders/styling on the avatar). This is working fine. Its the swap for the guest image I am struggling with. I have a default avatar set up on my forum, that is the image I want to link to.

Code:
1
2
3
4
5
6
7
8
<td class="tcat">
       <div class="float_left smalltext">
	      <img src="{$post['avatar']}" class="miniav" "width="35" height="35" /><strong>
              <span class="smalltext">&nbsp;{$post['profilelink']}</span></strong> - 
	          {$post['postdate']}, {$post['posttime']}<span id="edited_by_{$post['pid']}"></span>
	</div>
	          {$post['posturl']}
</td>


Here is a link to an example:  http://www.leefish.nl/mybb/showthread.php?tid=33 Once this is all polished up I hope to post it here in the XThreads examples forum - so I want it to be REALLY nice Smile

I don't know which variable to call. Can anyone help?



[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 06-12-2010 04:47 AM by leefish.)
06-12-2010 04: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: #18
RE: template conditionals HTML
Try something like this:

Code:
<if $post['avatar'] then>
<img src="{$post['avatar']}" class="miniav" "width="35" height="35" />
<else>
<img src="image_path_for_guest_avatar" alt="" title="" />
</if>


06-12-2010 05:11 AM
Find all posts by this user Quote this message in a reply
leefish Offline
Hamster
*****
Posts: 1,009
Joined: Apr 2010
Post: #19
RE: template conditionals HTML
Thank you RateU. Indeed, I was thinking along the lines of <ïf poster was guest> but I see you cut the middle man out and went straight for <if the image defined as an avatar is there then blah blah>. I see.


[Image: leelink.gif]
MYBB1.6 & XThreads
(This post was last modified: 06-12-2010 05:30 AM by leefish.)
06-12-2010 05:29 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: #20
RE: PHP in Templates / Complex Templates
I merged the thread Smile

Yes, the code not only for guest, but for member who doesn't have avatar too. You can use $post['uid'] if you really want it for guest only.

06-12-2010 05:35 AM
Find all posts by this user Quote this message in a reply


Forum Jump: