Use Variables in Templates

by Coil - 25-07-2015, 03:31 PM
Junior Member
Jul 2015
2 Years of Service
Posted: 25-07-2015, 03:31 PM (This post was last modified: 25-07-2015, 03:39 PM by Coil.)
This tutorial is to help some of you create better* MyBB themes. As any normal thread, you can ask questions if you have any, however, I shan't be responding to any off topic comments or questions.

*not design wise.

This requires a brain, although knowledge of Php, and MyBB in general is rather useful and recommended.

Quick note to all of you using plugins for something stupid like a "posted by" in the showthread; You're stupid not the brightest fish in the sea.

You can reference variables from within MyBB templates, but only if said variables is defined in the file you are referencing it from. For example, I can call thread['username'] (thread poster's username) in showthread.php, however, I can't call it from index.php (because it's not defined in the index). This is basic knowledge, and if you can't grasp this, I don't know what to tell you.

Watch your language: Variables defined in Php will start with a dollar sign. Also, all lines will end with a semicolon.
PHP Code:
Don't fuck up: Modifying the core files of MyBB is only something that you are held responsible for, IF you decide, for any reason, to modify these files, I will NOT help you fix them. You are responsible for your own damage to your own board.

Typically, MyBB will look for GET requests for specific actions (e.g. member.php?action=profile&uid=1). If you are looking for specific variables within a template, you will want to look for variables defined only if the action is the same as the one for the current template you are modifying. If you are working in the member_profile template (member.php?action=profile&...), there is a conditional for the profile action specifically.

[Image: lZicSMM.png]

Watch your language: Some variables use conditionals to be defined, if a variable is only present if a specific condition is met, it's in your best interest not to call this variable.
Don't fuck up: It is in your best interest to call variables that are strings or integers. Calling a query will make query the database twice with the same query.

Once you've found a variable that you would like to use in a template (in the following examples we will be using the avatar variable in member.php), you will need to call it with curly braces and a dollar sign (duh).

Here is our example in the member.php file:

[Image: JdjcGBB.png]

(The following is useful if you allow users to use any size profile image over the default, but want their profile images to not take up 85% of their screen.)

Rather than just calling the avatar variable (which defaults to the users height and width for their profile image), we will call the avatar url itself, as well as the height and width of the image. We will use the style attribute in our image tags to set a max width and max height. We will call them like this:

[Image: EIx4qua.png]

You may notice a few things, one this is MyBB 1.6, and two the avatar dimensions have weird numbers after them. We can find specific items in an array using these "weird numbers". Computers start from 0 instead of 1, so our first value (the width) will be 0, and our second (the height) will be 1.

Watch your language: I won't be explaining what these "weird numbers in braces" are in this tutorial, for the sole reason I don't want to confuse people and have to explain much more than needed (that's why we're not calling them by name). Read the Php documentation if you really care about them.

Now then, you are free to move on your own, however, don't fuck up, if you call variables that aren't defined they won't be displayed on your page.

If there are any grammatical errors in this tutorial (or any other kind of error for that matter), it's because I am writing this at 06:30 and am barely able to think after a night of programming, webdesign, lots of Dr Pepper, and gaming. If you really feel the urge to correct me on something, go ahead, but if it's something minor you might be smack-a-doodle-dooed.

P.S. Hooray for my first informative thread on here.
P.P.S This is NOT an in-depth tutorial, lots of details were left out, especially not to confuse new users and aspiring theme designers. You also might be smack-a-doodle-dooed if you decide to put more depth than needed. Though probably not, depth can be nice.
P.P.P.S Works with MyBB 1.8 as well, since they're both written in Php.
26-07-2015, 08:42 PM
The last reply on this thread is older than a month. Please do not unnecessarily bump it.
Register an account or login to reply
Create an account
Create a free account today and start posting right away. It only takes a few seconds.
Log into an existing account.
1 Guest(s)