Tims' Blog

Visit the Old WP Blog

Take Me Home


HFS Simple Blog 2 - The Folders and Files

So we need to take a look at the folder structure and what files we need to make this all work.

Note: These pages are going to be updated quite frequently as we dive into this script. It's a work in progress and there will be a point where the "simple" might get a bit blurry. So we need to stick to our guns. But as with all projects and especially ones like this, they will grow into something bigger than what they started out as. That's all part of the fun of this stuff.

The Folder Structure

        pages             - This is "above or equal to" the public_html_folder
        [Article/Post files live here]
        template          - This is "above or equal to" the public_html_folder
        [The main Page Templates live here]
        public_html
        [The index.php and .htaccess file lives here]
        -- assets        - This is "below" the public_html folder
          [css, js, images etc live in here]
        

So if you are paying attention ( and I just added some comments to help explain the following a bit better) , you will see that the pages and templates folders are not in the public_html (Document Root) Folder. This has a few benefits.

They are very hard to access via the URL when they are equal to or above the public_html folder. Anything in or below the public_html folder is open to anyone. So they are kind of inaccessible via the URL, but the script can easily access files in these locations.

The Files

So we have a collection of "article" or Post files, which are created manually on my local server before being uploaded via FTP. That's all part of the Simple part (but is it? More on that later...)

Using the PHP highlight_file($filename) function, we can directly view the pages code, in glorious "Syntax Highlighting", which is used in the creation of this blog.

As a sneak peek as to what is in a "typical" article file, I'll include the display of it here.

This is the actual code for this "Article" page

<?php
$page_info 
= [
    
'title'       => 'HFS Simple Blog 2',
    
'description' => 'PHP Code Tutorial - HFS Simple Blog 2 - The Folders and Files',
    
'keywords'    => 'PHP,Blog',
    
'next'        => 'hfs-simple-blog-3',
    
'prev'        => 'hfs-simple-blog-1',
    
'date'        => '2020-08-22 8:55pm',
    
'author'      => 'Tim Brownlaw'
];
?>
<section>
    <h2>HFS Simple Blog 2 - The Folders and Files</h2>
    <p>So we need to take a look at the folder structure and what files we need to make this all work.</p>
</section>
<p>Note: These pages are going to be updated quite frequently as we dive into this script. It's a work in progress and there will be a point where the "simple" might get a bit blurry. So we need to stick to our guns. But as with all projects and
    especially ones like this, they will grow into something bigger than what they started out as. That's all part of the fun of this stuff.</p>

<h4>The Folder Structure</h4>

<pre>
        <strong>pages</strong>             - This is "above or equal to" the public_html_folder
        [Article/Post files live here]
        <strong>template</strong>          - This is "above or equal to" the public_html_folder
        [The main Page Templates live here]
        <strong>public_html</strong>
        [The index.php and .htaccess file lives here]
        -- <strong>assets</strong>        - This is "below" the public_html folder
          [css, js, images etc live in here]
        </pre>
<p>So if you are paying attention ( and I just added some comments to help explain the following a bit better) , you will see that the pages and templates folders are not in the public_html (Document Root) Folder. This has a few benefits.</p>
<p>They are very hard to access via the URL when they are equal to or above the public_html folder. Anything in or below the public_html folder is open to anyone.
    So they are kind of inaccessible via the URL, but the script can easily access files in these locations.</p>
<h4>The Files</h4>
<p>So we have a collection of "article" or Post files, which are created manually on my local server before being uploaded via FTP. That's all part of the Simple part (but is it? More on that later...) </p>
<p>Using the PHP <strong>highlight_file($filename)</strong> function, we can directly view the pages code, in glorious "Syntax Highlighting", which is used in the creation of this blog.</p>
<p>As a sneak peek as to what is in a "typical" article file, I'll include the display of it here.</p>

<div class="card mb-4">
    <div class="card-header">
        <h4>This is the actual code for this "Article" page</h4>
    </div>
    <div class="card-body">
        <?php highlight_file('../pages/hfs-simple-blog-2.php'); ?>
    </div>
</div>

<div class="card mb-4">
    <div class="card-header">
        <h4>This is the code for the Index.php file.</h4>
        <p>And not a function to be found (at this stage).</p>
    </div>
    <div class="card-body">
        <?php highlight_file('./index.php'); ?>
    </div>
</div>

<p>So as you can see, we have a very basic "single" file blog system.</p>
<p>But where does all this "stuff" end up? In a Template...We had better go and take a look at that...</p>

This is the code for the Index.php file.

And not a function to be found (at this stage).

<?php
/**
 * HFS Simple Blog
 * @version 0.1
 *
 * @var array $page_info
 */
// Get the Page Name from the URL
$page_name trim($_SERVER['REQUEST_URI'], '/');
$page_path '../pages/';
$page $page_path $page_name '.php';

$page_content '';

if (
file_exists($page)) {
    
ob_start();                         // Start the Buffer
    
include $page;                      // Include the page to display
    
$page_content ob_get_clean();     // Save (Store) it and clean the buffer

    // Check the array for Prev / Next Links
    // This is going to go somewhere else as a function as we progress
    
if ($page_info['prev'] or $page_info['next']) {
        
$page_content_prev $page_info['prev'] ? '<a href="' $page_info['prev'] . '" class="btn btn-danger">Prev</a> ' $page_info['prev'] : '';
        
$page_content_next $page_info['next'] ? '<a href="' $page_info['next'] . '" class="btn btn-primary">Next</a> ' $page_info['next'] : '';
        
$page_content .= '<div class="row">';
        
$page_content .= '<div class="col-md-10">';
        
$page_content .= '<hr>';
        
$page_content .= '<div class="float-left">' $page_content_prev '</div>';
        
$page_content .= '<div class="float-right">' $page_content_next '</div>';
        
$page_content .= '</div>';
        
$page_content .= '</div>';
    }
} else {
    
$page_content '';
    
// We need to scan and show snippets from each file
    
$file_list glob($page_path '*');
    foreach (
$file_list as $page_name) {
        
$page $page_path $page_name;
        
ob_start();
        include 
$page;
        
$content ob_get_clean();
        
// Grab the block of HTML in the first <section></section> to act as the summary.
        // This makes it more flexible.
        
$content_start_pos strpos($content'<section>');
        
$content_end_pos strpos($content'</section>');
        
$page_content .= substr($content$content_start_pos$content_end_pos $content_start_pos);

        
$show_more_link str_replace(' ''-'strtolower($page_info['title']));
        
$page_content .= '<br><a href="' $show_more_link '">Show More</a>';
        
$page_content .= '<hr>';
    }
    
// Set the Default settings when displaying all Posts
    
$page_info = [
        
'title'       => 'Tims\' Blog',
        
'description' => 'A Blog about everything',
        
'keywords'    => 'PHP',
        
'next'        => '',
        
'prev'        => '',
        
'date'        => '',
        
'author'      => ''
    
];
}
// Build the final output page.
extract($page_info); // Create variables from the array name=>value pairs
include '../templates/template.php'// Throw it all into the Template and watch the magic

So as you can see, we have a very basic "single" file blog system.

But where does all this "stuff" end up? In a Template...We had better go and take a look at that...


Prev hfs-simple-blog-1
Next hfs-simple-blog-3