Connecting Tech Pros Worldwide Help | Site Map

Neat way to use Headers and Footers in PHP

Newbie
 
Join Date: Mar 2008
Posts: 1
#1   Mar 26 '08
I found that I couldn't edit my page easily using programs like dreamweaver after I had divided the template into header and footer files.

Using this method you can load 'template.php' into your visual designer and edit it no probs.

Note:Examples below are basic but you get the point


template.php

Expand|Select|Wrap|Line Numbers
  1. <?php if($hf=='header'){ ?>
  2.  
  3. <html>
  4. <head>
  5. <title>Page Title</title>
  6. </head>
  7. <body>
  8.  
  9. <?php } elseif($hf=='footer') {?>
  10.  
  11. </body>
  12. </html>
  13.  
  14. <?php } ?>
  15.  

header.php

Expand|Select|Wrap|Line Numbers
  1. <?php $hf='header'; include('template.php')?>
  2.  

footer.php

Expand|Select|Wrap|Line Numbers
  1. <?php $hf='footer'; include('template.php')?>
  2.  

Last edited by Markus; Dec 6 '08 at 03:10 PM. Reason: Fixed [code] tags.



Newbie
 
Join Date: Apr 2008
Posts: 1
#2   Apr 3 '08

re: Neat way to use Headers and Footers in PHP


I think that is not as 'neat' as it could be. And, it is also over-complicating it.

Just make 2 functions like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function page_header() {
  3. echo '
  4. <html>
  5. <head>
  6. <title>Page Title</title>
  7. </head>
  8. <body>
  9. ';
  10. }
  11.  
  12. function page_footer() {
  13. echo '
  14. </body>
  15. </html>
  16. ';
  17. }
  18. ?>
And then just enter either 'page_header()', or 'page_footer()' where you want the headers or footers

Cameron

Last edited by Markus; Dec 6 '08 at 03:11 PM. Reason: Fixed [code] tags.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#3   Jun 24 '08

re: Neat way to use Headers and Footers in PHP


Quote:

Originally Posted by meep

I think that is not as 'neat' as it could be. And, it is also over-complicating it.

Just make 2 functions like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function page_header() {
  3. echo '
  4. <html>
  5. <head>
  6. <title>Page Title</title>
  7. </head>
  8. <body>
  9. ';
  10. }
  11.  
  12. function page_footer() {
  13. echo '
  14. </body>
  15. </html>
  16. ';
  17. }
  18. ?>
And then just enter either 'page_header()', or 'page_footer()' where you want the headers or footers

Cameron

Agreed.

I'm not understanding the whole use of $hf
rohypnol's Avatar
Member
 
Join Date: Dec 2007
Posts: 54
#4   Jul 29 '08

re: Neat way to use Headers and Footers in PHP


Quote:

Originally Posted by meep

Just make 2 functions like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function page_header() {
  3. echo '
  4. <html>
  5. <head>
  6. ...
  7. ...

Please don't put HTML code in PHP... I can hardly stand to see PHP code in HTML, sometimes it may be OK, but please never ever put HTML code in PHP. It's a very bad practice, unfortunately very common. The best way to do it is using a templating engine such as Smarty but if you don't want to get through the hassle of working with Smarty because you're just learnign PHP, use <?php echo $var; ?> inside your HTML. It will keep things a lot cleaner and working with the HTML will be a lot easier.

The best way I've found to use headers and footers in PHP without a templating engine may something like this:

header.php
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title><?php if (isset($title)) echo $title; ?></title></head>
  3. <body>
page_xxx.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $title = 'my page';
  3. require('header.php');
  4. if (isset($_POST['name'])) echo 'Hello, '.$name;
  5. require('footer.php');
  6. ?>
footer.php
Expand|Select|Wrap|Line Numbers
  1. </body>
  2. </html>
Any page you have goes in stead of page_xxx.php;
header.php and footer.php should be included in all (or most of :) your files.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#5   Dec 6 '08

re: Neat way to use Headers and Footers in PHP


Quote:

Originally Posted by rohypnol View Post

Please don't put HTML code in PHP... I can hardly stand to see PHP code in HTML, sometimes it may be OK, but please never ever put HTML code in PHP. It's a very bad practice, unfortunately very common. The best way to do it is using a templating engine such as Smarty but if you don't want to get through the hassle of working with Smarty because you're just learnign PHP, use <?php echo $var; ?> inside your HTML. It will keep things a lot cleaner and working with the HTML will be a lot easier.

The best way I've found to use headers and footers in PHP without a templating engine may something like this:

header.php

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title><?php if (isset($title)) echo $title; ?></title></head>
  3. <body>
page_xxx.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $title = 'my page';
  3. require('header.php');
  4. if (isset($_POST['name'])) echo 'Hello, '.$name;
  5. require('footer.php');
  6. ?>
footer.php
Expand|Select|Wrap|Line Numbers
  1. </body>
  2. </html>
Any page you have goes in stead of page_xxx.php;
header.php and footer.php should be included in all (or most of :) your files.

If someone is a newbie, I don't see the problem with this coding style. They'll learn to separate the two later on in their learning.
FLEB's Avatar
Newbie
 
Join Date: Aug 2008
Posts: 30
#6   Dec 17 '08

re: Neat way to use Headers and Footers in PHP


Using the if/thans is a bit much, but using an echo doesn't achieve the goal evilrevolution was talking about: making the editor colorize/WYSIWYG the HTML as HTML, not as a long PHP string.

By coming out of PHP mode, the HTML code goes back to being colorized/WYSIWYG'd with HTML rules, not PHP rules.

However, it could be just as easily achieved by:

Expand|Select|Wrap|Line Numbers
  1. <?php function header() { ?>
  2. <html>
  3. <title>Title goes here</title>
  4. <body>
  5. ...Header, header, header...
  6. <?php } ?>
  7.  
  8. <?php function footer() { ?>
  9. ...Footer, footer, footer...
  10. </body>
  11. </html>
  12. <? } ?>
  13.  
The HTML, while logically wrapped in a PHP function, is not within a <?php ?> block, and is not being interpreted (by the WYSIWYG/code-coloring editor) as PHP code.
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#7   Jan 6 '09

re: Neat way to use Headers and Footers in PHP


I think smarty is the way to go. i don't care how new you are, smarty is easy to use.

<vent>
Besides you should get into classes objects and security as soon as possible. It pains me to sift through tens and hundreds of /scripts/ of PHP from people who actually are responsible for giving PHP a bad name.

PHP is a beautiful language with a bad rep from newbies making insecure, vulgar, and to be blunt horrible code. I was there. 4 years ago I didn't know much PHP at all. I learned OOP first, then dissected PHP applications, then started coding and solving first hand problems.

I despise the sight of PHP in HTML now. Your HTML file should not contain no logic other than one or two if and loop statements. No assignment and declarations. NO SQL! No data validations and most importantly no access to variables that have not passed through your PHP script (ie doing something like $_POST['foo'] on a page).

</vent>


Dan
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,078
#8   Jan 6 '09

re: Neat way to use Headers and Footers in PHP


May I join the discussion.
I agree with rohypnol
Quote:
Please don't put HTML code in PHP
I handle Headers and Footers the same way I handle all HTML within PHP by using include.
Expand|Select|Wrap|Line Numbers
  1. include 'header.html';
  2. .............................
  3. some php.....
  4. include 'webForm.html';
  5. more php .............
  6. include 'footer.html';
All HTML is neatly out of the way in their own files.
The only HTML I have never succesfully placed in a HTML file is a
drop down <select> created on the fly within a loop from a database query.
This is better placed in a function.

I have nothing against meep's function idea, in fact a function will parse a lot quicker than include if faster parsing is needed.
I would dump all HTML functions in a seperate file say htmlFunctions.php and
include this at the beginning.

But I think the OP's real problem was reading his code in Dreamweaver,
may I suggest phpDesigner.
Reply