html extract header and footer to separate documents - php

I have several html documents which share the same header and footer and would like to extract header and footer to separate html files and include them to other html documents.
The idea is very simple: when I change something in header, the change has to be made on all pages.
Until now I used PHP include function. But I have read some articles that this has impact on performance.
What is the best way to do this for clean html pages ?
Is the PHP the way to go ?
If it is, should I insert the whole html content into PHP echo ?

There isn't really a good way doing it in pure HTML, since iframe or an ajax-request using Javascript wouldn't be a good solution here.
I would say the best way is to use the PHP include or PHP require-function:
http://php.net/manual/en/function.include.php
http://php.net/manual/en/function.require.php
You won't see any performance issues. Remember that include is a little bit faster than include_once, since include_once will have to make an extra check if the file has already been included.

You want to include header in all html pages, so php include is better.
no need of echo, just create a header.html(or any html file).
Then use
<?php
include "header.html"; //or name of your headerfile
?>
add this php code to all your pages.Thus all pages will point to same header file, and i dont think it will affect performance. Same idea for footer too :)

Related

PHP includes header or footer location

I was hoping someone could help. I have just started to dabble with PHP includes for time saving in the future. For example I want to change the footer and header on a web page once (using include) instead of copying and pasting the code 30 or 40 times - oh no... a typo start again.
Which brings me to the question(s) where is it best to place this script?
<?php include("includes/headernav.html"); ?>
Can it be placed in a div, or should it be placed at the top of your code under the body?
If I want to make an image/banner include module. Can I
<?php include("includes/image.jpg"); ?>
Or is best to wrap the image in html and apply like this?
<?php include("includes/imagewrapped.html"); ?>
Do not include .jpeg files directly, use a wrapper. Only use include with other PHP files.
As for including the header, do it any way that feels natural as long as it produces valid html. There is no particular reason to declare another div element.
Hope this helps:
<?php include("includes/ui_header.php"); ?>
My page content between header and footer
<?php include("includes/ui_footer.php"); ?>
You can probably save this as a function and call that function wherever you want to display.
It doesn't matter whether you put include in any place. However, it's better to put include in the top or bottom of your code
While including headers/footers/menus on the site, please keep in mind following things:
1) Your header/footer includes(blocks) should be wrapped inside a div.
2) This way then can be differentiated and any new change to them can be done easily.
3) Its always a good practice to include a wrapper div around an element as CSS can use it for styling.
4) Your header/footer includes (blocks) should have a flexibility that even we place them in header,footer or any sidebar, they should not disturb the UI.
1) Because you are including the HTML file, you probably need to include it where you want to display it.
2) Create a function in php where you send only image URL (maybe some other parameters) and function returns the HTML code (String) which you only echo on page where you want to display it. This way you can ensure, that all images will have the same code and styling.
for example
function generateImage($url=null) {
if (isset($url)) return '<img src='.$url.' style="width: 100px; height:100px; border: 1px;" />';
else return false;
}
The better way is to include always a php file.

Minimize code size to avoid duplication of the same code

I need one advice from you. I am working on a website, which uses PHP and HTML. As the biggest part of the header and footer code will be same for many pages, I am thinking of using PHP's include to avoid code duplication. But, each of those pages requires different stylesheets and JS files included. What do you think how could I let the other file know what scripts and stylesheet to import?
Our company does this:
The header reads the filename of the page calling it when it's included.
Then, it changes the extension to '.js' and outputs that if it exists. Same for CSS.
So if I have a page "register.php", it will auto-include "register.js" and "register.css" if they exist.
Here's what I do:
<?php include("includes/headContent.php"); ?>
<title>Page title goes here!</title>
<script src="script_only_used_on_this_page"></script>
<?php
require_once("includes/siteHeader.php");
?>
Site Content Goes Here!!
<?php
require_once("includes/siteFooter.php");
?>
Head Content includes any PHP I want included in every page, as well as the opening html and head tag, and any Javascript libraries and css stylesheets I want on every page. Site header closes the /head tag, and opens the body as well as printing out my site header and some other markup that goes on every page. Finally Site Footer closes out my template. Everything in between is my content area!
There are lots of different ways you can do templating, if you wanted to create a simple include and an echoHeader() and an echoFooter() function... just have the echoHeader function accept a parameter which you would pass your javascript and CSS lines to.
you can use MVC coding pattern

PHP file include general query

In a website say abc.com, I have products, about and contact page. These pages have same Header, Footer section. Should we include header and footer with PHP or We copy/paste HTML/CSS coding done in Index page? Which is the best proctise?
Use PHP to include your header and footer, that way if you ever need to make a change you only have to edit one file to update the entire website.
You can set up a cool method like this:
<?php
$title = "This is the page title"; // Make your header use this
require "Header.php";
?>
Put the page contents in between.
<?php
require "Footer.php";
?>
The widely accepted way is to have one central file (e.g. index.php) that has the header and footer (you can choose to include them or just have them in the file), and that central file includes the content files in its content area.
There are other ways to do this, but definitely do not copy&paste the same header/footer to many pages as that will make future editing a nightmare.
You should use PHP to include them.
Also you could think about using a Template-Engine which would offer you some nice features
like Nesting content and defining base-layouts which you can extend.
Twig has those features:
http://twig.sensiolabs.org/
create file like footer.php
Set your code in this and you can add this in index.php page like below
<?php
require_once( 'footer.php' );
?>
same way for header

Include a repetitive chunk of HTML

I basically have a div on my site that always has the same stuff. However, this div is not present on all pages which is why I won't use the dynamic web template. I was wondering if it was possible for PHP to get the code from a document on the server and put in into the div?
Ex:
<div id="section...
then my text file contains
<p>hello</p>
Basically I want PHP to put it into the div when the user sees it.
If theres a smarter way of acheiving this I'd be open to it as well.
Thanks
Simply include() the file. The interpreter will drop out of PHP mode back into HTML mode, outputting anything it encounters.
Did you try this in the first place?
<div id="section"><?php include ('path-to-your-file') ?></div>
Just have a file with some echo statements that generate your div and then include it only when required.
How to control whether to include it or not depends on how your website is built.
For instance if you have DB entries for your pages add a column called includediv (you can probably come out with a better name...) and if that is true you include the file otherwise not.

Breaking up PHP Websites

I am wondering how I can break up my index.php homepage to multiple php pages (i.e. header.php, footer.php) and build a working index.php page using those separate php pages. I know WordPress uses this with different functions like:
GetHeader();
GetFoodter();
But when I tried to use those functions, it errors. I am guessing they are not native functions to PHP.
What would I need to do to get this functionality?
include 'header.php';
include 'footer.php';
Go with an MVC framework like Zend's. That way you'll keep more maintainable code.
You could do the following:
<?php
include('header.php');
// Template Processing Code
include('footer.php');
?>
The include() statement includes and evaluates the specified file.
so if you create index.php as:
<?php
include("1.php"); include("2.php"); include("3.php");
?>
processing it will combine three php files (result of parsing them by php) into output of your index.php ... check more at http://pl.php.net/manual/pl/function.include.php
Also, if i recall correctly, you can also use
<?php
require('filename');
?>
the difference being, if php can't find the file you want to include, it will stop right there instead of keep excecuting the script...
If your server is configured accordingly, you can use PHP's built in auto append/prepend settings and set it in a .htaccess file:
php_value auto_prepend_file "header.php"
php_value auto_append_file "footer.php"
Info:
www.php.net/manual/en/configuration.changes.php#configuration.changes.apache
www.php.net/ini.core#ini.auto-prepend-file
www.php.net/ini.core#ini.auto-append-file
I realize this is an old question, which already has a perfectly valid accepted answer, but I wanted to add a little more information.
While include 'file.php'; is fine on it's own, there are benefits to wrapping these sorts of things up in functions, such as providing scope.
I'm somewhat new to PHP, so last night I was playing with breaking things into files such as 'header.php', 'footer.php', 'menu.php' for the first time.
One issue I had was that I wanted to have the menu item for a page/section highlighted differently when you were on that page or in that section. I.e. the same way 'Questions' is highlighted in orange on this page on StackOverflow. I could define a variable on each page which would be used in the include, but this made the variable sort of global. If you wrap the include in a function, you can define variables with local scope to handle it.
You could also look into a template engine like Smarty. That way you define the the header and footer and all other common elements in a single file, then fill in the rest through smaller templates or direct output.
Use include statements to just include those files to your Page
I think it's
include '[filename]'

Categories