4
\$\begingroup\$

I feel that I am repeating myself a lot with this HTML/PHP code. Am I right, and is there a maintainable way of reducing the amount of repetition in the code?

mydomain/index.php:

<!DOCTYPE HTML>
<?php $root="/mywebdir"; ?>
<html>
<head>
<?php include("$root/inc/meta.php"); ?>
<meta name="description" content="Some description" />
<meta name="keywords" content="some,home,page,keywords" />
<title>Home - This Website</title>
</head>
<body>
<div id="main">
<?php include("$root/inc/header.php"); ?>
<?php include("$root/inc/nav.php"); ?>
<div>
<h2>Home</h2>
<p>Introduction.</p>
<p>Some content.</p>
<p>More content.</p>
</div>
<?php include("$root/inc/footer.php"); ?>
</div>
</body>
</html>

mydomain/somedir/index.php:

<!DOCTYPE HTML>
<?php $root="/mywebdir"; ?>
<html>
<head>
<?php include("$root/inc/meta.php"); ?>
<meta name="description" content="Some description" />
<meta name="keywords" content="some,keywords,about,this,page" />
<title>Some Page - This Website</title>
</head>
<body>
<div id="main">
<?php include("$root/inc/header.php"); ?>
<?php include("$root/inc/nav.php"); ?>
<div>
<h2>Some Page</h2>
<p>Some content about this page.</p>
</div>
<?php include("$root/inc/footer.php"); ?>
</div>
</body>
</html>
\$\endgroup\$
4
\$\begingroup\$

Consider using existing template systems to help you that support inheritance. Template inheritance isn't a strict requirement, but is a powerful feature that makes maintaining templates a lot easier. Twig is easy to use, and feature-rich.

If you want to go it your own way, you could simply implement the use of layout files:

layout_default.php

<!DOCTYPE HTML>
<?php $root="/mywebdir"; ?>
<html>
<head>
<?php include("$root/inc/meta.php"); ?>
<meta name="description" content="<?php echo $description ?>" />
<meta name="keywords" content="<?php echo keywords?>" />
<title><?php echo $title ?></title>
</head>
<body>
<div id="main">
<?php include("$root/inc/header.php"); ?>
<?php include("$root/inc/nav.php"); ?>
<div><?php echo $contents ?></div>
<?php include("$root/inc/footer.php"); ?>
</div>
</body>
</html>

example page.php

<?php ob_start() ?>
<p> main body html</p>
<?php
$title = "x"; 
$description = "x"; 
$keywords = "x"; 
$contents = ob_get_clean();
require 'layout_default.php'

This is a very crude implementation, simply enough to illustrate the principle.

\$\endgroup\$
  • \$\begingroup\$ Would it be bad to use a string to define the page contents? (Instead of ob_get_clean) \$\endgroup\$ – Kendall Frey May 16 '12 at 21:54
  • \$\begingroup\$ I wouldn't say bad, but it won't make your life easy - output buffering simply makes it a lot easier to construct your main html body any way you wish (i.e. as .. plain html without $contents .="<p>this.."; $contents .= "<p>that.."; etc.) - but the above is just an example. \$\endgroup\$ – AD7six May 16 '12 at 21:56
  • \$\begingroup\$ Well, I think I'll give it a try. \$\endgroup\$ – Kendall Frey May 16 '12 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.