Help With Including Files in index.template.php

Started by motumbo, May 07, 2006, 02:12:09 PM

Previous topic - Next topic

motumbo

(I am using 1.1 RC2, with the relevant updates and patches)

What I've done is strip away everything from index.template.php that I don't need for my theme and I've incorporated things like my own custom header, a navigation bar, footer, etc., to match my website.

What I decided to do was redesign my site in php and use "include" statements in my webpages and forum index.template.php so all I have to do is make one change to the included file and the change occurs across my entire website and forum.  It's fast and convenient to make changes this way.

I have it set up currently as follows:

webvariables.php - this is my file that includes the paths to all the included files.  The example of the contents of this file:

$header = absolute path to my header html file
$footer = absolute path to my footer html file
$navbar = absolute path to my navigation bar html file
and more

What I do in the template.index.php is include webvariables.php using the absolute path of the file, then when I want to incorporate one of the included files, I just put in a statement like "include $header;", and my header is included in the output.

The problem is that the scope of the functions is such that I have to include webvariables.php in EVERY function in index.template.php.  I do not want to do that.  What I want to do is have a central location where I can include webvariables.php and have the contents (which are the variables mentioned above) available throughout the entire index.template.php and, if possible, throughout the entire project.  I want to have only one place where I need to include webvariables.php instead of all over the place in template.index.php.

I hope you understand what I'm trying to do and can offer some suggestions how I can include webvariables.php without having to have a include statement in every function in the template.

Also, is there an easier or better way of doing what I want to do?  If so, please let me know. 

Thank you!



Rudolf

My suggestion would be:
in webvariables create an array with the paths, then use a require in your template file, out side of any function.
Then just declare the $paths array as global in every function.

Like:
webvariable.php:

<?php
$paths 
= array('header' => '/path/to/header.html''navbar' => '/path/to/navbar.html');
?>



index.template.php:

<?php
require(webvariables.php);

function 
my_header()
{
global 
$paths;

#include $paths['header'];
}
?>


Though if you ask me it's an ugly solution.
Rudolf
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

Rudolf

OK, forget that, it was just a brain fart.
Better do this:
webvariable.php

<?php
$header 
'/path/to/header.html';
$navbar '/path/to/navbar.html';
?>


and in index.template.php

<?php
require('webvariable.php');
...
function 
myfunction()
{
echo 
$GLOBALS['header'];
include(
$GLOBALS['navbar']);
//or anything else you want to do with them
}
?>



This way you don't need to include anything in the functions.
Rudolf
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

motumbo

What I tried is defining a constant in index.php (the main index file).  This makes the constant a true global variable (constant), available across the entire project.  (I'm no expert in php, but I would have thought that declaringing a variable "global" would make it global across the entire project!)

In index.php:

define ('myinclude', 'absolute path')

In index.template.php:

template_main_above()
.
.
.
include myinclude;
include $header; (Includes the header html file and outputs the HTML contained within).
include $navbar; (Includes the navigation panel on the lefthand side).

Then:

template_main_below()
include myinclude;
include $footer;

My original goal was to avoid having to include the absolute pathname in main_above and main_below.  I achieved that goal, even if I have to include 'myinclude' in both functions.  At least if I want to modify the path to webvariables.php, all I have to do is change the "define" in index.php.  Thus it is in one spot.

I'm sure there is a better way to do this.  Please suggest if you know how.  Thanks.

Rudolf

You could do the same as it's done the $scripturl.
You could define in the Settings.php the $header etc paths, then declare them as global in the functions you want to use it.
Just like it's done the $scripturl.
Though my second suggestion should do the trick.
Rudolf
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

Advertisement: