Forum Moderators: coopster

Message Too Old, No Replies

PHP template.

converting HTML template to PHP

         

pmheart6

4:41 pm on Dec 3, 2007 (gmt 0)

10+ Year Member



I am totally ignorant when it comes to PHP. Well I had started some stuff years ago, but never got to involved. Mostly just debugging a friends thing.

I have a html template that I want to convert to PHP so I can use a single template rather than 1 template for each body file.

here is the sample PHP file I found:

<?php

$header = "header.html";
$footer = "footer.html";

$file = $_SERVER["PATH_TRANSLATED"];
readfile($header);
readfile($file);
readfile($footer);

?>

Here is my current html file [notice I have 4 includes and use CSS to layout the includes in DIV sections]
My current page can be found at <snip> However as I make this new template I will be changing the layout:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<title>Amazing Hands Therapeutic Massage (Massage Therapy New Orleans)</title>
<link rel="stylesheet" type="text/css" href="amazinghandsdefaultheader1.css">

<style type="text/css">
table.c19 {width: 100%;}
td.c18 {height:48px; text-align: right; vertical-align: top;}
img.c17 {width: 44px; height:46px;}
td.c16 {height: 48px; text-align: left; vertical-align: top;}
img.c15 {width: 34px; height: 46px;}
td.c14 {height:48px; vertical-align: middle; text-align: right;}
td.c13 {height: 48px; text-align: center; vertical-align: middle;}
td.c12 {width:100px;height: 48px; text-align: center; vertical-align: middle;}
img.c11 {width: 540px; height: 360px;}
td.c10 {text-align: left; vertical-align: top;}
table.c9 {text-align: left; width: 100%;}
span.c8 {font-style: italic;}
div.c7 {text-align: center}
table.c6 {text-align:left; background-color:#FFCC66; width:100%;}
td.c5 {vertical-align: top;}
p.c4 {font-size: 10px; text-align:justify}
h1.c3 {margin-top: 0PX;margin-bottom: 0PX;}
td.c2 {vertical-align: top; width: 100px;}
img.c1 {width: 96px; height: 98px;}
</style>
</head>

<body>
<div id="bodyarea">Best viewed with Firefox or IE 7
<div id="headerarea"><!--#include virtual="header.html"--></div>
<div class="c7" id="leftcol"><!--#include virtual="menu.html"--></div>
<div id="contents"><!--#include virtual="body/main.html"--></div>
<div id="footer"><!--#include virtual="footer.html"--></div>

</div>
</body>
</html>

[edited by: eelixduppy at 4:49 pm (utc) on Dec. 3, 2007]
[edit reason] no URLs, please [/edit]

HarryM

6:51 pm on Dec 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did the same thing some years ago. What I did was make the whole of the html template into an include. Then each page just becomes a list of variables, and at the bottom calls the template as an include. Within the template use the print command to display the content of the variables as required. Once you have a template up and running on a test page then you can refine it.

For example using your html:

PAGE (somename.php)

<?php
$title = "Amazing Hands Therapeutic Massage (Massage Therapy New Orleans)";
$content = "whatever";
// other variables as required
require("/includes/template.inc");
?>

TEMPLATE (template.inc)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<title><?php print $title;?></title>
<link rel="stylesheet" type="text/css" href="amazinghandsdefaultheader1.css">

<style type="text/css">
<!-- if needed -->
</style>
</head>

<body>
<div id="bodyarea">Best viewed with Firefox or IE 7
<div id="headerarea"><!--#include virtual="header.html"--></div>
<div class="c7" id="leftcol"><!--#include virtual="menu.html"--></div>
<div id="contents"><?php print $contents?></div>
<div id="footer"><!--#include virtual="footer.html"--></div>

</div>
</body>
</html>

You don't actually need to really know PHP in any depth. Just pick up the commands you need as you go along.

pmheart6

2:55 am on Dec 4, 2007 (gmt 0)

10+ Year Member



So I would still have a *.php for each page, and a html/contents file?

I am kinda thinking it should be in the htaccess file. As a wrapper....

i.e.
http:/ / webpage. blah/indexprices.html#specials

where the actual indexprices.html file would just contain the html for the page and the header, menu, and footer would be wrapped via php called from htaccess.

Actually I may have just answered my own question below
$content = "whatever";
would be
$content = $_SERVER["PATH_TRANSLATED"];

and in htaccess use:

AddHandler headered .html

Action headered /header/somename.php

-- would there be a way to call the title from this "actual html document"? Not that it matters...

You see it on the free web servers all the time i.e. tripod, geocities or whatever they all are

HarryM

10:42 am on Dec 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know nothing about using htaccess for this. What I have described is a flat-file system that works OK for me. You could also use a database to store the content and other information. The template is just there to generate the page.

You say that the title doesn't matter, and I note that all the pages on your website have the same title. The title does matter, and should be unique for each page. You should also have a unique meta description tag for each page. If you don't you are unlikely to get indexed properly in search engines.

Also you don't need to have the CSS styles in your html. You can put them in the stylesheet that you link to.

Another point is that your site can be accessed by "domain.us/" but your internal links for your home page direct to "domain.us/index.html". This means that Search Engines will probably index your home page twice as "domain.us/" and "domain.us/index.html". You need to decide on which one to use. If it is to be "domain.us/" (which is fairly normal) then your internal home page links should be "/", and you should set up a mod rewrite in htaccess so that "domain.us/index.html" is converted to "domain.us/".

pmheart6

5:12 pm on Dec 4, 2007 (gmt 0)

10+ Year Member



I thought I had it work (once) but I must have been mistaken. I can not get it to parse the includes when called via php. I can get the php to be called when requesting an html document and that does not help.

I tried changing the template.inc to template.html it works the same way.

calling template.html by itself loads the includes.... hmmm

Wait if I can call my body with a php file command, why not the rest? <?php print readfile($file)?>

Don't you love it how I progress as I am giving up and writing a response.
--Pausing to test--
ok that is getting close, I just need to purge & clean the css to see if it will work. and display correctly.
--------
If this works, I'll post the htaccess, *.php and *.inc file for reference. Then anyone can use css and load any *.html onto a page wrapping it in.

pmheart6

10:18 am on Dec 5, 2007 (gmt 0)

10+ Year Member



I am getting a number after every file. It displays the contents correctly, but adds a number right after it.

Here is the lines in the .inc (html template):

<?php print readfile("menu.html")?>
<?php print readfile("header.html")?>
<?php print readfile($file)?>

HarryM

11:37 am on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try adding a semi-colon.

<?php print readfile("menu.html");?>
<?php print readfile("header.html");?>
<?php print readfile($file);?>

Also I would have thought that the last command could be just

<?php print $file;?>

HarryM

11:51 am on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is also an addressing issue when trying to call an include within an include. The address of the include has to be relative to the page, not the include which is making the request.

There are several ways of doing this, but I set a variable on each page which is dependant on the directory level of the page.

$prefix = "../"; // or whatever

on the home page it is:

$prefix = "";

Then I code the commands in the template as:

<?php require("{$prefix}includes/whatever.inc");?>

I also use it within the template html for images:

<img src="<?php print $prefix;?>images/someimage.jpg" alt="">

pmheart6

2:37 pm on Dec 5, 2007 (gmt 0)

10+ Year Member



Ok this seems to be pretty perfect. Clean page, clean folders, clean code. I think I have stripped all the url references...

I created a php wrapper using htaccess so all
pages load with the same header/footer/menu. Makes it
real easy to update now. Its like CSS did for layout
and coloring. One site calls it a tortilla wrap.

Only I replaced the readfile with an include. It gets
rid of the checksum number after the file. And SSI
includes won't work in the PHP or template.inc (not
sure about main body), but we are using PHP so I used
it for all the parts. Header/Menu/Footer/Body -- You don't
need full html just body text in each file. Head
information is only needed in the template since its
wrapped around the other pages.

I got rid of the title in the header and put it in the
actual html body file. The browser still seems to read
it. Hopefully the search engines do, otherwise, I'll
have to figure out a php solution, if there is one.

Another quark is that there are no error pages. You
would have to add that to the php if you want, but
with the menu on each page, they can easily find
there way to a working page.

Here is the code (your menu.html, header.html, footer.html go in template folder too):

.htaccess
AddHandler headered .html

Action headered /neworleans2/template/somename.php
##Note full path from top of web I WOULD LIKE TO USE
RELATIVE if someone gets it to work

template/somefile.php
<?php
$file = $_SERVER["PATH_TRANSLATED"];
// other variables as required
require("template.inc");
?>

template/template.inc
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- <title>CSS Page Template - Two Column, Left
Static</title>-->
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<meta name="Robots" content="NOINDEX" />
<meta http-equiv="PRAGMA" content="NO-CACHE"
/>
<link rel="stylesheet" type="text/css"
href="template/***layout.css" />
<link rel="stylesheet" type="text/css"

href="template/***presentation.css" />
<link rel="stylesheet" type="text/css"
href="template/other.css">

<style type="text/css">
<!-- if needed -->
</style>

</head>
<body>
<!-- left column -->
<div id="lh-col"><br />
<?php include("menu.html")?>
</div>
<!-- end of left column -->
<!-- right column -->
<div id="rh-col">
<?php include("header.html")?>

<?php include($file)?>
<!-- These don't work in the wrapper use the php include instead #include
virtual="template/footer.html"-->
<?php include("footer.html")?>
</div>
<!-- end of right column -->

</body>
</html>

pmheart6

2:44 pm on Dec 5, 2007 (gmt 0)

10+ Year Member



Harry, the $file is calling the original html document, the somename.php file fills the variable with the path:

$file = $_SERVER["PATH_TRANSLATED"];

and the template.inc calls the file.

-------------Harry Said:
<?php print readfile($file);?>

Also I would have thought that the last command could be just

<?php print $file;?>