Results 1 to 6 of 6

Thread: Templates

  1. #1

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Templates

    I want to be able to create every page on my site with the same style using a template. I have a design for my Template but I'm not sure how I'm gonna get this to work. My first Idea was to include the file passing a Variable (say named $Content) and this variable contians all the content for the page, like this:
    PHP Code:
    <?
     //Do Stuff...
     
     $Content = "This is the Content of the Page.";
     
     include "Template.php";
    ?>
    However this way the $Content variable could get very big on some of the larger pages.

    I then thought about spliting the template into two parts, one for the stuff that'll be before the content and one after. Like this:
    PHP Code:
    <?
     include "TemplateStart.php";
     
     //Do Stuff...
     
     echo "This is the Content of the Page.";
     
     include "TemplateEnd";
    ?>
    But Surley theres a better way to do this, does anyone have any ideas?
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    well, there's lots of ways to expand upon your ideas.. a friend of mine uses a main page that includes all of his database configuration files and other files to clean user's input, and then builds up the page within the index.. and then has a page that will have his template on it and will put the content in.. it's usually something like this:
    PHP Code:
    index.php:
    <?
      require("inc/clean.inc.php");
      require("inc/config.inc.php");

      $output = "";

      if($page == "blah"){
        $output .= "<b>Blah!</b><br><br>\n";
        $output .= "<font color='red'>Blah!</font>\n";
      }elseif($page == "otherpage"){
        //other pages
      }
      require("output.php");
    ?>

    //////////////////////////////////////////////////////////////////
    output.php:
    <html>
      <head><title>the page!</title></head>
      <body>
      <!-- some header -->
      <!-- some menu/table things -->
      <!-- insert the content -->
      <?=$output;?>
      <!-- some footer -->
      </body>
    </html>
    So, basically, it's like your idea of using the $content and including a template file. His page is set up around the output.php page, but the content is generated within the index file. I thought the filesize would get huge too if you tried to store a whole website of information.. and I'd never personally do it, but it could be used for smaller projects. On bigger projects, you could do basically the same thing, but have different files and still base them from the output.php, which might've been what you were getting out.. like, have a index.php that will show site news or something, and then a seperate page that was the same as the index but just changed the $output variable..

    I don't do it either of those ways just because I found my own way of doing it, and it works fine.. so, I figure why change?

    This is basically what I do, it's based around an index file and then includes files as it sees fit..
    PHP Code:
    <?
      require("inc/config.php");
      
      $aim = (isset($_GET['aim']) && $_GET['aim'] != "") ? $_GET['aim'] : "";
      $ref = (getenv("HTTP_REFERER") != "" && $aim == "") ? "REFERER: " . getenv("HTTP_REFERER") : "";
      $ref = ($aim != "") ? "REFERER: AIM: " . $aim : "REFERER: none";
      
      $action = (isset($_GET['act']) && $_GET['act'] != "") ? strtolower($_GET['act']) : "main";
      switch($action){
        case "main":
          $pg['include'] = "main";
          break;
        case "register":
          $pg['include'] = "register";
          break;
        default:
          $pg['include'] = "error";
      }
      $pg['include'] = strtolower($pg['include']);
      if(!file_exists("inc/" . $pg['include'] . ".php") && file_exists("inc/" . $pg['include'] . "inc.php")){
        $pg['include'] = $pg['include'] . ".inc";
      }
      if(!file_exists("inc/" . $pg['include'] . ".php")){
        $pg['include'] = "error";
      }
    ?>
    <html>
      <head>
        <title><?=$glob['title'];?></title>
      </head>
      <link href="css/main.css" rel="stylesheet" type="text/css">
      <body topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" onSelectStart="return false;">
        <center>
          <table width="700" cellspacing="0" cellpadding="0">
            <tr>
              <td></td>
              <td height="13" colspan="2" align="right" class="head-date">
                <?=date("F jS, Y") . "\n";?>
              </td>
            </tr>
            <tr>
              <td></td>
              <td height="100" colspan="2" valign="bottom" class="head-banner">
                <img src="img/logo.gif">
              </td>
            </tr>
            <tr>
    <? if(!$user['login']){ ?>
              <form action="login.php" method="post">
    <? } ?>
              <td width="100" valign="top">
                <div class="mnu-top"></div>
                <div class="menu">
                  <a class="menu" href="/index.php">Home</a>
                  <a class="menu" href="/index.php?act=register">Register</a>
                  <span class="mnu-space"></span>
    <? if(!$user['login']){ ?>
                  <div style="padding-bottom: 7px; padding-left: 4px;">
                    username:
                    <input type="text" name="user" maxlength="20" class="login-input">
                    password:
                    <input type="password" name="pass" maxlength="20" class="login-input">
                    <input type="submit" value="login" class="login-submit">
                  </div>
    <? }else{ ?>
                  <div style="padding-bottom: 7px; padding-left: 4px;">
                    Logged in as:<br>
                    <?=$user['name'];?><br>
                  </div>
                  <a class="menu" href="/index.php?act=post">New Entry</a>
                  <a class="menu" href="/users.php/<?=$user['name'];?>">My Entries</a>
                  <a class="menu" href="/index.php?act=acct">My Account</a>
                  <a class="menu" href="/login.php?act=logout">Logout</a>
    <? } ?>
                </div>
                <div class="mnu-bot"></div>
                <div style="height: 15;"></div>
              </td>
    <? if(!$user['login']){ ?>
              </form>
    <? } ?>
    <? require("inc/" . $pg['include'] . ".php"); ?>
            </tr>
            <tr>
              <td width="100" style="font-size: 1px;">
                &nbsp;
              </td>
              <td height="14" colspan="2" align="right" class="foot">
                <div class="foot">&nbsp;</div>
              </td>
            </tr>
          </table>
        </center>
      </body>
    </html>
    I didn't exclude any of the HTML, and you can see a working example of it at http://npss.gamersepitome.net.

    You can also do what you were saying with the template headers and footers.. You would usually include any configuration files within each page or even just in the header and then put in the content and a footer.. just like this, which is basically just your code..
    PHP Code:
    <?
      require("head.php");
      echo "this is my page";
      require("foot.php");
    ?>
    I've tried this way, but just thought it would be harder to maintain and update because I was used to having one localized and central file that controlled almost everything.

    I think that you should just try playing around with all of them, and then find out what works out best for you..

    Hope that helps a bit.

    edit: by the way, the page I included is for some blog software I'm writing..
    Like Archer? Check out some Sterling Archer quotes.

  3. #3
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    You could use a template engine, there are lots out there, then all you need to is make your html template with variables in it then change them in your PHP script.

    A good example of one is Smarty.

    http://smarty.php.net

    There are loads out there and you could build your own if u have some time on your hands

  4. #4
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    I use a slightly different method. I have a header.php, footer.php, and then a base file for each different function of my website. The base file is setup in a template that has an include for the header and footer and then the content for that page is put in a specific set of <TD> elements. I also have a navigation and database connection page that are included just like the header and footer.

    My template:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
        <title></title>
        
    <link rel="stylesheet" type="text/css"
    href="../ss.css" />

    </head>

    <?php
    include("conn.php");
    ?>
    <body bgcolor="White" alink="blue" vlink="blue" link="blue" topmargin="0" leftmargin="0" rightmargin="0">

    <?php
    include("../header.php");
    ?>


    <!----------table containing "frame" contents----------------->
    <table width="100%" cols="2" cellpadding="0" cellspacing="0">
    <tr>
    <td width=180 bgcolor="#0099ff" valign="top">
    <?php
    include("nav.php");
    ?>
    </td>

    <td>
    <br><br>






    <br><br><br><br>

    <?php
    include("../footer.php");
    ?>
    </td></tr>
    </table>
    <!----------END table containing "frame" contents----------------->

    <table width="100%" cellpadding="0" cellspacing="0">
    <tr><td width="180" bgcolor="#9999cc" align="center"><img src="/images/php-power-micro.png" width=80 height=15 alt="" border="0"></td>
    <td>&nbsp;</td>
    </tr></table>
    </body>
    </html>
    I can't show you this online, because it's an internal website, but I think you can get the idea.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  5. #5
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Ok, I took a screenshot of it and blanked out the company name. And the website it's hosted on is not the company. That's my side business.

    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can get the output of a PHP file into a variable, do this:
    PHP Code:
    ob_start();
    include(
    "path/to.php");
    $data ob_get_contents();
    ob_end_clean(); 
    $data now contains the output of the included file.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width