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: 12-22-2015 09:33 PM by ZiNgA BuRgA.)
Find all posts by this user
Quote this message in a reply
Download: phptpl-2.1.7z (1.95 KB)
Plugin Version: 2.1
Last Updated: 12-22-2015, 09:33 PM

Downloads: 13,080
MyBB Compatibility: 1.2.x, 1.4.x, 1.6.x, 1.8.x
Plugin License: GPLv3
Uploader: ZiNgA BuRgA
Gerti Offline
Junior Member
**
Posts: 5
Joined: Jun 2020
Post: #221
RE: PHP in Templates / Complex Templates
I create in "ACP > Configuration > Help Pages > a new help page" with the following content:

PHP Code:
1
2
3
4
5
6
7
8
Here comes the content of the test_page.php:
Test 1 = Start with Path:
<?php include ("./folder/test page.php"); ?>
End.
 ||| 
Test 2 = Start with URL/Path:
<?php include ("https://url/folder/test_page.php"); ?>
End.

The content of the test_page.php is not displayed. It will be displayed:
Here comes the content of the test_page.php: Test 1 = Start with Path: End. ||| Test 2 = Start with URL/Path: End.

The above page definition integrated in "ACP > Templates & Styles > any template" works.

What am I doing wrong?

(This post was last modified: 06-22-2020 05:53 PM by Gerti.)
06-22-2020 05:44 PM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,349
Joined: Mar 2010
Post: #222
RE: PHP in Templates / Complex Templates
Oh... The plugin name is PHP in Templates, so it only works in templates.

06-23-2020 02:45 AM
Find all posts by this user Quote this message in a reply
Gerti Offline
Junior Member
**
Posts: 5
Joined: Jun 2020
Post: #223
RE: PHP in Templates / Complex Templates
so i told you! Jap

Is there (or would anyone have) a solution how to implement "php-include" in help_pages?
(This post was last modified: 06-23-2020 06:30 PM by Gerti.)
06-23-2020 05:16 PM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 483
Joined: May 2011
Post: #224
RE: PHP in Templates / Complex Templates
You could achieve this with a simple plugin (I'd use something like Hooks or Patches to avoid a plugin for this). In misc.php find:

PHP Code:
$plugins->run_hooks("misc_help_helpdoc_end");


Add after:

PHP Code:
$templates->cache['helpdoc_'.$helpdoc['hid']] = $helpdoc['document'];

$document = eval($templates->render('helpdoc_'.$helpdoc['hid']));


Then, in your misc_help_helpdoc template search {$helpdoc['document']} and replace with {$document}.

BTW, nice to see you around @RateU!


Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 06-24-2020 11:38 AM by Sama34.)
06-24-2020 11:38 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Gerti Offline
Junior Member
**
Posts: 5
Joined: Jun 2020
Post: #225
RE: PHP in Templates / Complex Templates
It works as desired.
Many thanks for the solution.
06-24-2020 06:38 PM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,349
Joined: Mar 2010
Post: #226
RE: PHP in Templates / Complex Templates
(06-24-2020 11:38 AM)Sama34 Wrote:  BTW, nice to see you around @RateU!

Nice to see you too Smile

06-25-2020 03:44 AM
Find all posts by this user Quote this message in a reply


Forum Jump: