tags:

views:

117

answers:

7

I have been learning syntax for PHP and practicing it. I come from a .NET background so masterpages always made things pretty easy for me when it came to headers and footers.

So far I have a mainHeader.php and mainFooter.php which have my head menu and my footer html. I created a mainBody.php and at the top I put

<?php include "mainHeader.php" ?>

and for the footer I put

<?php include "mainFooter.php" ?>

This worked perfectly and made me smile because my pages all came together nicely. the mainHeader has my <html> and <body> and my mainFooter has my closing tags for those.

Is this good practice?

+1  A: 

The good practice nowadays is to use a templating engine, such as smarty. For the whole application consider using a framework, like codeigniter.

aularon
couldnt disagree more about smarty
Galen
Yes, I know. I'm having bad times myself with it :) it was just the first example of a templating engine that came across my mind. And after suggesting to use a framework I didn't think the templating engine example matters that much, so I didn't think much over that. Thanks for the feedback anyway : )
aularon
I don't like smarty much, but the idea of using a templating engine is great. Except with PHP... since PHP is already a templating engine, it's kind of weird. Still, the MVC type pattern is so much better... um... let's all just use a framework! But h20 templates is pretty cool for PHP.
Mark Snidovich
However much you like or dislike a particular template engine, using one to handle variable substitution, loops, conditionals, and sub-templates / includes / macros as markup (vs php code) is a huge advance IMO. I use PHPTAL, and I'm sure plenty of people hate it, too, but templates are your php equivalent to master pages.
grossvogel
Here to second CodeIgniter. It makes coding in PHP well-structured for those of us who tend to list off the beaten path a bit. Alternatively, the OP could check out Kohana.
treeface
@grossvogel i can do all that stuff with php...and it won't add a layer of complexity, or another "language" to learn.
Galen
Smarty is PHP written in PHP. While PHP is perfect template engine itself.
Col. Shrapnel
@Galen: Of course you can, but sometimes it's nice to bring in a mature library that does just what you want instead of rolling your own. I've done lots of smaller sites where your approach suits me just fine, but for the huge application I spend most of my time on, the handful of hours learning PHPTAL (which is basically just xml) has already been worth it a hundred times over.
grossvogel
@grossvogel Now php developers that may have to maintain it in the future must spend hours learning phptal as well. I just feel I haven't seen anything done in templates that can't easily be done in native php
Galen
+2  A: 

You can also do it the other way round. Have a main page with header/footer and include only the body.

<!DOCTYPE html>
<html lang="en">
    <head>
        ...
    </head>
    <body>
        <?php include $page ?>
    </body>
</html>
BalusC
Ahh I can see how this would be useful for generating dynamic data. Thank you very much!
drpcken
A: 

What you're doing is ok until you start using "Views" or "Templates" in which case you no longer arrange your content HTML inside the "controller" or "action" running.

Instead you will load a view and populate it with values which leaves all the HTML source ordering to the view and not your PHP file.

$view = new View('layout.php');
$view->header = $header;
$view->content = 'This is the main content!';
$view->footer = $footer;
print $view;

which then loads the layout file which looks something like this:

<!DOCTYPE html>
<html lang="en">
    <head>
        ...
    </head>
    <body>
        <div id="header"><?php print $header; ?></div>
        <div id="content"><?php print $content; ?></div>
        <div id="footer"><?php print $footer; ?></div>
    </body>
</html>
Xeoncross
A: 

This is a perfectly fine method, as long as your site doesn't outgrow the 20 pages threshold. I'd however advise to use include() in function style, not as construct, and place these templates in a separate subfolder. If there is no PHP code in them, also use a .htm file extension (htm designating partial html).

 include("template/main/header.htm");   // would still parse PHP code!

The disadvantage with this approach is, that you somewhen end up injecting HTML through global variables into it. $HEAD='<link...>'; include("../header.htm"). Which is not bad per se, but can quickly amass cruft.

mario
+3  A: 

I include my views from my controllers. I also define file locations to make maintenance easier.

config.php

define('DIR_BASE',      dirname( dirname( __FILE__ ) ) . '/');
define('DIR_SYSTEM',    DIR_BASE . 'system/');
define('DIR_VIEWS',     DIR_SYSTEM . 'views/');
define('DIR_CTLS',      DIR_SYSTEM . 'ctls/');
define('DIR_MDLS',      DIR_SYSTEM . 'mdls/');
define('VIEW_HEADER',   DIR_VIEW . 'header.php');
define('VIEW_NAVIGATION',   DIR_VIEW . 'navigation.php');
define('VIEW_FOOTER',   DIR_VIEW . 'footer.php');

now i have all the info i need just by including config.php

controller.php

require( '../config.php' );
include( DIR_MDLS . 'model.php' );

$model = new model();
if ( $model->getStuff() ) {
    $page_to_load = DIR_VIEWS . 'page.php'
}
else {
    $page_to_load = DIR_VIEWS . 'otherpage.php'
}

include( VIEW_HEADER );
include( VIEW_NAVIGATION );
include( DIR_VIEWS . $page_to_load );
include( VIEW_FOOTER );
Galen
Thank you! This is extremely informative and I have already implemented something similar.
drpcken
Quick question: are you using a framework? I'm trying codeigniter and I'm wondering if it is required of me to define these directory info in the config.php?
drpcken
no thats using my own "framework". you don't have to do that with code igniter
Galen
A: 

I like using functions to print headers and footers instead of includes. You can fine tune the variable scope better that way.

Mark Snidovich
A: 

To summarize all the above.
That's good way to use includes, but do not forget to use a template page for the page contents.

Partly based on Galen's and Balus':

page.php

require $_SERVER['DOCUMENT_ROOT'].'/../config.php';
$data = get_data(); // assume we get all required data here.
$pagetitle = "This is a sample page";
$template = "page.tpl.php";
include "main.tpl.php";

main.tpl.php

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
         <title><?php echo $pagetitle?></title>
    </head> 
    <body> 
        <?php include $template ?> 
    </body> 
</html> 

page.tpl.php something like this:

<h1><?php echo $pagetitle?></h1>
<?php if (!$data): ?>
No news yet :-(
<?php else: ?>
<ul>
<? foreach ($data as $row): ?>
<li><a href="news.php?id=<?php echo $row['name']?>"><?php echo $row['name']?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
Col. Shrapnel