Design on the server's side.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
figaro11
Forum Commoner
Posts: 64
Joined: Mon Sep 17, 2007 11:49 pm

Design on the server's side.

Post by figaro11 »

Hi, I don't know if anyone's going to be able to understand this, but I'll still try.

How do I design web pages without having to write the same code or layout over again? It seems like on every website there is usually a header and a footer that are always the same; consistent throughout the website. How do they accomplish this without having to copy and paste the same header and footer on every page?

I'm assuming it's a php, or other server-side language, solution. But please explain the methodology most server-side developers use construct their pages, if you will. Go wild with detail if you'd like.

Thanks.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to PHP-Code.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Templates.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You might want to start by learning about the PHP include() command. That will give you a start at combining common parts of pages with the page content.
(#10850)
User avatar
figaro11
Forum Commoner
Posts: 64
Joined: Mon Sep 17, 2007 11:49 pm

Post by figaro11 »

I'm aware of the include function and am using it for my projects.

Feyd's comment seems interesting. But my concept of the word templates often makes me think of a css template. If your meaning something else could you elaborate more, maybe?

My current methodology is using the include function to piece together all the chucks of code into one page.

But this posses some problems. Let us say that I have a header.php file which holds all the external scripts and css tags in the head tag. Also, header.php would hold a navigation menu, logo, etc. Then the index.php file would include header.php at the beginning and then add some homepage content then include a footer.php page to the footer.

This seems good. But the problems I'm having with this is that I have multiple body tags. This problem can easily be fixed by not ending the body tag in the header.php file and not beginning the body tag in the footer.php file. My question on this problem: is there a better way?

My other problem is that I don't have control over the title tag for each page that includes the header.php file. Because it's in the header.php file that has the head tag which has the title tag. If I where to include both a head tag and a title tag in the index.php file then this might cause some problems if I'm in standards mode and it might not comply with web standards.

There. Hopefully I was able to express my problem; I have a hard time doing that.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Templates, as in Template Lite, Smarty, et al.
User avatar
figaro11
Forum Commoner
Posts: 64
Joined: Mon Sep 17, 2007 11:49 pm

Post by figaro11 »

feyd wrote:Templates, as in Template Lite, Smarty, et al.
I took my time looking into it. This is very useful. Thank you feyd. :D

Although, I might use this in later projects, currently I'm just using the include function. But the biggest problem I'm having with include() is the disability to change the title of my pages. I include header.php, which writes the DTD, includes scripts and stylesheets, and also a <title> tag. I do believe that the title element can only be written once. So I can't change the title from what header.php states it to be.

So my questions are: If I include a header.php, which has all the head part of the document, then how do I change the title, for different pages? And can I write multiple html, head and body tags in my markup with an XHTML Transitional document type; would it be valid standards?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

The idea is that you compile your template into a single template at runtime (header, content, footers).. then make the appropriate string replacements.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Set a variable in your main file, then reference it in your include file:

Code: Select all

<?PHP
$title = "This is my page.  Bask in its glory";
include 'header.php';
?>

Code: Select all

<?PHP
echo <<<PAGE
<html>
  <head>
    <title>
      $title
    </title>
  </head>
  <body>
  ...
PAGE;
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
figaro11
Forum Commoner
Posts: 64
Joined: Mon Sep 17, 2007 11:49 pm

Post by figaro11 »

pickle wrote:Set a variable in your main file, then reference it in your include file...
Hey! Thanks.............. Why didn't I think of that? :banghead:

This creates a Big Bang in my head! Now all I need to do is create a function that can do this for me. :D
User avatar
figaro11
Forum Commoner
Posts: 64
Joined: Mon Sep 17, 2007 11:49 pm

Post by figaro11 »

Btw, who all here uses templates, raise their hand?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Image
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
figaro11
Forum Commoner
Posts: 64
Joined: Mon Sep 17, 2007 11:49 pm

Post by figaro11 »

pickle wrote:Image
I prolly have to be a bit more specific. Who here uses Template Engines?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Image
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
figaro11
Forum Commoner
Posts: 64
Joined: Mon Sep 17, 2007 11:49 pm

Post by figaro11 »

pickle wrote:Image
Oh yeah, Which one? :)
Post Reply