Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Problem when using templates with modules

    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:

    index.php:

    PHP Code:
    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
            
    ob_start();
            include(
    "modules/$module/index.php");
            
    $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);
    $t->generateOutput(); 
    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.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You can use return to end execution of an included file and pass control back to the script that is including it.

  • Users who have thanked Inigoesdr for this post:

    guvenck (11-16-2007)

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thanks a lot Inigoesdr. That was exactly what I was looking for! You made my day!

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    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:

    PHP Code:
    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";
            include(
    "redirect.php");
            exit();
        } else {
            
    //some error
            
    include("header.php");
            echo 
    '<p>Error!</p>';
            
    NewsForm();
            include(
    "footer.php");
            exit();
        }
        exit();
    } else {
            include(
    "header.php");
            
    NewsForm();
            include(
    "footer.php");

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

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •