View Full Version : Problem when using templates with modules

11-15-2007, 02:51 AM
This won't be easy for me to explain. Any advices are welcome, as this problem prevents me from developing my script further.

I've simplifed the code to present it clearer.

I use a templating system (MiniTemplator) with my CMS system, like this:


if(isset($_GET['page'])) { // this is a static page
$page = $_GET['page'];
$r1 = mysql_query("SELECT title, body FROM pages WHERE ID='$page'") or die(mysql_error());
$row = mysql_fetch_array($r1);
$content = '<h3>' . $row['title'] . '</h3>';
$content .= $row['body'];

if(isset($_GET['module'])) { // this is a module
$module = $_GET['module'];
if(file_exists("modules/$module/index.php")) { // module exists
$content = ob_get_clean();

// build the page
require_once ("includes/MiniTemplator.class.php");
$t = new MiniTemplator;
$t->readTemplateFromFile ("templates/" . $config['theme'] . "/template.htm");
$t->setVariable ("header", $header);
$t->setVariable ("menu", $menu);
$t->setVariable ("content", $content);
$t->setVariable ("footer", $footer);

Where the variables come dynamically from the DB.

When displaying static pages, there is no problem as the page content comes from the DB.

But when I use modules, the problem starts. The reason is that I can't use exit() function anymore in a modules' code. When I use it, the script dies without generating the template. Modules may become complex when there are forms, error checks etc. involved and I need to use the exit() function. Think of a signup form in the "user" module and it's error checking. I can make the page generated by avoiding exit() functions but that makes php go through the whole unnecessary code.

I'm stuck at this point. Any advices are welcome.

11-15-2007, 08:36 AM
you don't need to use exit, and for this very reason, it's often a bad idea. There isn't a situation where conditionals ('if', 'else') can't have the required effect.

Failing that, you might want to have a look at how Exceptions work.

11-15-2007, 08:50 AM
You can use return (http://php.net/return) to end execution of an included file and pass control back to the script that is including it.

11-16-2007, 03:43 AM
Thanks a lot Inigoesdr. That was exactly what I was looking for! You made my day!

11-16-2007, 05:25 PM
There is only 1 time when you really need exit. When an error/exception has occured and running the script further would cause problems. Do not use it for logic.

11-16-2007, 06:27 PM
I actually used it a lot for logic before. Mostly in if/else statements and at times when I saw no reason for the logic code to be executed. Like:

if(isset($_POST['form_news'])) {
// several variable error checking
$result = mysql_query("INSERT news SET title='$title', body='$body' WHERE ID='$id'") or die(mysql_error());
if($result) {
$msg = "News item added successfully!";
$goto = "news.php?op=list";
} else {
//some error
echo '<p>Error!</p>';
} else {

So, you say, I should use return; instead, or not use anything at all?

11-16-2007, 07:08 PM
Not at all - except as stated before.

You will not gain any speed benefits from exiting as soon as possible. If there is code after that point, you probably need to structure it differently.