How to design simple php site template ?

Discussion in 'PHP' started by baris22, Aug 19, 2009.

  1. #1
    I am trying to make a simple site template. Something which will be easy to update. I do not want to use template engines like smarty..

    Has anybody already got something like this? This is just an example

    
    
    <!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>
    <!--start head.php-->
    	<?php include("head.php"); ?>
    <!--end head.php-->
    </head>
    
    <body>
    
    <!--start top.php-->
    <div class="top">
    	<?php include("top.php"); ?>
    </div>
    <!--end top.php-->
    
    <!--start left.php-->
    <div class="left">
    	<?php include("left.php"); ?>
    </div>
    <!--end left.php-->
    
    <!--start page.php-->
    <div class="page">
    	<?php include("page.php"); ?>
    </div>
    <!--end page.php-->
    
    <!--start footer.php-->
    <div class="footer">
    	<?php include("footer.php"); ?>
    </div>
    <!--end footer.php-->
    
    </body>
    </html>
    
    
    PHP:

     
    baris22, Aug 19, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I simply do something like this:

    
    $header_title = 'Bla';
    $footer_text = 'Bla';
    //any other variables needed by header & footer..
    
    require 'header.php';
    
    echo 'Content goes here';
    
    require 'footer.php';
    ?>
    
    PHP:
     
    premiumscripts, Aug 19, 2009 IP
  3. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #3
    everything you can do with HTML do it.
    its faster and better.
    making a header.php just for the img is not a good idea.
     
    astrazone, Aug 19, 2009 IP
  4. softsolutions

    softsolutions Peon

    Messages:
    48
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for sharing here.
     
    softsolutions, Aug 19, 2009 IP
  5. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #5
    I made my own custom template class. I wanted to make it easy to use for designers that are not familiar with PHP, so it's pure HTML. It's not the best solution and there's probably similar solution somewhere, but it works for me.

    It's simplified usage goes something like this:
    
    $tpl->setTemplateDir();
    
    $tpl->loadTemplateContent();
    // Do $tpl->set("{placeholder}", $templateElement); here
    $tpl->compileTemplateContent();
    $tpl->clearTemplateData();
    
    $tpl->loadTemplateMain();
    $tpl->set("{content-goes-here}", $tpl->template("content"));
    $tpl->compileTemplateMain();
    
    $tpl->printTemplate();
    $tpl->clearGlobal()
    
    PHP:
     
    Gray Fox, Aug 19, 2009 IP
  6. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Might as well use Smarty when you get to that point. As more designers and developers are familiar with the smarty template engine.
     
    kblessinggr, Aug 19, 2009 IP
  7. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #7
    I will probably test my project with Smarty, since I didn't bother that much to get familiar with it :)
     
    Gray Fox, Aug 19, 2009 IP
  8. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Can anyone tell me why exactly you use smarty? I've always wondered. It adds an extra layer of code to your site which isn't really necessary. You may claim that it's a little more readable but I don't see much difference..

    {variable} = <?php echo $variable; ?>

    etc...
     
    premiumscripts, Aug 19, 2009 IP
  9. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #9
    I can tell you why I don't use it. Same reason you don't. Only rational reasons I can think of would be for standardizing, useful if lots of different people work on the project etc. and caching is built-in.

    And to the OP's code, I would question why you are putting the doctype into the main file? Why not have all of that in the header file? If you wanted/needed to change doctypes, it's in one file.


    
    <?php
    $page_title = "Demo";
    $page_meta ="Keywords List";
    
    @include "includes/site.php";
    @include "includes/header.php";
    ?>
    <div id="content">
    <!-- content starts here -->
          <h2 class="article_title">Article Title</h2>
    
    <!-- content ends here -->
    </div>
    <?php 
    @include "includes/sidebar.php";
    @include "includes/footer.php";
    ?>
    
    Code (markup):
    site.php checks if $page_title is set, if not it puts a default in place. Also sets the stylesheet to use and the other META tags. The DIVs for page layout are defined in the included files, could wrap those like the OP did in the main file but I find it easier to work with them in the include files.
    If you formalize the structure of the HTML you can have almost any layout, check layout gala http://blog.html.it/layoutgala/ for how to do this.
     
    shallowink, Aug 19, 2009 IP
  10. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #10
    http://smarty.net/manual/en/language.modifiers.php

    http://smarty.net/manual/en/language.builtin.functions.php

    http://smarty.net/manual/en/language.custom.functions.php

    Also some major projects use it such as phpBB, SMForum, x-cart, etc.

    I mean if you're coding strictly for yourself, sure use a dumbed down token method, otherwise standardizing and separating the coding layer from the design layer can help with people who may not be entirely familiar with your coding style. Not to mention it can also allow you to encode your logic while still allowing the design to be adjusted.

    To each their own of course.
     
    kblessinggr, Aug 19, 2009 IP
  11. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You're exchanging one coding style for another, smarty still is a coding style. Someone can't just come in and make changes to the foreach loop, etc..

    This can be done just the same with normal PHP. Do your preprocessing (db calls, etc) in a separate file, then assign template variables and require the template file.

    Anyway, I guess this is just personal preference again, can't really debate on such things :)
     
    premiumscripts, Aug 19, 2009 IP
  12. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #12
    More people are likely to understand smarty's rules, modifiers, and other functions that they can use to present the same data. Plus has it's own caching system if desired.
     
    kblessinggr, Aug 19, 2009 IP
  13. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #13
    Ok this is what i have got so far.

    My root folder:


    config
    #config.php
    #functions.php

    files
    #footer.php
    #header.php
    #left.php
    #right.php
    #topmenu.php

    images
    #logo.gif

    pages
    #about.php
    #contact.php
    #index.php

    index.php
    about.php
    contact.php

    On index.php I have got

    
    <?php
    $page_title = "title of site";
    $page_keyword = "keywords";
    
    define('_This_IS_not-FOR_YOU', TRUE);
    
    // start config
    include "config/config.php";
    // end config
    
    // start header
    include "files/header.php";
    // end header
    
    // start topmenu
    include "files/topmenu.php";
    // end topmenu
    
    // start content
    include "pages/index.php";
    // end content
    
    // start footer
    include "files/footer.php";
    // end footer
    
    ?>
    
    PHP:
    On pages/index.php I have got

    
    <?php defined('_This_IS_not-FOR_YOU') or die('Direct access not allowed.'); ?>
    <table width="800" border="1" bgcolor="#FFFF00">
      <tr>
        <td>&nbsp;</td>
        <td>Index</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    
    PHP:
    On files/header.php I have got this

    
    <?php defined('_This_IS_not-FOR_YOU') or die('Direct access not allowed.'); ?>
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo $page_title; ?></title>
    </head>
    <body>
    
    PHP:
    How does this system look? Any feedback
     
    baris22, Aug 20, 2009 IP