This should be extremely simple, but actually has some larger architectural implications. I come from a tiles and dynamo droplets background which allows you to pass parameters into include files. PHP handles that a bit differently, so the following requirements become tricky:
Must be able to pass in content to the head tags for specific pages.
- For instance, if the contact.php page has a email.js file that needs to be included and the index.php doesn’t, there is a way to pass this into header.php and print it out in the head tag.
All tags closed w/in each include file.
- …not sure this is possible, but I get warnings in phpEclipse if tags aren’t close.
- An example is the body tag which would be opened in header.php and closed in footer.php.
Top navigation highlights current page you’re on.
- For instance, if you’re on the contact.php, that button in the topnav gets a highlite state.
Currently, I’m not using any major frameworks or templating systems, I just have a section on the top of each page for setting up data so the markup below it is as simple as possible.
Possible Solutions:
using method(s) to set data and print out header accordingly. This allows you to pass in params. (I try to avoid printing out html markup in methods b/c I find it to be messy and complicates the code)
setting variables about include statement that header include picks up and uses. (always feels a bit hackish)
using some other framework that I’m unaware of?
Any thoughts, ideas or solutions would be greatly appreciated!
And in PHP, the usage would be something like this:
$tpl = new Template('Wrapper.tpl.php');
$tpl->title = 'my super duper page';
$tpl->menu = new Template('Menu.tpl.php');
$tpl->menu->title = 'My Menu Title';
$tpl->content = new Template('Content.tpl.php');
echo $tpl;
Where the Template class would be something like:
class Template {
private $file = null;
private $data = null;
function __constructor($pFile) {
$this->file = $pFile;
}
public function __set($key, $value) {
$this->data[$key] = $value;
}
public function __get($key) {
return $this->data[$key];
}
public function __toString() {
extract ($this->data);
ob_start ();
include ($this->file);
$contents = ob_get_contents ();
ob_end_clean ();
return $contents;
}
}
I like the second method because you can control what data gets passed where, you can cache components easier, and your HTML blocks are complete (so you can use them anywhere without having to worry about what HTML makes up the page your adding them to.) It’s also easier to use from front controllers, or chain controllers.
But, the first method is allot simpler and faster.
Or maybe use a client side app like Dreamweaver. You can set up a template file for your site and have editable blocks so on certain pages you can inject javascript into the header/footer, edit the page title, meta tags, etc.
yeah, not going to work for this site. I use wordpress for specific pages, but not the whole thing. Pre built CMS’s never seem to do exactly what i want and end up being more work than it would be to build on my own. IMO they try to do too much! …should leave some of the coding to the devs
(probably a topic for a diff. thread tho)
I’d assume there would be an array of js file paths that gets iterated to print out the script tags? how would you add additional js file from outside the header include? Then your solution might be something like this?:
$page_title = 'Contact Us Page';
$curPage = 'contact'; // if you need this for topnav, etc
require_once('header.php');
$jsPaths[] = '/js/email.js';
header->printHeader();
So, the idea here is that the jsPaths array is instantiated in header.php, so we have to include header.php first, then add the email.js path to its jsPaths array. Finally, we have to call printHeader since we’ve updated the data it’s using.
…seems a bit sloppy and some minor scoping issues arise as well since we’re exposing unnecessary vars to the page that need to be available to header.php. All minor issues I supposed…PHP is a scripting language after all, but I feel like there might be a more advanced way to do this?
I’ve known about cakePhp, but have never played around w/ it. …seemed a bit complicated, but probably not too bad once you dig into it. Zend looks really cool! I’ve worked w/ Smarty and tested out some other template engines, but eventually decided that php can do anything they can and it’s better to leave it at that. An MVC framework could be the tkt tho.
I once had a system with php includes and all that. The I realised that life would be easier if I used a tried and tested CMS. So I use Wordpress now. Unless you are doing something that cannot be done in Wordpress, maybe just migrate over.
I think you just answered your own question, tkane. Setting a bunch of global variables and then include a procedural file is basically how you do it in PHP, if you don’t use a framework or some sort of rendering engine. It’s not exactly pretty, but for small sites, it can work just fine.
I’m not sure I understand your questions entirely, but:
There’s no need to close all html tags in each php file. The important thing is that the resulting HTML page is valid. It’s no problem if the body tag is opened in header.php and closed in footer.php. If your editor issues warnings, just ignore them.
There’s no need to ‘pass data’ to includes. All data available in the main script is also available in the included scripts.