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.
Related
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
I just learned how to include php .Here's the index or main php file
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php include 'header.php'; ?>
</body>
</html>
now in header.php file which way is better to print html
Way 1 directly use html without php
<header>
<h1>Header</h1>
</header>
Way 2 Using php and echo
<?php
echo '
<header>
<h1>Header</h1>
</header>
'
?>
Another quick question. Will it work if I use .html for the base or index file ??
sorry for my bad english
Directly use HTML without PHP:
<header>
<h1>Header</h1>
</header>
As for your second question:
The file you're using include() in must have .php extension, but the file that's being included doesn't necessarily need the .php extension. The .html extension would work fine as well.
Just include the PHP file.
BTW, in case you haven't, read about this too:
Difference between require, include and require_once?
HTH.
you should use include_once ;-)
http://us2.php.net/manual/en/function.include-once.php
There are a few ways to go around it. Rather than echoing everything, I like to go like this:
<title><?php echo $title; ?></title>
Or if I have a nice block to work with, maybe within an if statement, I also like to go like this :
<?php if($weather = "sunny") { ?>
<div id="sunny">
<p>It's a beautiful day outside.</p>
</div>
<?php } // end if($weather = "sunny")
else { ?>
<div id="sunny">
<p>Today is yucky.</p>
</div>
<?php } ?>
The answer is sometimes different. If you use the same header on a lot of pages, use Way1. If you're only doing this in one place, you want to use Way 2 (or keep it in html), since it's more readable at quick glance to someone studying the page.
You can use .html for the file if you change the server config to tell it to look at it for php first, but you shouldn't do that, it just increases complexity and may have unintended consequences if you do it wrong.
Use .php or .phtml for files with any amount php in it. The only distinction you can make is to use .phtml files for files that are mostly html.
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.
I have some html files that are exactly the same, except for a little area where goes some text. And I was thinking if there was a way to save writing the same text in the 7 files.
I thought on a possible soluction, but I find it a bit messy:
I split the page in 2 parts, upper.php and lower.php:
upper.php
<html>
<head>
...
</head>
<body>
...
<div id=content>
lower.php
</div>
...
</body>
</html>
And then I write the different pages like this:
home.php
<? include "upper.php" ?>
Welcome to my webpage where I do random stuff.
<? include "lower.php" ?>
contact.php
<? include "upper.php" ?>
Contact me by sending an email to asdf#a.org
<? include "lower.php" ?>
etc..
I suspect that this is a very dirty way to do it, any other workaround?
That's how I do all my websites. Sometimes there'll be settings before the first include (such as to determine if the user must be logged in or not), but for the most part that's how I like to do things.
The only real issue is to make sure that you don't get mixed up if you have files in different folders. Other than that, you should be good.
I'm done creating a php website that has 6 pages, and the structure of the pages is the same for each one of them, the only thing that changes is the content, so is the same header, same design and same footer, the only thing that changes like I said before is the content itself.
so i was thinking instead of having many pages, I could have only one design page, and change only the content, what do you recommend?,and how do I do that?, also im not planning installing anything like Typo3, wordpress, joomla or whatever in my server, so I want something i could do using php idk. thank you!
Simplest solution is to create separate files.
header.php
footer.php
menu.php
In header.php put your code from header
<?php ?>
<HTML>
<HEAD>
</HEAD>
<BODY>
...
<? ?>
Same goes for footer and menu files.
Then you can use it by including them.
Your index.php could look like following.
<?php
include("header.php");
include("menu.php");
?>
<h1> This is my content </h1>
<?php
include("footer.php");
?>
This is the easiest option I think for someone who doesn't want to spend using templates, CMS etc. Also you can create function called header that takes $title and changes title of your window. Up to you.
Sounds like you want AJAX. Use prototype. You can make one page, and then use prototype to swap out the content (which could include a PHP page) based on user clicks.
Simple and easy solution:
create footer.php and header.php
and in the header.php you can have something like this:
<?php function top_header($title) { ?>
<html>
<head>
<title> <?php echo $title ?> </title>
</head>
<body>
<?php } ?>
footer.php
<?php function footer() { ?>
</body>
</html>
<?php } ?>
Your index.php could look this:
<?php
include("header.php");
include("footer.php");
top_header("Title of the page");
?>
Hello World!
<?php footer(); ?>