Understanding variable scope in theme templates

Permalink 1 user found helpful
Hello friends, I'm trying to wrap my head around how variables work in a c5 theme, specifically what is the best method for defining custom variables that can in turn be reused in the various template files i.e. header.php, footer.php, etc.

In my header.php file:
$themePath = $this->getThemePath();
echo $themePath; // ouputs /path/to/my/theme/


But in my footer.php file:
echo $themePath; // outputs nada, nothing, blank


Forgive me if this is something simple, I'm still learning the in's and out's of php and I suspected there is an issue with variable scoping going on but I'm not sure how to address it.

Any kind souls who could help expand my level of awareness will be showered with appreciation :)

MrNiceGaius
 
12345j replied on at Permalink Best Answer Reply
12345j
the php variables are defined on a per file basis- a variable in one is not usable by another. (if this wasn't the case you'd have to make up really wierd variable names becaus e all the normal ones would have been used.) If you define $themePath in your footer.php then it will work again.
MrNiceGaius replied on at Permalink Reply
MrNiceGaius
Thanks, I guess I was stuck on the idea that the template files were being included and thought somehow they would share variables in a way that is similar to using a regular include(); statement.

Instead of re-declaring the variables in my footer.php can't I just put them into a separate php file and include that file into the header.php & footer.php ?
12345j replied on at Permalink Reply
12345j
yup you could do that if you have a lot of them, just make sure you don't forget. The reason it doesn't work this way is because the two files aren't included by each other, they're included by the default.php.
MrNiceGaius replied on at Permalink Reply
MrNiceGaius
Thanks Jack, a little hand holding goes a long way thank you so much for taking the time.

I'm creating a method for adding css|js to the header OR the footer with the ability to invoke custom css preprocessors and/or minify & cache the output.

IN header.php
<?php
include(my_functions.php);//helper functions (only need loading once)
include(my_variables.php);//variables (need loading per template file)
?>

IN footer.php
<?php
include(my_variables.php); // now all variables accessible in footer.php
?>

--- for example purposes
IN my_variables.php
inspired by boomgraphics header.php file found herehttp://www.concrete5.org/index.php?cID=173001...
<?php
// ### OBJECT VARIABLES ###
$area = array();//usage: $area['Header Area'] = new Area('Header');
$c = Page::getCurrentPage();
$cp = new Permissions($c);
$u = new User();
$html = Loader::helper('html/v2');
$pageName = $c->getCollectionName();
$pageHandle = $c->getCollectionHandle();
$pageID = $c->getCollectionID();
$themePath = $this->getThemePath();
$notEditMode = !$c->isEditMode();
$isEditMode = $c->isEditMode();
$youAreLoggedIn = $u->isLoggedIn();
$headerBar = is_object($cp) && ($cp->canWrite() || $cp->canAddSubContent() || $cp->canAdminPage() || $cp->canApproveCollection());

IN my_functions.php
//send css|js to Minify.php for minification via YUI Compressor & return cached file
function makeMinified(... 
... more helper functions ...


---
Then in my header.php OR footer.php files I can use the function to generate the src attributes of my style or script tags. Obviously, style tags only go into the header.
<script src="<?php echo makeMinified($themePath. '/js/plugins.js', true, true); ?>"></script>


This is working but I want to dial it in before I post here.