I am trying to make my life a lot easier and make all pages have the same footer and head content from one file and this is what I have so far:
Page with content
<?php
include ("content.php");
echo $page_header;
?>
<div id="content">
</div>
<?php
echo $page_footer;
?>
content.php
<?php
// This is the header which we want to have on all pages
$page_header = include ("resources/content/header.php");
// This is the footer which we want on all pages
$page_footer = include ("resources/content/footer.php");
?>
Header.php Example
<html>
<head>
<title>This is my title</title>
</head>
<body>
<div id="logo">
</div>
Footer.php Example
<div id="footer">Copyright to me!</div>
</body>
</html>
The problem I am having is my header.php content isn't all displaying and causing issues with the page formatting. The header.php does include some php if statements and some in-line javascript... should this matter?
Is there a better way of doing it?
PLEASE NOTE: I am using PHP 5 locally and my server is PHP 4 so the answer needs to work for both
One way is to use output buffering functions for that.
Change content.php file:
ob_start();
include ("resources/content/header.php");
$page_header = ob_get_clean();
ob_start();
include ("resources/content/footer.php");
$page_footer = ob_get_clean();
ob_start() function creates a temporary buffer for any output, then include() makes it's output not to page response, but to buffer that have been created by ob_start(). ob_get_clean() collects contents of a buffer, destroys it and returns collected data as a string.
Another way as mentioned by #u_mulder is to simply include() those files right where they are needed.
Change page with content file:
<?php include ("resources/content/header.php"); ?>
<div id="content">
</div>
<?php include ("resources/content/footer.php"); ?>
However in some time you'll might need some complex template processing engine. There are plenty of them for php.
Related
I am using a very simple template system to avoid having to work through super long html pages using "php include" as in the following:
For example, in my index.php file...
<!doctype html>
<html lang="en">
<?php include 'head.php'; ?>
<body>
<div>
<header><?php include 'nav.php'; ?></header>
<main>
<?php include 'article1.php'; ?>
<?php include 'article2.php'; ?>
<?php include 'article3.php'; ?>
</main>
<footer><?php include 'footer.php'; ?></footer>
</body>
</html>
The php components themselves (e.g., nav.php) are html files. In some cases these components, such as the footer.php will be duplicated in every main page. Is there an easy way to cache the main pages and/or their components (e.g., automatically generate static pages) on the server.
This looks hopeful, https://dzone.com/articles/how-to-create-a-simple-and-efficient-php-cache . Should I even be concern about caching? I know very little about php. I welcome your suggestions. Thanks
If all you are doing is using include statements to wrangle templated pages, you shouldn't be too concerned about performance. The caching process outlined in the article you refer to is designed to reduce "expensive" complexity in script processing. For example, if you have to query a database and the data is not going to change often, it may be good to cache the result. Or if you have very complicated calculations, you may be able to cache common inputs to increase performance.
If you are interested in automatically generating static pages and then serving them, you can use output buffering and save the result to file. Then when someone visits the page, you check to see if the static page exists, if so, give them the contents of that, but if not, run your controller and save the results as a static page. Here is an example:
<?php
//check to see if static file exists
//if so, serve the contents and you're done.
if (file_exists('path/to/static/file/static-file.html')){
echo file_get_contents('path/to/static/file/static-file.html');
exit;
}
//call ob_start(); to begin buffering output
ob_start();
?>
<!doctype html>
<html lang="en">
<?php include 'head.php'; ?>
<body>
<div>
<header><?php include 'nav.php'; ?></header>
<main>
<?php include 'article1.php'; ?>
<?php include 'article2.php'; ?>
<?php include 'article3.php'; ?>
</main>
<footer><?php include 'footer.php'; ?></footer>
</body>
</html>
<?php
$staticFile = ob_get_contents();
file_put_contents('path/to/static/file/static-file.html', $staticFile);
?>
But remember when it comes to performance questions, it is always best to measure before you start addressing the issues. Performance bottlenecks may not be where you first suspect.
You can use PHP built-in OPcache. You can read more about it from PHP manual.
I work on PHP Script and I try to merge it with another file upload ccript I was made before (PHP & HTML Page)
I don't want to make another PHP page and include the old script it, I want the file upload script in the same page of my new script (same file).
I try the method but I only get errors.
And I try every method I found but none.
The idea simply is:
My Current PHP Page:
<?php
<html>
<body>
<!-- here I want the Page 2 in a specific area ( It's responsive ) -->
</body>
</html>
?>
exactly like:
Thank you PROs.
When you include a PHP file in another, everything in that file are copied to second one.
for example:
p1.php:
<?php
echo "hello ";
?>
p2.php:
<html>
<body>
<?php
include "p1.php":
?>
world
</body>
<html>
output of p2.php will be:
hello world
now if you want to use some php scripts you already have, It is recommended to save them as PHP file, completely separated from codes that handle view, and use them anywhere you want. It is better if you make them Class file.
Open the php page where you want to show the another php page and add
<?php include: 'page.php'; ?>
Note: you cannot write html like this
<?php <html> </html> ?> this is wrong
To add the another php page write code like this below
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>
and save it as name.php
you can use query for add a second page into a div,
using this technique you can append one or more pages into a specific div
<?php
<!--HERE WILL BE ADDED THE SECOND PAGE -->
<div id ="content"></div>
<script>
$(document).ready( function() {
$("#content").load("content.php"); //NAME OF THE PAGE TO ADD
});
</script>
?>
use iframe to include te ather page
<iframe src="demo_iframe.htm" width="200" height="200"></iframe>
or in php use
ob_start();
require $action;
$content_for_layout = ob_get_clean();
and echo
$content for layout
or visit php layout
is there a way to include only part of a file using the PHP's include function?
Say inside the document is normal html and the only parts i want are those marked with div id="content" how would i include just the "content" portion?
is not possible such thing, i suggest you to use template files
the with with templates is something like this
html.tpl.php:
<html>
<head>
<?php echo $html_headers; ?>
</head>
<body>
<?php echo print_content_div(); ?>
</body>
</html>
my_php_page.php:
$html_headers='your headers';
include 'content_div.php';
include 'html.tpl.php';
content_div.php:
<?php
function print_content_div(){
echo "<div id='content'>Your content here</div>";
}
this way you can write huge pages without repeating your code
I'm looking for ways to have my pages search for the page layout from an external template page. Please see the below example.
<head>
<title></title>
</head>
<body>
<search for header, css, layout, etc from external page>
Page contents
<search for footer>
</body>
Is there any way to do this using PHP or HTML? I want to be able to edit the layout for all the pages without having to do it page by page. I welcome any other means to achieve the same effect as long as it works on all the browsers.
Thank you very much!
This is exactly the sort of thing that PHP is for. A PHP script can include the contents of another script using the include statement.
So each page in your application could have an associated PHP script that generates the contents, and includes footer.php for the footer layout. In this way, when you change footer.php all the pages that use it will automatically get the changes.
You can't do this with pure HTML, though you could with some javascript and Ajax.
Like Andrew said, use includes. I'll set up 2 basic examples.
The simplest, have multiple layout files that are called by your main file(s):
header.php:
<div id="header">
Menu can go here.
<?php echo 'I make all my files .php, so they can use PHP functions if needed.'; ?>
</div>
footer.php
<div id="footer">
Footer Link
</div>
index.php
<html>
<head></head>
<body>
<?php include('/path/to/header.php'); ?>
Specific index.php content here.
<?php include('/path/to/footer.php'); ?>
</body>
</html>
The other option is to have one PHP file which includes all your different layout elements in functions. The reason I like this, is because you can include one file and then call specific functions for different parts. This can also be used to pass variables like a title of a page.
layout.php
<?php
function makeHeader($title) {
return 'My title is: '.$title;
}
function makeFooter() {
$html = '
<div id="footer">
Footer Link
</div>
';
return $html;
}
?>
index.php
<?php include('/path/to/include.php'); ?>
<html>
<head></head>
<body>
<?php echo makeHeader('Page Title'); ?>
Specific index.php content here.
<?php echo makeFooter(); ?>
</body>
</html>
Just make sure you use relative paths (no http://www.) when including files. This will allow variables and functions to transfer over smoothly. The easiest way to do this is using the PHP variable $_SERVER['DOCUMENT_ROOT'] so if you have a file http://mysite.com/includes/layout.php, you could include it with include($_SERVER['DOCUMENT_ROOT'].'/includes/layout.php') no matter where your file you are including from is located.
Something basic that i don't understand:
I have header.php with navigation bar for my site. Inside it, there's a <head>...</head> section.
Now, in each other page of my site, I'm using require_once 'header.php' so that each page will show the navigation bar. But, I need also specific <head>...</head> sections to the different page.
For example, in page customers.php, I'm using <script>...</script> to include the jQuery library. I don't need to include it in other pages.
Now, searching the web I see that multiple head tags is wrong syntax.
So, how can anyone:
avoid multiple "head" tags
WHILE
separating his work to different PHP files and including them ?
You have to change your page structure and employ templates.
Instead of loading header at the top of the code, you have to do it at the bottom!
And page code should output not a word, but collect all data in variables.
And only after that output can be started by calling template.
A example layout is going to be like this:
First. page itself.
it outputs nothing but only gather required data and calls a template:
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.php";
include "template.php";
?>
Next, template.php which is your main site template, consists of your header and footer:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
And, finally, links.php is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><?=$row['name']?></li>
<? endforeach ?>
<ul>
easy, clean and maintainable.
there are many advantages in such approach:
as requested, you can populate header with actual page-relevant data.
HTTP headers can be sent as well, before any output. It includes cookies, sessions, cache-control and many more.
it's 2011 today. AJAX era. You may wish change your code to return JSONed data instead of whole HTML page. It's going to be easy using such layout.
Imagine you're going to create very similar site with just different design. You will have to change only templates and don't touch engine files. That's really great advantage of using templates.
Here are some simple ways you can look at.
You can have jQuery on the pages
that don't need it; once it's
downloaded it will be cached so it
still wont use more bandwidth.
You can move out the closing </head>
tag from header.php and close the
<head> tag in the page that's including
header.php.
You can include javascript anywhere
on a page, not only in the header.
You can also do something like this.
Before you do require_once 'header.php'; you put a variable called $jquery = true;
In your header.php file you check if $jquery is set to true, if it is, you include jQuery.
in header.php
you can type like this
<head>
<?php echo $script; ?>
</head>
then in your customers.php
you can first assign the variable
$script = '<script>...</script>'
then
require_once 'header.php'
One possible solution.
You create a global variable before including header.php.
You test this variable in header.php.
If it is true, You print script or something. Something like this:
<!-- Fragment of header.php -->
<?php if ($i_want_jquery): ?>
<script ...>
...
</script>
<?php endif; ?>
On the other hand, a template may be a better solution.