Advertisement

Accessing variables from another file

Everything to do with Joomla! 1.5 templates and templating.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Locked
Ugluth
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Sat May 08, 2010 11:51 am

Accessing variables from another file

Post by Ugluth » Tue Aug 17, 2010 3:33 pm

Hello there, i have a template of my own, and what i want to do is have another file where i can get the some variables. My secondary file is in template/lib/php/setup.php

So to include it, in my index.php i have the following line:

Code: Select all

include_once('/public_html/templates/vfxhost/lib/php/setup.php');
Inside the setup.php though i have declared some variables which seems like i'm not able to call from index.php. In setup as in index i start with:

Code: Select all

defined('_JEXEC') or die('Restricted access');
and after that i have something like:

Code: Select all

$template_width = $this->params->get('page_width');
But if i write:

Code: Select all

<?php echo $template_width; ?>
in my index.php i don't see the value of the variable as it should appear.

I'm using joomla 1.5.20 if you need any other information please let me know.
Thanks in advance!

Advertisement
Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: Accessing variables from another file

Post by Alex Dobrin » Tue Aug 17, 2010 6:16 pm

Try defining the variables with some initial values in index.php, call the other file after that.
Didn't test this. :)
My latest project - http://www.extraglaze.co.uk/

Ugluth
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Sat May 08, 2010 11:51 am

Re: Accessing variables from another file

Post by Ugluth » Wed Aug 18, 2010 9:05 am

Well the whole point for me doing this is getting my index.php a bit shorter cause at the moment its really a mess to make some changes, the variables are supposed to get the value the administrator has given in the template management page.

I'm not really sure, but i think the problem is that the setup.php isn't included any at all? I can't imagine something else going wrong, but i don't see anything wrong with my include statement either. If i do what you say it does work, but its not really what i'm looking for, thank you for your time though

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: Accessing variables from another file

Post by Alex Dobrin » Wed Aug 18, 2010 9:12 am

Your problem is generated by the variables scope ( http://www.php.net/manual/en/language.v ... .scope.php ).
Starting from that you might be able to find a solution that fits your needs.
My latest project - http://www.extraglaze.co.uk/

Ugluth
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Sat May 08, 2010 11:51 am

Re: Accessing variables from another file

Post by Ugluth » Wed Aug 18, 2010 9:32 am

All of the variables i define are of global scope, i don't define them as global but they're not in any function either, so they should be seen. I took the concept from another template, i have it pretty much identical to the way they do it in that template. He doesn't seem to use anything special to call the variables. He just references them and it works.

What troubles me is that if i use:

Code: Select all

include_once(JPATH_ROOT . '/templates/vfxhost/lib/php/setup.php');
instead of:

Code: Select all

include_once('/public_html/templates/vfxhost/lib/php/setup.php');
my page doesn't load any at all.

Hope that might give you some more info about my problem. Again thank you for your time :)

Edit: Forgot to ask, would you happen to know a way to check what files are loaded? looked for some addon but there's only for css files not for php ones.

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: Accessing variables from another file

Post by Alex Dobrin » Wed Aug 18, 2010 9:44 am

Ugluth wrote:All of the variables i define are of global scope, i don't define them as global but they're not in any function either, so they should be seen. I took the concept from another template, i have it pretty much identical to the way they do it in that template. He doesn't seem to use anything special to call the variables. He just references them and it works.
Is that template working on the same server that your template is not ?
I suspect it has the global variables on and that is not a recommended setting.
Ugluth wrote:What troubles me is that if i use:

Code: Select all

include_once(JPATH_ROOT . '/templates/vfxhost/lib/php/setup.php');
instead of:

Code: Select all

include_once('/public_html/templates/vfxhost/lib/php/setup.php');
my page doesn't load any at all.
See the exact path generated by JPATH_ROOT ( I bet it is not "/public_html" ).
Ugluth wrote: Edit: Forgot to ask, would you happen to know a way to check what files are loaded? looked for some addon but there's only for css files not for php ones.
I don't think you can check if a php file was loaded ( don't know of any way of doing it ), but you can define a function in that file and check if it was defined or not ( using "function_exists()" http://www.php.net/manual/en/function.f ... exists.php ).
My latest project - http://www.extraglaze.co.uk/

Ugluth
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Sat May 08, 2010 11:51 am

Re: Accessing variables from another file

Post by Ugluth » Wed Aug 18, 2010 10:02 am

First of all you are correct about the full path, the full path is /home/katsanto/public_html.
Now i tried what you said and it doesn't see the function i created. I'll paste the function just to make sure i didn't make a typo or something.

Code: Select all

function test() {
		echo "test";
	}
and then used:

Code: Select all

if (function_exists('test')) {
    echo "test function is available.<br />\n";
} else {
    echo "test function is not available.<br />\n";
}
on my index.php and i got that the function is not available. And i also did a:

Code: Select all

if (file_exists(JPATH_ROOT . '/templates/vfxhost/lib/php/setup.php')) {
    echo "The file exists";
} else {
    echo "The file does not exist";
}
and it says that the file exists. But still as i said earlier too, if i use that path for the include i make my page wont load at all.

Hope that helps you understand something more.

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: Accessing variables from another file

Post by Alex Dobrin » Wed Aug 18, 2010 10:14 am

function_exists('test') will return true only after you include the setup.php file ( I'm guessing you defined the function test() in it ).
You can check function_exists() and if returns false you include setup.php.

I forgot about another function that might help you. :)
include_once() - http://www.php.net/manual/en/function.include-once.php
My latest project - http://www.extraglaze.co.uk/

Ugluth
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Sat May 08, 2010 11:51 am

Re: Accessing variables from another file

Post by Ugluth » Wed Aug 18, 2010 10:19 am

Yes you are right, the function is in my setup.php file. But all this time, that is what i'm trying to do really. The problem is that the setup.php isn't actually included. At the moment i'm using

Code: Select all

include_once('/templates/vfxhost/lib/php/setup.php');
which i know is wrong after you pointed me out that this is not the full path.
But if i use

Code: Select all

include_once(JPATH_ROOT . '/templates/vfxhost/lib/php/setup.php');
which is the correct one, my site won't load any at all.

Ugluth
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Sat May 08, 2010 11:51 am

Re: Accessing variables from another file

Post by Ugluth » Wed Aug 18, 2010 11:03 am

That's it i should give up programming, while i was testing a lot of stuff to see if something works, i copied pasted the original file as it was, and the template worked, it even printed a variable from the included file! I really don't know what was wrong with my code (which i had copied and pasted) but when i copied the whole file it just worked...

I really wanna hit my head against the wall really hard at times like these. Thank you very much for your time, i guess my problem is solved, but i don't really know how.

Anyway thank you again for your effort and time :)

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: Accessing variables from another file

Post by Alex Dobrin » Wed Aug 18, 2010 11:04 am

Happy to hear you solved your problem. :)
My latest project - http://www.extraglaze.co.uk/

Advertisement

Locked

Return to “Templates for Joomla! 1.5”