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?
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_VIEWS . 'header.php');
define('VIEW_NAVIGATION', DIR_VIEWS . 'navigation.php');
define('VIEW_FOOTER', DIR_VIEWS . '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 );
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>
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><?php echo $row['name']?></li>
<?php endforeach ?>
</ul>
<?php endif ?>
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>
The accepted answer is form 2010 and things have changed in the past ten years.
The way to go, now with composer having replaced most hand wired autoloaders, the best practice is to use a single require_once utilizing __DIR__ from a script in a fixed, known place:
require_once __DIR__ . '/vendor/autoload.php';
Using define() is not common anymore.
According to the environment agnostic approach, dependencies from the environment get injected to the application using .env or similiar.
The good practice nowadays is to use a templating engine, such as smarty. For the whole application consider using a framework, like codeigniter.
For small sites, include/include_once and require/require_once are great, I haven't built a site without them in years. I would, however, recommend making sure each of your include files is a discrete code block that is valid XML. What I mean is don't open a tag in one include and close it in another, or vice versa - it will make changes complex and more prone to break things because you have dependencies between files. Happy coding!
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.
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:
<?php
define("DOC_ROOT", "/var/www/htdocs/"); # Add a trailing slash to make it a little easier
function GetHeader()
{
return include DOC_ROOT . 'includes/parts/header.htm'; # Header found at include/parts/header.htm
}
function GetFooter()
{
return include DOC_ROOT . 'includes/parts/footer.htm'; # Footer found at include/parts/footer.htm
}
?>
And used as such:
<?php
# From the main page (/var/www/htdocs/index.php)
require_once 'includes/functions/template-parts.php';
GetHeader();
?>
<!-- Page content -->
<?php
GetFooter();
?>
I like using functions to print headers and footers instead of includes. You can fine tune the variable scope better that way.
Related
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?
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_VIEWS . 'header.php');
define('VIEW_NAVIGATION', DIR_VIEWS . 'navigation.php');
define('VIEW_FOOTER', DIR_VIEWS . '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 );
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>
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><?php echo $row['name']?></li>
<?php endforeach ?>
</ul>
<?php endif ?>
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>
The accepted answer is form 2010 and things have changed in the past ten years.
The way to go, now with composer having replaced most hand wired autoloaders, the best practice is to use a single require_once utilizing __DIR__ from a script in a fixed, known place:
require_once __DIR__ . '/vendor/autoload.php';
Using define() is not common anymore.
According to the environment agnostic approach, dependencies from the environment get injected to the application using .env or similiar.
The good practice nowadays is to use a templating engine, such as smarty. For the whole application consider using a framework, like codeigniter.
For small sites, include/include_once and require/require_once are great, I haven't built a site without them in years. I would, however, recommend making sure each of your include files is a discrete code block that is valid XML. What I mean is don't open a tag in one include and close it in another, or vice versa - it will make changes complex and more prone to break things because you have dependencies between files. Happy coding!
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.
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:
<?php
define("DOC_ROOT", "/var/www/htdocs/"); # Add a trailing slash to make it a little easier
function GetHeader()
{
return include DOC_ROOT . 'includes/parts/header.htm'; # Header found at include/parts/header.htm
}
function GetFooter()
{
return include DOC_ROOT . 'includes/parts/footer.htm'; # Footer found at include/parts/footer.htm
}
?>
And used as such:
<?php
# From the main page (/var/www/htdocs/index.php)
require_once 'includes/functions/template-parts.php';
GetHeader();
?>
<!-- Page content -->
<?php
GetFooter();
?>
I like using functions to print headers and footers instead of includes. You can fine tune the variable scope better that way.
Currently I am building a website in which I am trying to have one location (e.g. header.html or header.txt) to edit content on multiple pages (e.g. each page has the header). I assumed I could easily do this using PHP, importing the html code into the pages using
<?php
echo file_get_contents("header.txt");
// or echo file_get_html("header.html");
?>
Does not seem to be working. Any suggestions on how to do this? I want to be able to edit the header across all pages from one location.
Take care!
EDIT: Okay so I think I am making a simple mistake that prevents the php to run. Just to lay out what I have and what I want to do:
1: I have a piece of code that represents the header that I want to include on multiple pages. This is currently saved in a header.html file.
2: I have a webpage saved as trial.html where I am trying to place that piece of code using php.
Am I forgetting something?
You are making it too complicated:
<?php
require_once('header.php');
It does not matter if header.php contains just html, just php, or some mix of both.
Use PHP files, no plain html or txt because PHP can be protected to prevent directly access, include more parameters and your project can be more modular.
When you include(or require) php files, those are included in the main file before page loads (server side), so you can have more variables in it.
header.php
<?php
echo '<title>Welcome</title>';
body.php
<?php
echo '<h1>Hellow world!</h1>';
$custom_page = 'page 01';
footer.php
<?php
echo '<p>My site footer for ' . $custom_page . '</p>';
index.php
<?php
// debug errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Note: __DIR__ is absolute path for current file (index.php)
// if you have to go up one dir:
// include_once __DIR__ . '/../your_file.php';
// if you have to go down one dir:
// include_once __DIR__ . '/subdir/your_file.php';
/* more code */
?>
<html>
<header>
<?php include_once __DIR__ . '/header.php'; ?>
</header>
<body>
<?php include_once __DIR__ . '/body.php'; ?>
<?php include_once __DIR__ . '/footer.php'; ?>
</body>
</html>
Rendered HTML for index.php
<html>
<header>
<title>Welcome</title>
</header>
<body>
<h1>Hellow world!</h1>
<p>My site footer for page 01</p>
</body>
</html>
Recommended reading first page on google result:
PHP Getting Started
How To Start Programming
Getting Started With PHP: Basic Scripts
Problem ended up being in the path towards the file I wanted to include, as well as a multitude of other issues:
header.html -> header.php
index.html (page) -> index.php
get_contents -> include
include('/path/file') -> include 'path/file'
add set_include_path($_SERVER['DOCUMENT_ROOT']);
Thanks everybody for you input!
I'm looking for advice on the best practice for separating site content up into logical blocks. I want a header and footer that are constant throughout the site, so that if I have several pages of different content, they will all look as below — changes made to the header and footer then update automatically without me having to change each individual page.
<?php
include 'header.php';
?>
<body>
<p>page content here</p>
</body>
<?
include 'footer.php';
?>
The header.php would contain the opening <html>, <head> and static content, and the footer.php would contain any extra static content and the closing </html> tag. So, my question is: Is this a good approach? I'm worried that spreading the <html> tags across multiple files is bad practice. If so, what is the right way to approach this kind of design?
Nope, your approach is wrong.
Here are main faults in your design:
You're assuming that header.php would be called on the every page call. That's wrong.
You're assuming that header.php will always be static. That's wrong.
You forgot to create a template for the page itself.
The main rule everyone have to learn by heart:
Not a single character has to be sent into browser, until all data gets ready.
Why?
it's 2011 today. AJAX era. What if your code will have to send JSONed data instead of whole HTML page?
there is a thing called HTTP header. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent.
it's for just 4-page site. Okay. Imagine you've got lucky and got a request for another 4-page site. You will have to change only templates and don't touch engine files. That's really great benefit.
Imagine you're going to make a custom <title> tag for your pages, based on the page content. Isn't it extremely common thing? But you can't make it without using templates.
So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.
An example layout is going to be like this:
.1. page itself.
it outputs nothing but only gather required data and calls a template:
<?php
//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.tpl.php";
include "template.php";
?>
.2. 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">
<?php include $tpl ?>
</div>
</body>
</html>
.3. and finally links.tpl.php is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<?php foreach($DATA as $row): ?>
<li><?=$row['name']?></li>
<?php endforeach ?>
<ul>
easy, clean and maintainable.
In building off of Your Common Sense's answer, there's not a good reason to have 2 files for every page. You can easily combine your template (YCS called this .tpl.php) and your actual page into one file.
First, start off with a class that you can expand as your template needs expand:
<?php
#lib/PageTemplate.php
class PageTemplate {
public $PageTitle;
public $ContentHead;
public $ContentBody;
}
Then, make your layout:
<?php
# layout.php
require_once('lib/PageTemplate.php');
?>
<!DOCTYPE HTML>
<html>
<head>
<title><?php if(isset($TPL->PageTitle)) { echo $TPL->PageTitle; } ?></title>
<?php if(isset($TPL->ContentHead)) { include $TPL->ContentHead; } ?>
</head>
<body>
<div id="content">
<?php if(isset($TPL->ContentBody)) { include $TPL->ContentBody; } ?>
</div>
</body>
</html>
And finally, add your page with the body content:
<?php
#Hello.php
require_once('lib/PageTemplate.php');
# trick to execute 1st time, but not 2nd so you don't have an inf loop
if (!isset($TPL)) {
$TPL = new PageTemplate();
$TPL->PageTitle = "My Title";
$TPL->ContentBody = __FILE__;
include "layout.php";
exit;
}
?>
<p><?php echo "Hello!"; ?></p>
This is a basic approach but, yeah, it does work :) I sure would bother with a lot of templating and OOP but you are definitely on the right path
As i can't comment anymore, then i will answer here ;) If he need a custom title then he needs some more advanced functions. So, as i told, this is a basic approach. But in the end, if he really have a static header/footer, and really use them everywhere, well, yes, this is a good way to go.
So ofc you could bother with some advanced headers with parameters you could feed on each page. You could go on a whole MVC stuff. In the end just tell him to use a pre-made framework and stop bothering. How could he learn if you don't let him do some trial and error ?
index.php -- includes header, footer, and content based on REQUEST variable.
header.php -- header content
footer.php -- footer content
content1.php, content2.php, etc.
index.php:
<?php
include ('header.php');
// VERY IMPORTANT - do not use the GET variable directly like this
// make sure to filter it through a white-list
include(basename($_GET['page']).'.php');
include ('footer.php');
?>
if you want the URL to go www.domain.com/pagename where the page you're trying to load into index.php is "pagename", use HTACCESS and do some URL Rewriting: http://corz.org/serv/tricks/htaccess2.php
In my file index.php I have included this text from another file.
<title>
<?php
$title = "";
if (basename(__FILE__, ".php") == "index") {
$title = "Home";
} else {
$title = ucfirst(basename(__FILE__, ".php"));
}
echo $title;
?>
</title>
And I guess you can see what i does, and if not, then it's supposed to set the title to the basename of the file. So say you have a file called downloads.php, then the title with this script would be Downloads. But I have this problem which I don't know how to get past. When I include the text via.
<head>
include "filename.php";
</head>
And my problems is when I include the text, the 'script' runs before it includes. Say if the name of the file you included is filename.php and the main page where you have included the text is main.php, the header would be Filename and not Main. And the reason why I want to include the text, and not just paste directly into main.php is because it's much easier to edit if you have multiple files where you need the exact same code.
I hope you understand what I'm asking, and that you are able to help me.
Try $_SERVER['SCRIPT_NAME']:
if (basename($_SERVER['SCRIPT_NAME'], ".php") == "index") {
$title = "Home";
} else {
$title = ucfirst(basename($_SERVER['SCRIPT_NAME'], ".php"));
}
It looks like you're trying to set up a simple system for breaking your pages up into "subpages". Good idea. But rather than relying on the filename, why don't you set a variable? For example:
In page_title.php:
<?php
echo "<title>" . $title . "</title>";
In index.php:
<?php $title = "Home"; ?>
<head>
<?php include "page_title.php"; ?>
</head>
In some_other_page.php:
<?php $title = "Some Other Page"; ?>
<head>
<?php include "page_title.php"; ?>
</head>
And the same for any other pages you want...
In fact, you probably want to encapsulate the whole <head></head> section of your page.
Finally, rather than reinventing the wheel, you might want to look at some of the templating engines out there for PHP. I like Smarty, but there are others. These templating engines make it possible to write template files (including other template if necessary) and make it easy to simply pass variables to the template & render your HTML.
I'm new to PHP (3 days) and I understand the basic concept of using includes for creating high level templates. I'd like to further utilize this tool to load more granular content, but I'm not sure how to approach this. The html below is an example of a page template put together with php includes. If you reference this html, let's say I have a widget on the page contained here: <?php include('include/WIDGET.php'); ?>. In the simplest of scenarios, there would be a link above the widget that reads "Widget 2". On click of this link, I would want the WIDGET.php content to be replaced with widget2.php. How can I manipulate the include to load widget2.php?
HTML
<!-- File: index.php -->
<html>
<head>
<?php include('include/head.php');?>
<title><?php echo $siteName;?></title>
</head>
<body>
<?php include('include/header.php'); ?>
<!-- CONTENT -->
<?php include('include/WIDGET.php'); ?>
<?php include('include/main-content.php'); ?>
<!-- CONTENT END -->
<?php include('include/footer.php'); ?>
</body>
</html>
To change what file(s) is included, you would need to provide some sort of parameter to the page, that can conditionally include the correct widget.
For example, including it in URL query string, such as:
http://yoursite/index.php?content=widget2
Then, in your PHP file, you can get this value:
if (isset($_GET['content']) && !empty($_GET['content'])) {
$widget = $_GET['content'];
} else {
$widget = 'widget';
}
And include it in your HTML:
[...]
<!-- CONTENT -->
<?php include('include/' . $widget . '.php'); ?>
[...]
This is just to give you an example of the logic involved, but I wouldn't use the code as I've provided it as it is incredibly insecure, and doesn't check the existence of files, etc.
I know you're just getting started, and it's a lot to take in at once, but you might want to consider using a PHP framework such as Zend or Symfony, or CakePHP to take advantage of some routing and templating solutions that have already been set up for you.