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?
The good practice nowadays is to use a templating engine, such as smarty. For the whole application consider using a framework, like codeigniter.
I know this is very late, just wanted to add my "pennies worth" to this question.
My suggestion would be to create methods for this, e.g my root is: var/www/htdocs/ and the functions file is in includes/functions/template-parts.php.
My functions would look as such:
And used as such:
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.
which then loads the layout file which looks something like this:
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).
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.You can also do it the other way round. Have a main page with header/footer and include only the body.
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
main.tpl.php
page.tpl.php
something like this: