MyBB Community Forums

Full Version: PHP in Templates and Template Conditionals
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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>

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:


<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"; ?>

More info & Download
(it's easier to keep one location up to date Toungue)
Huh... this is very useful.

// We can insert javascript code in templates... so, why not some php code ?
Thank you so much. Smile

//Later: I think this should be included in the 1.4 version.
amazing Release Big Grin

Thanks Zinga
So this works without any hooks?
labrocca Wrote:So this works without any hooks?
Explicitly, no.

Gaining control of objects is a way to implicitly get "hooks".
I suck at objects..I just spent the day working on them though and learned a few things. I hope to be gain better OOP knowledge this upcoming year. It's the future of php of course.

Thanks for response Zinga.
thx zinga, you're beating one of the biggest weaknesses in mybb with this...
Great Plugin! I have found it every useful.
eval does run slower, primarily due to the fact that it can't precompile/cache the PHP code, but since all of this is eval'd anyway, it's not too much of a difference. eval does cause some overhead when you put an extra layer on it though. Also, ob_* functions are used, which isn't the most efficient either.

This plugin is mainly for convenience purposes. As stated, if someone does manage to gain access to the AdminCP (or modify the templates table), they've got the ability to execute arbitrary code. Plus it's recommended to stick PHP in the .php files rather than templates (for speed and security).
How to put specific chunk of code....say "Test:Test" using gid & conditionals!?

Can I actually use GID in templates?
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20