Gen. partnerAlgotech

Include (folding pages from pieces)

PHP is originally a templating language, which was created to make it easy to put pieces of pages together.

|
August 23, 2019

PHP is originally a templating language, which was created to make it easy to put pieces of pages together.

Supported formats

Folding works as text, so it is advisable to use relevant formats such as .html or .md.

When a PHP file is pasted, its contents are executed as if they physically existed at the pasted location.

Folding pages and inserting common content

Often we need to create several pages that have common content - for example, a menu.

In plain HTML, we would first create a page with a menu and then copy it many times. But in PHP we can automate the whole process.

Let's have a file menu.html where the menu content is and index.php where we put the content and the menu.

A simple example:

<div class="page">
<div class="content">
<?php
include __DIR__
'/article/' . ($_GET['page'] ?? 'index') . '.html';
?>
</div>
<div class="menu">
<?php
include 'menu.html';
?>
</div>
</div>

This script automatically inserts the page content from the /article directory and reads the file name according to the user input (URL parameter ?page=...). If no parameter was passed, index.html is used.

So the URL might look like, for example, example.com?page=contacts and load /article/contacts.html.

Loading comments...

Stay in the loop

Subscribe to our newsletter and get the latest cybersecurity news delivered straight to your inbox.

Your data is safe. You can unsubscribe from the newsletter at any time.