PDA

View Full Version : Problem adding and using global vars of my own


raztrip
05-22-2008, 11:40 AM
Hi,

I'm using vB 3.6.8 patch level 2.

I want to add variables for use in any and all templates. To do so, I've created a mytxtfile.txt file with the desired content (in HTML), and added the following lines at the end of global.php:

$mytxtfile = 'mytxtfile.txt';
$mycontent = file_get_contents($mytxtfile);

Then, I added $mycontent inside templates, by writing $mycontent in the template editor.

The problem is that this works with some templates, but not with others. For example, it worked with navbar and with a new template I made, but not with postbit.

1. Why?

2. Is there a better way to do this?

3. Where are custom templates (like the new one I made) stored?

Any help will be appreciated,

Thank you,

R.

Marco van Herwaarden
05-22-2008, 11:50 AM
Add the following line to your plugin:

global $mycontent ;

raztrip
05-22-2008, 11:54 AM
First, thank you. Now, I'm a total newbie. Where is my plugin (do I even have one?) and how do I add this line to it?

Add the following line to your plugin:

global $mycontent ;

Marco van Herwaarden
05-22-2008, 12:03 PM
Hmm now see that you don't use a plugin but instead edit global.php (bad choice in my view). Just add it in global.php then, just above the 2 lines you already added.

raztrip
05-22-2008, 12:07 PM
Thank you again, Marco.

- Is the global command a vB function?

- What is the better way to do this, in your experience?

Hmm now see that you don't use a plugin but instead edit global.php (bad choice in my view). Just add it in global.php then, just above the 2 lines you already added.

Marco van Herwaarden
05-22-2008, 12:18 PM
"global" is a standard PHP command that will set the variable in the global scope.

I would not do any code edits, but just put this code into a plugin, probably using the 'global_start' hook location.

raztrip
05-22-2008, 01:22 PM
I've tried this - but it still doesn't work with the postbit template. Why could that be?

"global" is a standard PHP command that will set the variable in the global scope.

I would not do any code edits, but just put this code into a plugin, probably using the 'global_start' hook location.

--------------- Added 1211547826 at 1211547826 ---------------

To anyone who's had the same type of problem, I've found a different solution. I am ignorant as to why this worked and the other way didn't, but here it is:

Instead of referencing $mycontent directly, I called it through $GLOBALS, which is a superglobal (http://il2.php.net/global). It is an array of all global-scope variables, the keys of which are the variables' names. Therefore, I was able to call $mycontent by writing $GLOBALS[mycontent].

This page (http://il2.php.net/global) helped me find this solution.