How can I update multiple pages when editing one page? For example, I am editing index.html, and I am changing its footer (which all other pages have). When I change the footer I want the other pages on my domain to be updated as well. Thanks!
This is a major reason why platforms like PHP and ASP.NET exist; they can help you dynamically generate HTML containing common sections like headers and footers that are defined in one place. One simple alternative that may be available for you is Server Side Includes.
Update:
Since you said you're using PHP, rather than having pages with the .html extension, you should write your pages in PHP. That way, you can include separate files containing your headers and footers in each page, so any changes to those only have to be done in one place, i.e.:
<?php include 'header.php'; ?>
<!-- Your page-specific content -->
<?php include 'footer.php'; ?>
There are a few ways to do this. You can use an editor that opens files of a certain project, and use regular expressions to edit.
Or a more common way to achieve this is to use SSI's or Server Side Includes (http://en.wikipedia.org/wiki/Server_Side_Includes). This is more common, and less likely to destroy pages if a regular expression isn't formed properly.
If you're using a different engine (like PHP) you can do something like file includes (require_once, include, etc), to pull the information from a single place. This is similar to SSI's in concept, except it requires another engine to parse the page.
Modern day IDEs for Web Design (DreamWeaver) include this type of functionality within them too. You only need to set it up.
For such automatic updating you can create a function in PHP
function generate_footer() {
echo "<p>Page maintained by Lucifer</p>";
}
and in the HTML code you just have to write this
<div class= "footer">
<?php generate_footer()?>
</div>
this is in php you can use any other server scripting language you wish
Related
One of the nice things about ASP.Net is the ability to set a site.master file that holds all the repeated HTML/code from a site, and still be able to alter things on it from an individual website page. (For example, changing the <title> tag for every page, or adding other things to the <head> of your document.)
In the past in PHP I've used Server Side Includes to remove duplicate HTML/code (such as the top of the document, main navigation, footer, etc.) but obviously you can't alter any of the contents from the page.
Is there any way to implement site.master type functionality in PHP? If not, what's a good way to remove repeated HTML/code while still being able to change things like the page's title, highlighted navigation, etc.
Thanks.
This is just called Template Engine and there is tons of engine for php.
Smarty is one of them
The simplest way of pulling in common fragments is to use require() which will allow you to include other files within the current file.
An example of how you might use it:
header.php - a reusable fragment
<div class="header">
<h1><?= $pageTitle ?></h1>
</div>
pageContents.php
<body>
<?php
$pageTitle = 'Check it out, the Foobar page!';
require('header.php');
?>
blah blah
</body>
What you are looking for is a templating engine built for PHP. ASP is a framework which has templating built in which provides you the functionality that you are referring to (i..e templates, membership provider, etc.)
In order to get the same in PHP you will need to use either a framework or a templating engine.
Some samples:
http://www.webresourcesdepot.com/19-promising-php-template-engines/
http://www.phpframeworks.com/
there is no such functionality in PHP, you can move your components into separate files and include them in your main file using include or require functions.
Some frameworks do provide (simulate) such functionality though. e.g. In cakePHP you can have a layout file where you can output your pages.
Is it better to have HTML files for including as header and footer in a PHP application instead of PHP files?
I usually do something like the following to keep things neat and consistent:
<?php require_once('includes/header.inc.php'); ?>
<!-- Html here -->
<?php require_once('includes/footer.inc.php'); ?>
If your webapp is written in php I see no reason to deviate away from the php extension as you may want to add some dynamic content into the include at a later date.
including as an header and footer in
php application is good
like
index.php
include_once('header.php');
// your code regarding this page
include_once('footer.php');
It depends - if you have static header and footer html is enough. If you need there any kind of dynamic, then php comes to fight.
when you are building just some very simple php web page i suggest using require_once to include basic layout of the page. It doesnt matter if you are including html or other php code and you should know whether you would benefit from php or not.
Also including html is faster then including php code which is just calling many echos or prints.
Provided you use secure PHP code, it does not make a (significant) difference. I would suggest separating the main application concerns from your header and footer files, to avoid placing costly operations which may be unecessarily repeated in these files. Some guidelines to address possible security issues can be found here: http://phpsec.org/projects/guide/.
If you are using kinds of include for inclusion, the file type doesn't matter, the PHP code in it will execute, if there is one. Note that this is also a subtle security concern.
Because of this, I don't think the file type is relevant in this. The real question is what have you got in the footer and the header, e.g. PHP processing, database calls, ...?
I personally think that is better to require_once your php header and php footer. Maybe it is a bit slower thant hmtml (but not very relevant) but this unnecessary "optimization" doesn't worth the certainly needed php functions used in your header like session_start() or mysql_close() in your footer.
My personal point of view though :)
There is nothing better in including HTML
moreover - a real life will tell you that you can't use plain HTML anyway.
I've answered similar question recently, with strong reasoning: Using PHP include to separate site content
This is what I do
index.php
<?php
include_once('header.php');
//Your code goes here
include_once('footer.php');
?>
and yes it is a better practice to use php files as header and footer.
I have a medium size legacy php application with almost no javascript integration. I've recently been learning javascript (yes actually learning it) using Douglas Crockfords excellent book and taken YUI as my library of choice. I've tried out a few things and got a few things working but now I want to integrate the various components etc into my application properly, my question is how to go about doing this for ease of maintenance and code reuse.
Currently the application consists of
php pages
smarty templates (html templates with some special template markup) for each section of a page so multiple templates may be used for a single page.
single css file.
I have the following ideas about how to integrate my javascript into the pages.
Write inline javascript in each smarty template with the code required for that section.
Write a seperate .js file to go with each smarty template that is linked in and then a single inline script to run it.
a seperate .js file for each php page which would have all the functionality required for the entire .php page. A small inline function would call whatever functions were required.
Something I havent though of?
Does this make any sense? Does anyone have a good suggestion for this?
Update:
One extra bit of info is that its an internal application, so its not so important to restrict everything to a single file.
Two other options:
A single JS file that contains all the JS for your entire site. Depending on your caching settings, you should be able to get the user to download just one file for the entire site and use a cached version for every other page.
Divide your JS up according to function, rather than page, and include whatever functionality each page requires. E.g. one page may require tabs, form validation and animation, while another may only require tabs.
Or you can have a hybrid of these: one JS file that contains the vast majority of your code and an extra file if needed for particular pages or templates.
None of the approaches mentioned are wrong (though I'd skip the inline JS one as much as possible); which one is best will depend on your precise situation and your personal preferences.
Firstly, most setups allow a master layout template in which you can place a common page opening or, alternatively, each template includes some global header.
That being said, you should do a combination of 1, 2 and 3:
Have a .js that is included in all templates that contains global functionality. Each template may also optionally have it's own .js specific to that page or section of pages. Finally, if there's tiny amounts of code specific to a page (or must be dynamically generated each time), it won't make sense to initiate another http connection for it so have that source be right in the template.
if you don't have a ton of javascript then create an external js file and include it in the header of the webpages and be done with it.
i would like to know your point of view on where to position the PHP code on .php page and why?
a) top of the document
b) just above the html elements where i am going to use it.
thank you.
c) In a different file and use a template engine such as smarty
http://www.smarty.net/
Your life will be beautiful and awesome after smarty.
EDIT : I won't downvote other solutions , but it's a very ugly anti-pattern to mix html code with php, you have good, stable and easy solutions to avoid that, use it now or your website will be a big mess of spaghetti code.
Depends on the purpose.
Database query related posts that determine the contents within the part, I call it before there is any input. Also any type of PHP commands that contain raw header information should be presented before any output is made.
Any content related stuff can be positioned anywhere on the page. PHP code is really everywhere - where ever, and however you want to create the HTML from your PHP dynamically.
My pages usually take this structure:
<?
include 'start.php';
$pagetitle = 'the services we offer (branding, web, print etc.)';
$metatitle = 'Our Creative Services (branding/logo, web, print)';
$scriptinclude = 'whatwedo.js';
include 'header.php'; // contains the <body><head></head><body> and a few more elements to start the header/menubar etc.
?>
<div class="full_grid" id="index_slide">
// content here, mixed with PHP if you like...
</div>
<?
include 'footer.php'; // contains the footer HTML, as well as </body></html> etc. to wrap things up.
?>
I put as much code as I can at the top. And only use php withi HTML where I need loops or output data.
This gives me a better overview of the code and it's easier to work with.
Keep your code and HTML as separate as possible. Have them in entirely separate files where you can.
Your HTML should be as much pure HTML as possible, and your PHP code should contain as little HTML as possible.
Obviously, you're producing a web page, so there will have to be some mixing, but keep it as limited as possible: The only code you should mix in with your HTML should be the one-liners to place specific bits of PHP-generated code into your HTML template.
It completely depends what you're doing with it. Personal preference for me is to create any functions I need at the top and then scatter inline php throughout the document calling the functions at the top of the page.
If something needs calculating and it can be done at the top, it's much easier to read and debug if you keep it all in one place. And keeping this the same throughout all your files will help too. What you could do is just include a config file at the top of the page with any site-wide functions you need too, so you don't have to copy and paste through all your files.
If you are only using one PHP file then definitely put all PHP code at the top, then the HTML below with variables where necessary.
For example, $title = 'Page title'; at the top of the page, then <h1><?=$title?></h1> in the HTML portion of the page.
However a better solution is to have two (or more) files. The main one contains all the PHP logic to grab/process data, while the second one is a "view" file containing mostly HTML. The simply include your view file from the main PHP file.
I have a website which consists of a bunch of static HTML pages. Obviously there's a lot of duplication among these (header, menu, etc). The hosting company I plan to use supports PHP, which I know nothing about. Presumably PHP provides some sort of #include mechanism, but what changes to I need to make to my HTML pages to use it?
For example, suppose I have a page like this
index.html
<html>
<head></head>
<body>
<h1>My Common Header</h1>
</body>
</html>
Obviously I need to move the common part into it's own file:
header.html
<h1>My Common Header</h1>
Given the example above (and assuming all files are in the same directory):
What do I add within the body tag to get header.html included?
Do I need to rename index.html or add some special tags to indicate that it's a .php file?
Do I need to make any changes to header.html?
Update: I want to emphasise that my objective here is simply to find the lowest-friction means of reducing duplication among static HTML files. I'm a bit reluctant to go down the server side includes route because I don't yet know what type of server (IIS/Apache) I'll be hosting the files on, and whether includes will be turned on or off. I was drawn towards PHP only because it is about the only thing I can presume will be available that will be able to do the job. Thanks for the responses.
Thanks,
Donal
You are looking for include (or one of its derivative such as include_once, require, require_once):
header.php
<h1>My Common Header</h1>
index.php
<html>
<head></head>
<body>
<?php include('header.php'); ?>
</body>
</html>
And so on, for your footer for example.
You don't need to use PHP to get this functionality, and it's generally a bad idea to do so due to potential security concerns. Essentially, you're swatting a gnat with a nuclear bomb. If you're not using a dynamic language, then you're looking for server side includes.
In IIS, for instance:
<!--#include virtual="file.inc"-->
Be aware that you often have to configure the server to utilize them, as this feature is often turned off by default. Both IIS and Apache support server side includes, but they use different configurations.
You can find more information here:
Server Side Includes
EDIT: I don't mean that it's a bad idea to use PHP, just using PHP solely for including other files. It creates a larger attack surface by bringing PHP into the mix when it's not needed, thus the potential for security issues when the functionality of PHP is not required.
EDIT2: I think it's a bad idea to assume you won't be a target because of your size, and thus you can ignore security. Most sites are compromised by automated worms and turned into malware hosts, spam zombies, or pirated software/media servers. Apart from the fact that you might end up being involved with infecting others, your site can become blacklisted and it can cost you real money in bandwidth overage charges. We're talking hundreds or thousands of dollars.
Just because you're a small site doesn't make you any less of a target. Just being on the internet makes you a target.
Forget doing it on the server altogether.
If all you really want to do is maintain some static pages -- and don't anticipate ever having to really use PHP -- I'd just do it with Dreamweaver, which will allow you create and manage templates and variable content on your end.
No includes needed. No templating engine needed. (These would be overkill for what you are trying to accomplish.)
You should first change the file extensions of index and header to be .php, then you can do:
<html>
<head></head>
<body>
<? include 'header.php'; ?>
</body>
</html>
And your header.php file just has
<h1>My Common Header</h1>
While you can just use the "include", "require", or "require_once" directives to include things in one page, you might have better luck with a template engine like Smarty
While using an include file for the header is a solution I went a different route when I faced the problem several years back: I wanted all pages to use the same layout (which I assume is rather common ;-). Thus, as I only wanted to change the content of the page I made the page content the file that gets included and have a master template file that includes header and footer. For setting the page to be included I resorted to creating quite small php scripts that only set a variable that holds the page to get included. In some cases the page can also get named by a GET parameter. Of course this requires proper validation of that parameter. In the long run I don't need to worry about the HTML itself anymore -- all I do is write small snippets (which should be complete for themselves of course) that get included.
A possibly even better solution would be to use an existing template framework. Due to the contraints I had back then I wasn't able to do so, but I would do it when facing the same issue again.
Back in the day, I used SSIs (the "<!--#include virtual="file.inc"-->" method described above by Mystere Man) quite a bit for static HTML pages and I would definitely recommend using that.
However if you want to eliminate any uncertainty about whether support for that will be enabled on the server, you could develop your separate files locally and merge them into the resulting files before uploading to your server. Dreamweaver, for example, supports doing this in a seamless fashion.
Or you could do it yourself with a rather simple script in your language of choice by doing simple string replacement on markers in the files, replacing {{{include-header}}} with the contents of a "header.html" file and so on.
Edit
Oops! Somehow I didn't see Clayton's post with the same note about Dreamweaver.
OK this is a semi-programming related question only.
PHP does have include(), which is really easy to use, but it doesn't contribute to future maintainability. I wouldn't recommend it, especially for big sites.
I'm a pro-frameworks. I've used CodeIgniter, CakePHP and even Smarty template engine. If you are serious about PHP, do consider CakePHP. There's this "layouts" concept where you frame your header, footer, css, javascript outside of the main content; e.g. for the "about us" page, your content would be something like:
This is an about us page that tells you a whole bunch of stuff about us...
CakePHP takes this this content, and wraps your layout around it:
header
css
javascript
This is an about us page that tells you a whole bunch of stuff about us...
footer