SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best method to handle header/footer

    Hey,

    What would be the best method to handle header's and footer's?

    I was thinking that doing a require_once for the header file, the actual content page and footer file per page. But I know that includes and requires are heavy. And then just doing something like example.php?ref=login to create dynamic sections. But then I started thinking every time I refferenced a new "example.php?ref=" I would also be calling the 3 required pages again. So I don't know if this would be a good way to go?

    Or maybe do a class and initiate an object to return header / footer ?

    What I originally had planned was to just have everything be html/css on each page and if I ever needed to change the header/footer I would just need to edit each individual one. I don't know if this would be the best method ? It parses less PHP so it makes it faster but it slows down on production because you have to edit each page one by one. But how often would I be editing the header and footers? Not very much is my guess.

    Keep in mind that this site could generate millions per day. What I'm looking for mostly is performance and structure. How do some of these big dogs handle it?

    Let me know what you guys think.

    Thanks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    In most cases you can divide this into two templates.
    One will be the master template and the other the view template.

    Bare bones Master Template:
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8">
    	<title>Master Template</title>
    </head>
    <body>
    
    	<div id="container">
    	
    		<div id="masthead">
    		</div>
    		
    		<div id="content">
    		<?php $this->yield(); // view will dump here ?>
    		</div>
    		
    		<div id="footer">
    		</div>
    	
    	</div>
    
    </body>
    </html>
    Simple View:
    HTML Code:
    <p>Hello from view</p>
    Putting them together:
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8">
    	<title>Master Template</title>
    </head>
    <body>
    
    	<div id="container">
    	
    		<div id="masthead">
    		</div>
    		
    		<div id="content">
    			<p>Hello from view</p>
    		</div>
    		
    		<div id="footer">
    		</div>
    	
    	</div>
    
    </body>
    </html>
    There is another alternative that makes this much more flexible. If your master program can execute controllers, then you execute separate controllers/modules from within the view directly.

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8">
    	<title>Master Template</title>
    </head>
    <body>
    
    	<div id="container">
    	
    		<div id="masthead">
    			?php $this->_driver->execute('masthead'); ?>
    		</div>
    		
    		<div id="login">
    			<?php $this->_driver->execute('login'); ?>
    		</div>
    		
    		<div id="content">
    			<?php $this->_driver->execute(); // view will dump here ?>
    		</div>
    		
    		<div id="footer">
    			<?php $this->_driver->execute('footer'); ?>
    		</div>
    	
    	</div>
    
    </body>
    </html>
    That would make each piece dynamic. Obviously more involved though. The masthead, footer, login would be separate controllers being executed as sub views.

    I would probably recommend the first as its much less complex then setting up something that allows nested execution of modules/controllers.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Other methods available to you, if you feel like going over the top i highly recommend smarty.

    I don't know why you want to abstain from using include/require. I can't think of a site i've worked on that hasn't used these.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Smarty is a terrible idea becasue it is highly inefficient. The concept can be replicated easily using pure php which basically comes down to my first example. Executing controllers from within the view makes the code highly reusable. The Masthead could access the state of the entire application easily if it could be executed in a nested fashion from within the view.

  5. #5
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oddz: can you post your code that uses those templates?

    I have something similar to what you have, but I really don't like the usage of it.

    Example:
    PHP Code:
    $page = new Template(SITE_PATH '/templates/layout/outside.php');
    $page->title 'Profile Home';
    $page->content = new Template(SITE_PATH '/templates/statics/home.php');
    $page->content->userInfo = new Template(SITE_PATH '/templates/blocks/viewProfile.php');
    $page->content->userInfo->set('user'$P);
    $page->content->userStars = new Template(SITE_PATH '/templates/blocks/listStars.php');
    $page->content->userStars->set('stars'$P->stars);
    $page->content->userFleets = new Template(SITE_PATH '/templates/blocks/listFleets.php');
    $page->content->userFleets->set('fleets'$P->fleets);

    $page->leftMenu = new Template(SITE_PATH '/templates/menus/outside_left_menu.php');
    $page->submenu = new Template(SITE_PATH '/templates/menus/outside_submenu.php');

    echo 
    $page
    Plus, it tends to need duplicated code all over...

  6. #6
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Vali's way was the way I was thinking of doing the header and footer:

    PHP Code:
    <?php
    $template 
    = new Template();
    echo 
    $template->header();
    ?>

    HTML HERE

    <?php 
    echo $template->footer(); 
    ?>

  7. #7
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    9three, that is not my way... I only have 1 echo.

    You might a well do this:
    PHP Code:
    <?php require('header.php'); ?>
    html
    <?php require('footer.php'); ?>

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    I haven't the time to provide a thoughtful, detailed response so I apologize Vali.

    With that said, the primary difference between your system and mine is that the template is a singleton and each template file is responsible for executing a sub module.

    So taking your example into account the master template may look something like:

    Master Template
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8">
    	<title><?php echo $tpl['title']; ?></title>
    </head>
    <body>
    
    	<?php echo $this->_objDriver->executeModule('Menus.OutsideLeft'); ?>
    	<?php echo $this->_objDriver->executeModule('Menus.OutsideSubMenu'); ?>
    
    	<div id="content">
    		<?php echo $tpl['content'] ?>
    	</div>
    </body>
    </html>
    Home Template (content contents)
    HTML Code:
    <?php echo $this->_objDriver->executeModule('Block.Profile'); ?>
    <?php echo $this->_objDriver->executeModule('Block.ListStars'); ?>
    <?php echo $this->_objDriver->executeModule('Block.ListFleets'); ?>
    In my system I don't directly execute nested templates but indirectly through the execution of a sub module/controller.

    The other thing to understand is that the template is a single object. However, you can class the fetch() method to execute a template file and return its contents.

    PHP Code:
    $objTemplate = new Template($this); // pass driver

    $objTemplate->assign('title','title');

    /* other assignments */

    $content $objTemplate->fetch('/templates/statics/home.php');

    $objTemplate->assign('content',$content);

    echo 
    $objTemplate->fetch('/templates/layout/outside.php'); 
    Its may be a little difficult to grasp so if you would just like to see some of the code behind all this just ask. However, the basic concept is that any template file can execute any module from within. This means that a site can essentially be broken up into pieces rather then pages and creates highly reusable blocks of code.

    This is a simple example of how 9three's example could look in my system:

    Master Template
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8">
    	<title><?php echo $tpl['title']; ?></title>
    </head>
    <body>
    	<?php echo $this->_objDriver->executeModule('Header'); ?>
    	
    	<?php echo $tpl['content']; ?>
    	
    	<?php echo $this->_objDriver->executeModule('Footer'); ?>
    </body>
    </html>
    Its inspired by smarties fetch concept if you can't tell. Where there is a single template per request, it can be loaded with as much data as you would like and you can execute/fetch the content for as many template files as you would like.

  9. #9
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    Smarty is a terrible idea becasue it is highly inefficient.
    I could not agree more. I've had to fix an application that used Smarty; talk about a headache. I'm having a hard time refraining from dropping f-bombs here.

    Also, why not take a look at CodeIgniter? It's easy to pick up and would take care of this aspect of design for you
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Smarty is a love hate relationship but mostly hate. My template language of choice is PHP (not to get off-topic).


Bookmarks

Posting Permissions

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