Reputation: 1009
On my website i've got a number of folders named news / blog / products etc... all of these sections are virtually the same setup apart from small internal styling changes in the head, instead of copying and pasting the same file n-amount of times i'd rather have a template, and I started doing it like this:
Each page has 2 includes:
<?php
include "../article_template/part1.html";
?>
// Each pages internal styling goes here
<?php
include "../article_template/part2.html";
?>
Part 1.html:
<?php
require_once "directory/functions.php";
?><!DOCTYPE html>
<html><head>
<link rel="stylesheet" type="text/css" href="http://example.org/folder/style1.css">
<link rel="stylesheet" type="text/css" href="http://example.org/folder/style2.css">
<style type="text/css">
Part 2.html:
</style>
<title> <?php get_title(explode('/', $_GET['$url'])); ?></title>
</head><body>
<?php
include "../topborder.html";
include "../banner.php";
include "../navbar.html";
?>
<div id="container">
<div id = "article_container">
<?php
$uri = explode('/', $_GET['$url']);
get_article($uri[0]);
?>
</div>
<?php include "../folder/sidepanel.html"; ?>
</div></body></html>
In between parts 1 and 2 I can just slot in the internal styling for the page. While this is working fine, it seems a little hacky - am i going to run into many performance or any other issues with a setup like this? - While this setup is easier for me as I only have to change 1 file to effect all areas I'm a little worried about the performance. Is there a better way to do this?
Upvotes: 0
Views: 60
Reputation: 796
Repeating yourself costs more time up front, and potentially a lot more down the road if you have to go back and make the same change in multiple places. DRY ( Don't Repeat Yourself ) reduces duplication and the maintenance problems coming with it.
In your example you're including different HTML files, we can make a function that includes any of your HTML templates. Benefits: shorter code, you don't need to remember the path of every file, if you need to change an HTML file you only do this once.
<?php
// Add this to functions
function get_template($template = '') {
$path = '/absolute/path'; // Absolute path to your project
$templates = array(
'topBorder' => '/views/topborder.html',
'banner' => '/views/banner.html',
'navBar' => '/views/navBar.html',
'sidePanel' => '/views/sidepanel.html'
);
if(isset($templates[$template])) {
$templateFile = $path . $templates[$template];
if(file_exists($templateFile))
include($templateFile);
}
}
// Instead of including some static HTML
// We only call the function with the corresponding template
get_template('navBorder');
get_template('banner');
get_template('navBar');
Upvotes: 1
Reputation: 26056
I'm a little worried about the performance. Is there a better way to do this?
Why? The PHP tags coming before HTML means 100% of nothing. The PHP will be acted on as PHP & the second the HTML <!DOCTYPE html>
comes along, it gets spit directly through the web server as HTML.
Upvotes: 0
Reputation: 462
This is the same layout technique as a WordPress page uses. You shall be fine. If you need proof download a free WordPress theme and take a look at it.
Upvotes: 1