Dividing Layout into Elements in PHP
Inserting one file into another is often used to divide layout files into parts. This is necessary to remove repeating parts of HTML pages into separate files for easier editing.
Let's look at an example. Suppose we have the following page:
<html>
<head>
<title>page1</title>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
</head>
<body>
text 1
</body>
</html>
And one more:
<html>
<head>
<title>page2</title>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
</head>
<body>
text 2
</body>
</html>
As you can see, these two files have identical
content in the head
block. Let's extract it
into a separate file:
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
Let's connect this file to each of our pages:
<html>
<head>
<title>page1</title>
<?php include 'elem/head.php'; ?>
</head>
<body>
text 1
</body>
</html>
Given files with the following layout:
<!DOCTYPE html>
<html>
<head>
<title>title</title>
</head>
<body>
<header>
header
</header>
<aside>
sidebar
</aside>
<main>
content
</main>
<header>
footer
</header>
</body>
</html>
Suppose the layout of the files differs only in titles and content. Extract the content of the header, footer, and sidebar into separate includable files.