Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Best way to create template/reusable design?

    So far for re-using my design to easily create new pages, I have been simply creating a CSS file that creates the <div> sections for each of my pages, and then using .php files to use in the content area.

    I would like to do this a quicker way where the navigation always is the same, along with all my ads, so the only thing I have to change is the content area.


    Can anyone recommend a method they use for re-using pages or a good tutorial ?

    Thanks.
    I am on my way. :)

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    There are many techniques you can use for the purpose of reusing your page design.

    One of which is to create a template page with place holders of where you would like to display data. For example

    <html>
    <head>
    <title>
    [page_title]
    </title>
    </head>
    <body>
    <div id="header">
    [header_contents]
    </div>
    .....
    .....
    .....
    </body>
    </html>

    In the above template, you can then read the file, and replace [header_contents] etc with the appropriate data...using str_replace

    $template = str_replace( "[header_contens]", "<h1>My Website</h1>", $template );

    However, using the above method can slow down things...depending on number of connections to your website etc.

    An easier method would be to do create a template which you include with every page request and use php variables as place holders...for example

    <html>
    <head>
    <title><?php echo $page_title;?></title>
    </head>
    <body>
    <div id="header">
    <?php echo $page_header; ?>
    </div>
    <div.................
    .....................
    </body>
    </html>

    there might be other ways to do this of course...but I have been using the second method...but I always assign default values to each place holder by default...in case there are empty if data is stored in db or a file..and fails to connect for example.

    Cheers,
    Ess
    Last edited by ess; 06-02-2007 at 08:34 PM.

  • #3
    New Coder
    Join Date
    Apr 2007
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you for your input.


    The way I have been doing things is I made a home page called index.php that is seperated by <div> into four sections, the header, navigation, content, and then the footer.


    Then on the index.php page I simply used the include("header.php") where header.php contains all the html, images, etc, that I want to use for my header. I do the same with the navigation, footer, etc.

    What I am confused about what I should do, as this method is very easy to create the same layout, is should I use a content.php page and include that, or should I only use the include file for the header, navigation, and footer?

    I will have a dynamic site.


    I am thinking the best way to keep everything consistent would be to use the header.php, sidebar.php, and navigation thrroughout my site to keep things consistent.

    Then I will have a <div> for the content, where I will include different php files based on whether it is a form, search, etc.


    Any advice on how I should do the content part of the site? Should I use a main content <div> section and then simply include different .php files based on the page?

    Thank you for any advice.

    inVINCEable
    I am on my way. :)

  • #4
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    I suppose you can use different files for specific things...i.e. header.php

    Of course, it all depends on preferences, programmers style etc.

    Personally, I tend to divide pages into different categories. For example, dynamic pages, and static pages. Dynamic pages are pages that pull contents from a database, where static pages are those that don't change..or depend on an external resource.

    Here is how I usually write the header and footer into a page.
    I create a file and usually called all_pages.php and dump the following functions

    PHP Code:
    <?php
    function getHeader$title "defualt title such as name of site" ) {
    $header =
    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
    <meta http-equiv="content-script-type" content="text/javascript" />
    <meta http-equiv="content-style-type" content="text/css" />
    <meta http-equiv="content-language" content="en-GB" />
    <link href="main.css" rel="stylesheet" type="text/css" />
    <title>'
    .$title.'</title>

    </head>
    <body>
    <div id="divContents">
    '
    ;
    return 
    $header;
    //-- ends getHeader

    function getFooter () {
    $footer =
    '
    </div>
    <div id="divFooter">
    Copyright &copy; '
    date('Y') . ' All Rights Reserved.    
    </div>
    </body>
    </html>
    '
    ;
    return 
    $footer;
    //-- ends function getFooter

    ?>
    if you look at the getHeader() function you will notice that it opens the html page and allows you to change the page title when needed.

    Also notice that it opens <div id="divContents"> which is a div that contains all the page contents. the div is then closed when I call getFooter()

    Here is how I usually use it

    PHP Code:
    <?php
    include "all_pages.php";
    echo 
    getHeader"Home Page" );
    ?>
    <h1>This is the home page</h1>
    <p>
    I could write all the html here...or add php when needed
    </p>
    <?php
    echo getFooter();
    ?>
    As for the dynamic pages...I usually make use of class that map to tables in the database. That way, I can focus on concerns of security when maping classes to the database, and focus on layout and design when displaying data.

    Cheers,
    Ess

    P.S. I would definitely be interested in hearing your decision as to how you are going to organize your project.

  • #5
    New Coder
    Join Date
    Oct 2005
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post
    I use a similar system for my site. Here's my "template" source code:

    Code:
    <?php include("header.txt"); ?>
    
    <DIV id="main">
    </DIV> <!-- Main Content Area //-->
    
    <?php include("footer.txt"); ?>
    header.txt contains the opening <HTML> tag, the universal title of my pages, CSS external link, and META information inside the <HEAD> tags, and the opening <BODY> tag. It also includes the opening <DIV> tags of my layout as well as the header and menu of my site.

    footer.txt contains my copyright message, the ending <DIV> tags, a Google Analytics script, and the ending <BODY> and <HTML> tags.

    I just copy this template whenever I create a new page and create my content in between the <DIV id="main> tags.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •