PHP to write out HTML

Pretty basic question here.

I have a website with lots of pages. When I want to change the Menu for example, I gotta change it on every page.

Is there is a better way to change it with PHP and have it automatically updated on all pages? Obviously I would have to add PHP code in each page first, but at least I would only have to do it once.

Is this a safe/quality approach? And how would I do it?

thank you!

I take it a lot further – most websites have elements that are all the same regardless of the page, or at least mostly the same. The contents of the HEAD tag, the H1, the UL#mainMenu are just the start – for the most part your outer layout elements, width wrapper, and footer also tend to all be static.

Even if you aren’t going to build a real CMS or incorporate into one, a “poor man’s CMS” of using includes and functions for the static elements of every page is a huge time-saver and makes fixed pages easier to maintain.

The way I like to go about it is to have a common.template.php file with two functions in it - theme_header and theme_footer. I also pass some values to it as variables and even make 100% sure the server is outputting the character encoding I want with the HEADER function.

A typical common.template.php file for me goes something like this:

FILE: common.template.php


<?php

function theme_header($title,$keywords='',$description='') {

	header('Content-Type: text/html; charset=utf-8');
	
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
	xmlns="http://www.w3.org/1999/xhtml"
	lang="en"
	xml:lang="en"
><head>

<meta
	http-equiv="Content-Type"
	content="text/html; charset=utf-8"
/>

<meta
	http-equiv="Content-Language"
	content="en"
/>';

if (!empty($keywords)) echo '

<meta
	name="keywords"
	content="',$keywords,'"
/>';

if (!empty($description)) echo '

<meta
	name="description"
	content="',$description,'"
/>';


echo '

<link
	type="text/css"
	rel="stylesheet"
	href="screen.css"
	media="screen,projection,tv"
/>

<title>
	',$title,'
</title>

</head><body>

<div id="pageWrapper">

	<h1>
		Site Name
		<span></span>
	</h1>

	<ul id="mainMenu">
		<li><a href="home">Home</a></li>
	</ul>';
}

function theme_footer() {
	echo '
	<div id="footer"><hr />
		Footer Content Here
	<!-- #footer --></div>

<!-- #pageWrapper --></div>


</body></html>';

}

?>


So a typical index.php homepage for that would go something like this:

FILE: index.php


<?php

require_once('common.template.php');

theme_header(
	'Test Site', // TITLE tag
	'Poor, Man, CMS, Content, Management, System', // Keywords META
	'Just a quick test of linking together a poor man's CMS' // Description META
);

?>

<p>
	Your normal page markup would go here!
</p>

<?php 

theme_footer();

?>

Don’t know how much PHP you know – I tried to keep this example pretty simple code-wise while giving you a few advanced techniques to help bring you up to speed quick. Passing the title, keywords and description let you craft them to each page as needed, while keeping the rest of the static elements all the same.

Hope this helps…

What you and rguy84 are referring to (using templates) is a great way to organize a site.

You can get very elaborate with your design, but the most basic example would be:

Index.php:

require_once('header.inc.php');

<div id="content">
Blah blah blah
</div>

require_once('footer.inc.php');

In your case, you could include the menu bar code in the header.inc.php file itself or separate it out and have it in its own file:

require_once('header.inc.php');
require_once('menubar.php');

<div id="content">
Blah blah blah
</div>

require_once('footer.inc.php');

If the menu bar will stay the same then it makes more sense to keep it together with the header.inc.php, but that will depend on how you have laid out your site. :slight_smile:

I have php file that has my menu bar only in it, then use a PHP include to read in my menu file. If you look at my source it will look like i coded 40+ lines in every file, but it is actually just one. The only draw back is if they hack your server, and see you have include(‘menu.php’); in a page, they are smart enough that most pages will have a menu, so putting dirty code in menu.php vs index.php would be more beneficial.

That being said if you include(‘menu.php’); in every page, you auto update every page after you change menu.php