Alexey Bereznyak
Alexey Bereznyak

Reputation: 49

PHP static site generator

I am currently building up a static website with PHP. What I want to have is the following: There is one main.php file that includes all the common parts of the page (header, footer, navigation and so on) and a couple of pages like index.php, team.php, contact.php and so on. I do want to be able to edit the main.php in that way, that is effects all the pages in my project. I do however want to be able to output some specific content for each single page by writing code directly in the specific file (not main.php but e.g. index.php). So I want to assign each page of the project to use main.php as the core template. My main.php file which looks so far like this is here:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>PHP Static Site Genarator</title>
</head>

<body>

    <div id="navigation">
        <?php echo navigation()?>
    </div>

    <div id="pageWrap">
        <header>
            Header
        </header>
        <main id="content">

            <?php echo $templateContent; ?>

        </main>
        <footer>
            Footer
        </footer>
    </div>
</body>

</html>

and a couple of pages like index.php, contact.php and so on. All of them look like this:

<?php include_once $_SERVER['DOCUMENT_ROOT'].'/essentials/settings.php'; ?>
<h1>
    This is the h1 for the index page
</h1>

<?php require($_SERVER['DOCUMENT_ROOT'].'/essentials/exit.php'); ?>

I deliver some settings in my settings.php file and in my exit.php file I have the following code:

<?php 

$templateContent = ob_get_contents(); 

ob_end_clean();

echo $templateContent;
?>

I need to somehow bind all the pages to be the part of main.php at the point where I output the $templateContent variable at

What is the right way for me to achieve this?

Upvotes: 0

Views: 1454

Answers (3)

svet
svet

Reputation: 12078

I personally would consider using a micro PHP framework like lumen, slim, fat-free-framework for making even the smallest PHP web application.

That said, below is the solution of the approach you took to solve the problem. I will keep the file structure and file naming similar to yours, even though there is a place for improvement here.

Lets consider the following application structure:

essentials/main.php
essentials/navigation.php
essentials/exit.php
essentials/settings.php
index.php
about.php
contact.php

As you can see, I have moved all common files into the essentials folder and left all pages in the root folder

essentials/settings.php

<?php

// start output buffering
ob_start();

essentials/navigation.php

<ul>
    <li><a href="index.php">index</a></li>
    <li><a href="contact.php">contact</a></li>
    <li><a href="about.php">about us</a></li>
</ul>

main.php

<?php $templateContent = ob_get_clean(); ?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>PHP Static Site Generator</title>
</head>

<body>

    <div id="navigation">
        <?php include("navigation.php"); ?>
    </div>

    <div id="pageWrap">
        <header>
            Header
        </header>
        <main id="content">

            <?= $templateContent; ?>

        </main>
        <footer>
            Footer
        </footer>
    </div>
</body>

</html>

<?php require_once("exit.php"); ?>

essentials/exit.php

<?php 

ob_end_flush();

The actual pages structure index.php, about.php and contact.php look similar:

index.php

<?php require_once("./essentials/settings.php"); ?>

<h1>
    This is the h1 for the index page
</h1>

<?php require_once("./essentials/main.php"); ?>

I hope this helps to move your idea forward, but highly encourage you to investigate time and learn a modern approach for PHP application development. Laravel is a great stating point.

Upvotes: 2

Rado Salov
Rado Salov

Reputation: 9

It looks like you are looking for template engine. There are many out there like Twig, Blade, Smarty

Or you can write one alone like it is explained in this excellent post by David Adams

Upvotes: 0

akoneko47
akoneko47

Reputation: 387

Maybe you want something like this?

Structure

index.php
about.php
core\function.php
core\setting.php
templates\header.php
templates\footer.php
templates\menu.php
  • core\function.php - all function in here
  • core\setting.php - all setting in here
  • templates* - html page

index.php

<?php
include_once("core\setting.php");
include_once("core\function.php");

/*
* code php for this file in here
*/
$message = 'index data';

include_once("core\header.php");
include_once("core\menu.php");

echo <<<HTML

<div>show content {$message}</div>

HTML;

include_once("core\footer.php");
?>

Upvotes: 0

Related Questions