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
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #171
RE: PHP in Templates / Complex Templates
I'm not sure what your first question (?) is about, but I wouldn't recommend trying to use MyCode.
For a simple group conditional, just do it in PHP.  Trying to mix stuff will either lead you to a world of pain, and probably won't work.

My Blog
09-29-2015 12:30 PM
Find all posts by this user Quote this message in a reply
Gregonmybb Offline
Junior Member
**
Posts: 3
Joined: Dec 2015
Post: #172
RE: PHP in Templates / Complex Templates
Hello, I am trying to do something. I don't think the conditionals are grabbing what I intend. I don't see this ass a problem with the code, but I may be wrong.

This is me trying to be fancy and removing my own profile fields from posts.
Postbit Classic:

Code:
<if $mybb->user['uid'] != 2 then>
 {$post['usertitle']}
 </if>


This ended up deleting all the usertitles there are.
I'd love your help making this work if you have time!

(This post was last modified: 12-17-2015 02:34 PM by Gregonmybb.)
12-17-2015 02:29 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #173
RE: PHP in Templates / Complex Templates
$mybb->user refers to the user of the person viewing the page (i.e. you).
If memory serves correct, try using $post['uid'] instead.

My Blog
12-17-2015 04:59 PM
Find all posts by this user Quote this message in a reply
Gregonmybb Offline
Junior Member
**
Posts: 3
Joined: Dec 2015
Post: #174
RE: PHP in Templates / Complex Templates
Oh! That was my problem. Thanks a lot lol. It works wondrous now.
12-18-2015 12:18 AM
Find all posts by this user Quote this message in a reply
lukasamd Offline
Junior Member
**
Posts: 1
Joined: Dec 2015
Post: #175
RE: PHP in Templates / Complex Templates
Hello!
What about PHP 7 compatibility?

There is an issues with /e modifier in preg_replace from phptpl_parsetpl function.

EDIT:
I tried rewrite code to preg_replace_callback, but it doesn't work:

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
    $ourtpl = preg_replace_callback(
        '#\<((?:else)?if\s+(.*?)\s+then|else\s*/?|/if)\>#si',
        function ($m) {
            return phptpl_if($m[1], $m[2]);
        },
        $ourtpl
    ); 
    $ourtpl = preg_replace_callback(
        '#\<template\s+([a-z0-9_ \-+!(),.]+)(\s*/)?\>#i',
        function ($m) {
            return $GLOBALS['templates']->get($m[1]);
        },
        $ourtpl
    );
    $ourtpl = preg_replace_callback(
        '#\<\?=(.*?)\?\>#s',
        function ($m) {
            return strval(phptpl_unescape_string($m[1]));
        },
        $ourtpl
    );
    $ourtpl = preg_replace_callback(
        '#\<setvar\s+([a-z0-9_\-+!(),.]+)\>(.*?)\</setvar\>#i',
        function ($m) {
            $GLOBALS["tplvars"][$m[1]] = $m[2];
            return '';
        },
        $ourtpl
    );
    $ourtpl = preg_replace_callback(
        '#\<\?(?:php|\s).+?(\?\>)#s',
        function ($m) {
            return phptpl_evalphp($m[0], $m[1]);
        },
        $ourtpl
    );

(This post was last modified: 12-19-2015 05:43 PM by lukasamd.)
12-19-2015 05:21 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #176
RE: PHP in Templates / Complex Templates
Removal of the 'e' flag is problematic because PHP doesn't really have a direct replacement.  They suggest preg_replace_callback, but it's different in a number of ways.  I've been looking into the preg_replace_callback_array function added in PHP 7, which seems like a feasible solution, but does mean that I need two versions of the code.

My Blog
12-20-2015 11:44 AM
Find all posts by this user Quote this message in a reply
eldenroot Offline
Junior Member
**
Posts: 21
Joined: Dec 2015
Post: #177
RE: PHP in Templates / Complex Templates
Template conditionals plugin is also broken with php7... I am looking for a new updated version soon, must have plugin! Thank you very much!
12-21-2015 12:09 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #178
RE: PHP in Templates / Complex Templates
Updated to v2.1 for PHP 7 compatibility.  As of this version PHP 5.3 or later is required.

My Blog
12-22-2015 09:35 PM
Find all posts by this user Quote this message in a reply
chillwithme Offline
Junior Member
**
Posts: 3
Joined: Jul 2015
Post: #179
RE: PHP in Templates / Complex Templates
but the problem is when i save this the html part is not shown. how can i fix this? any idea?

p.s: i started php with this plugin since 4 days Biggrin
(This post was last modified: 02-10-2016 09:58 PM by chillwithme.)
02-06-2016 06:01 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #180
RE: PHP in Templates / Complex Templates
I can't really guess what you're trying to do.
I recommend printing out the variables (i.e. var_dump) so you can see what they're evaluating to and narrow down from there.

My Blog
02-06-2016 09:46 AM
Find all posts by this user Quote this message in a reply


Forum Jump: