Recently, I wrote a small and functionally CMS, without OOP logic, and with the famous bad practice to include the header, the content and the footer:
<?php
require_once("header.php");
//Content
require_once("footer.php");
?>
Now, I'm trying to setting up a new CMS, based on the template engine. I read a lot of articles about Smarty & CO. but I think it's not completly clear because you need to learn this specific language. So how can I do this? I've been searching for this and just can’t seems to find anything that would fit the bill....
Thanks in adavance!
What you need to do is separate the bits of code that generate the content's data and the bits of code that display the data. In MVC-land the data part would be handled Model/Controller, and the display by the View. To paraphrase your code:
<?php
require('data.php');
require('header.php');
require('display.php');
require('footer.php');
Which logically simplifies to:
<?php
require('data.php');
require('display.php');
Your data.php
will do nothing but generate the data into particular variables that your display.php
will then take and turn into displayable content. It's best to do all of the content-related work before outputting anything in case you want to go back and change something in an HTML header [ie: page title] or perform an HTTP redirect or other header()
operation.
It's a good idea to look into the MVC-style of coding for something like this, I've found the following two pages to be extremely useful in understanding, and then creating an MVC app.
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
Have everything go to a single route file with rewrite, then pull the correct files like this:
ob_start();
include('page_you_need_to_load.php');
$html = ob_get_contents();
ob_end_clean();
include('header.php');
echo $html;
include('footer.php');