MyBB Community Forums

Full Version: The missing link between .php files and templates
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm doing some relatively intensive modifications to a mybb forum, but I could use a little help in figuring out something that is puzzling me--

In template files, placeholders such as
{$welcomeblock}
are (by a php parser, I presume) replaced with the actual code for that section.

What I want to know -- in detail -- is how that happens.
Where are these various placeholders defined?
What files define which placeholders are loaded/available for which pages?
How can I define new placeholders and then load them in my modified template files?

(The particular problem I'm trying to work on now is that I would like the user's avatar to display in the welcomeblock. To do that, I'll need something like {$user_avatar}, I presume, but so far, I haven't been able to find any existing placeholder that works in that location. I'm guessing I need to modify a .php file or two so that the {$urltoavatar} placeholder is loaded to global.php rather than usercp.php... but I don't know how to do that yet.)
At any rate, I expect this problem to come up again and again from time to time as I continue to edit, so I'd like to learn how to handle these placeholders myself, rather than just getting a quick and easy solution to that one example of the issue.
(So please, no "This plugin puts the avatar on the welcome block" I want to know how to handle this myself.)

Oh, and the url of my (in development) mybb board is http://www.bronyville.org/mybbtest/ if for some strange reason you feel the need to see what I'm talking about.
any variable in a template, as identified by the {$varname}, will be parsed based on its contents, if it is in scope. all the template system does is run an eval( ) on the template code. Since the vars are wrapped in braces, the eval function can do an on the fly replacement of the variable with the variable contents.

Its not really a php parser,s o you can not use {2+2} and get a value of 4 in the template output. It is nothing more than

$this_html = "something goes here and your name is {$name}";

type php syntax. If you want real php parsing in templates (like wordpress) then you need PHP in Templates plugin or the lightweight Template conditionals plugin for simple php constructs
(2013-03-06, 04:51 PM)pavemen Wrote: [ -> ]any variable in a template, as identified by the {$varname}, will be parsed based on its contents, if it is in scope.
Ah, okay then.
In that case, what I need to know is how to get any given variable to be within any given scope. IE, how to get the {$urltoavatar} variable to be within the global scope rather than usercp.php scope.

I don't really need php in templates; I'd rather learn how to properly edit the existing php files and use the existing system to get the finished variables into the template.

This is very helpful though... So, if I went into global.php and defined
$user_avatar = "<img src=" . $user['avatar'] . " />"
or some such, then all I would then have to do would be to place {$user_avatar} where I wanted it in the template, right?

*edit*
Looking through global.php, I'm finding things like $mybb->user['uid']
So, my code to load the variable should be something like,
$user_avatar = "<img src='" . $mybb->user['________'] . "'>"
Right?

Just need to figure out what goes in the blank.

Well, I added
$wburltoavatar = "test";
to global.php

and added
 <img src="{$wburltoavatar}">
to the welcomeblock_member template

...but the {$wburltoavatar} gets replaced with a blank string when the page is displayed, not 'test' as expected.
The 'view page source' code generated thusly:
<img src="">

Why isn't that working?
Do I need to define the variable somewhere other than global.php?
It works if you add,

$wburltoavatar = "test";

to index.php/member.php/anything.php
(2013-03-07, 06:29 AM)EGman Wrote: [ -> ]It works if you add,

$wburltoavatar = "test";

to index.php/member.php/anything.php
Oh, so I can't add it to global.php, instead I'll need to add it to every directly called php file?
I don't really know/neither i ever tested, to define/use a VARIABLE in global.php. But I have tried using such things in my forumdisplay.php & index.php. Their they work fine Smile
you need to define the variable someplace in the code that is prior to the call to the template. It sounds like you want to put an avatar in the header, so global.php is the place to define it, but it needs to be before the header output is generated.

also, there are plenty of global scope variables and objects that you can use, such as $mybb object. It has a user variable (and array actually).

I would suggest that you create a new file in the root of your mybb with the following code

<?php
define("IN_MYBB", 1);
require_once "./global.php";
echo "<pre>";
print_r($mybb);
echo "</pre>";
echo "<pre>";
print_r($cache);
echo "</pre>";
echo "<pre>";
print_r($templates);
echo "</pre>";
echo "<pre>";
print_r($lang);
echo "</pre>";
echo "<pre>";
print_r($session);
echo "</pre>";
echo "<pre>";
print_r($db);
echo "</pre>";
?>

this will output all the primary objects that are available by default. Example you will see $mybb->user, which holds all the details of the current user whether registered or guest.

$cache will show all the in memory data that is available (high use/common data) so you can use that instead of running queries, etc

These items contain both variables and functions as they are a class object.
(2013-03-07, 02:53 PM)pavemen Wrote: [ -> ]you need to define the variable someplace in the code that is prior to the call to the template. It sounds like you want to put an avatar in the header, so global.php is the place to define it, but it needs to be before the header output is generated.
Ah, that must be what I did wrong. I defined it at the end of global.php
I guess I should try defining it near the beginning. That may work much better.
Quote:also, there are plenty of global scope variables and objects that you can use, such as $mybb object. It has a user variable (and array actually).

I would suggest that you create a new file in the root of your mybb with the following code

<?php
define("IN_MYBB", 1);
require_once "./global.php";
echo "<pre>";
print_r($mybb);
echo "</pre>";
echo "<pre>";
print_r($cache);
echo "</pre>";
echo "<pre>";
print_r($templates);
echo "</pre>";
echo "<pre>";
print_r($lang);
echo "</pre>";
echo "<pre>";
print_r($session);
echo "</pre>";
echo "<pre>";
print_r($db);
echo "</pre>";
?>

this will output all the primary objects that are available by default. Example you will see $mybb->user, which holds all the details of the current user whether registered or guest.

$cache will show all the in memory data that is available (high use/common data) so you can use that instead of running queries, etc

These items contain both variables and functions as they are a class object.
[Image: star_eyed.jpg]
This pleases me!

Thanks for that great bit of advice! I'll definitely do that, and then I can use the already pre-defined variables like a pro instead of fumbling around in the dark! Thanks!
dont forget that MyBB has an excellent plugin system and there may be a plugin already out there that someone has made that does what you need.

the plugin system is preferred over editing the core files as editing core files makes upgrades much harder.

if you must edit core files, there is a good plugin called Patches that while itself is a plugin to the ACP, it will perform edits to core files for you and it stores the edits to be easily reapplied after an upgrade.
(2013-03-07, 04:40 PM)pavemen Wrote: [ -> ]dont forget that MyBB has an excellent plugin system and there may be a plugin already out there that someone has made that does what you need.
Perhaps, but I've got a lot of changes to make. This is just the beginning.
At some point, I'm going to have to get my hands dirty and do some actual coding... might as well start now.
Quote:if you must edit core files, there is a good plugin called Patches that while itself is a plugin to the ACP, it will perform edits to core files for you and it stores the edits to be easily reapplied after an upgrade.
Ooh... that may be a good idea.
Looks like it'll be a bit of a pita to initially make the changes that way, but it may make things easier later on. Guess I should do it that way.

Great success!

I used that 'patches' plugin to add
//Load variable for displaying user avatar on welcome message
//$wburltoavatar = "test";
if($mybb->user['avatar'] != "")
{
	$wburltoavatar = htmlspecialchars_uni($mybb->user['avatar']);
}
else
{
	$wburltoavatar = "http://www.bronyville.org/images/no_avi.png";
}
Just after the global_start plugin hook.

Then, my {$wburltoavatar} variable in the template started working beautifully. ^.^

Thanks for all your help!
Pages: 1 2