Sign in to follow this  
  • entries
    43
  • comments
    51
  • views
    31881

Content vs. Structure

Sign in to follow this  

309 views

I'd like to make my CMS as flexible as possible when it comes to the layout of the page but at the same time I have very specific types of content that must be included. Originally I had planned to have the XHTML written into the same page that breaks up the URL and retrieves the correct data but that would mean that casual users of the CMS might be scared away from changing the out-of-the-box layout because of all that PHP in there. So this is the solution I'm going to implement:

When the index breaks up the URL there are three different cases:

  • The URL is pointing to a real document

  • The URL is pointing to the index

  • The URL is pointing to a virtual document



The case of the real document is easy: I simply include that document.

The case of the index is a little bit more complicated: I have to output the template and fill it with the front page. The front page is dynamic content and is generated by whichever module is designated as the default module.

The final case is slightly more complicated still: I have to output the template and fill it with the dynamic content as parameterized by the rest of the URL.

The front page is generated by a module as if the module was asked to display content with no parameters so it seems like these two cases can be combined into one. Each module already has functions to generate content so it seems like all I'll have to do is call these in the correct place.

First let's look at the current code that handles these cases

// Does this file exist?
if (file_exists($DOCUMENT_ROOT . $REQUEST_URI) and
($SCRIPT_FILENAME != $DOCUMENT_ROOT . $REQUEST_URI) and
($REQUEST_URI != '/')) {

// The file exists, simply include it
include($DOCUMENT_ROOT . $REQUEST_URI);
} else {

// The file does not exist, the content might be dynamic
$url = strip_tags($REQUEST_URI);
$url_array = explode('/', $url);

// The first element of the array is empty since the first char is '/'
array_shift($url_array);

if (empty($url_array)) {
// The request was for the index
if (DisplayFrontPage() == false)
print "Error loading front page";
} else {
// The first entry is the module to run, and so we must attempt
// to execute that module. The rest of the URL array become the
// module's arguments
$module = $url_array[0];
array_shift($url_array);

ExecuteModule($module, $url_array, GetRequestVars());
}
}



ExecuteModule() will execute the module's content-generating function and that function will output the content to the browser so the XHTML code will need to be embedded within this if. Ugly.

This is how I'm planning to reorganize it

---index.php---

// A generic function that prints the content of the URL
function PrintModuleContent() {
$url = strip_tags($REQUEST_URI);
$url_array = explode('/', $url);

// The first element of the array is empty since the first char is '/'
array_shift($url_array);

if (empty($url_array)) {
// The request was for the index
if (DisplayFrontPage() == false)
print "Error loading front page";
} else {
// The first entry is the module to run, and so we must attempt
// to execute that module. The rest of the URL array become the
// module's arguments
$module = $url_array[0];
array_shift($url_array);

if (ExecuteModule($module, $url_array, GetRequestVars()) == false)
print "Error loading page";
}
}

// Does this file exist?
if (file_exists($DOCUMENT_ROOT . $REQUEST_URI) and
($SCRIPT_FILENAME != $DOCUMENT_ROOT . $REQUEST_URI) and
($REQUEST_URI != '/')) {

// The file exists, simply include it
include($DOCUMENT_ROOT . $REQUEST_URI);
} else {

// The file does not exist, the content might be dynamic
// Output the template
require_once('template.php');
}

---template.php---






Now the user is free to make a template that is mostly free from PHP. The PHP parts will probably be labeled as 'Place this code where you want your content to show up'.

There we go! A basic template system.
Sign in to follow this  


2 Comments


Recommended Comments

You know you can have something like this:

// test.php

if ((@include('file.php') == 'OK') {
echo 'included';
} else {
echo 'not included!';
}

// file.php
echo 'Sup guy!';
return 'OK';

Share this comment


Link to comment
Yes. Actually, for the modules, they are using that mechanism to return an array of the various content generating functions.

If your comment was meant specifically towards the template issue please explain! I'm interested in different methods as well.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now