0
votes

I've asked a similar question before, but I've got a more specific question about this "style" of creating a page.
I have 3 pages for a template, header.php, page.php and footer.php. I'm trying to allow myself to easily edit parts of the site within a single page, but also be able to have extra things in on per-page basis. My current structure is:
header.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Website Name<? if ($title) echo ' &ndash; ' . $title; ?></title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="reset.css">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

page.php

<?
$title = 'Page Title';
require_once('includes/header.php');
?>
<!-- Any extra stuff for the header goes here -->
</head>
<body>
Page content goes here.
<? require_once('includes/footer.php'); ?>

footer.php

<footer>
I am a footer
</footer>
</body>
</html>

Although this works, I cannot make a publicly editable header (menus etc) that easily since my header.php page does not contain anything in the <body>. However, closing the <head> in header.php would not allow me to add extra files (such a page-specific javascript) on per-page basis. To my knowledge, CSS and javascript being included within the <body> tag is not a good idea.
I'm guessing a further file (say, menu.php) would be required and included at the top of each page, after the <head> tag? However, that doesn't seem that easy to read/natural, I feel there must be a better solution?

3
Javascript should be included in the body tag, at the bottom of your page, unless it needs to execute before the loading (ie shiv in your exemple.) - mddw
@mdi Ah, I was aware that sometimes you should be put it in the body, which a second reason I posted that. Now I know which situation, so thanks for the help! - Joseph Duffy

3 Answers

2
votes

One easy solution is to have inside "header.php" a line to echo the content of $extraHeaders:

<!DOCTYPE html>
<html>
<head>
<?php echo $extraHeaders ?>
...

Then, any page you want to add specific headers to (stylesheet, javascript file, etc.), you just include it in the $extraHeaders variable:

$extraHeaders = '<script type="text/javascript" src="myscripts.js"></script>'

And it will be automatically be included in the headers for that page.


To solve the problem of syntax highlighting and avoiding to have to escape the quotation marks, you can use the output buffer syntax:

ob_start();
?>
<your html goes here> Alternatively, you can include an html file here.
<?php
$extraHeaders = ob_get_contents();
ob_end_clean(); 
...

This will allow you to use a variable, as previously suggested, but with syntax highlighting, and there is no need to escape anything.

0
votes

Make an _autoload() script to pre-load all those php files.

This way when ever there is anything new to put, you can always go tho the script containing the _autoload() function and update it there.

Btw, putting javascript at the very end of the <body> tag is actually a good practice.

0
votes

You can use ob_start + regex ( tags like {title} or {scripts} to have an access at the end of loading the "page".