Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Writing a Template System in PHP

  1. #1
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing a Template System in PHP

    I don't want to use SMARTY would it be a stupid idea to write my own code from scratch? Are there any tutorials on this?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    <?php
    echo "<html>.............";
    echo $database_variables;
    echo "......... ..</html>";
    ?>


    There ya go.

    Just expand on that.

    Check out php.net for specific function reference, and http://php-mysql-tutorial.com for a general overview of using databases with PHP.



    EDIT: Actually, two things I should add--
    You don't need to use echo like that.
    Here are two possibly better options:

    PHP Code:
    <?php ... ?>
    html here like normal
    <?php
    echo $vars;
    ?>
    more html
    ...etc
    OR
    PHP Code:
    <?php
    echo <<<OUT
    This is actually
    a string
    but even line breaks and " and '
    are ok, since it's a special way to hold a string.
    It starts with <<<NAME (then a line break)
    then you keep going with it and end with:
    NAME; (must be the only thing on the line, no tabs, etc.)
    Also, you can use variables in this
    (unlike plain html)
    do this: 
    {$var}, and it will replace that
    with the value of 
    $var, including the {}.
    And... that's it for this method...
    OUT;
    ?>
    Last edited by djr33; 03-26-2007 at 09:01 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Or, if you want a complex templating system, here are a few tutorials:

    http://www.phpfreaks.com/tutorials/70/0.php

    http://codewalkers.com/tutorials/58/1.html

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Sure. What I have above is very basic, but it IS really the basic framework of any system.

    Also, something important to remember is that you could store template html (standard stuff on each page) in the database AND individual content for each page, so those database variables would cover the template and the content both, with anything specific to the page not in the database in the php itself.

    Using ifs and such, you can create a one-php-files site with many "pages" based on GET variables (?page=3) then you can control how it is all output.

    In that case, you might just store the system wide template on the page, such as CSS, etc., then just get stuff from the database and you're set.

    That's how I've made some cool pages. Easy to modify later, too.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Heya dj well say I have this page, it's pretty simple but how would I make it so it works better, and I keep the php seperate to the html

    PHP Code:
    <?php

    /* connect to the mysql database and use different queries for the count of members */

    include 'library/config.php';
    include 
    'library/opendb.php';

    //navigation
    include("nav.php");

    //approved
    $a mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='A'");
    $aCount mysql_num_rows($a);

    //deleted
    $d mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='D'");
    $dCount mysql_num_rows($d);

    //on hold
    $h mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='H'");
    $hCount mysql_num_rows($h);

    //pending
    $p mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='P'");
    $pCount mysql_num_rows($p);

    //not sure
    $n mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='N'");
    $nCount mysql_num_rows($n);

    //rejected
    $r mysql_query("SELECT * FROM `tblmembers` WHERE `MemberApproved`='R'");
    $rCount mysql_num_rows($r);


    /* Make the layout */
    ?>
    <link href="cs_style.css" rel="stylesheet" type="text/css" />

    <table border="1" cellpadding="3" cellspacing="1">
      <tr valign="top">
        <td colspan="2">Membership Status</td>
      </tr>
     
      <tr valign="top">
        <td>Status</td>
        <td>Total</td>
      </tr>

      <tr valign="top">
        <td>Approved</td>
        <td><a href="showMembers.php?cat=a"><?php echo $aCount;?></a></td>
      </tr>

      <tr valign="top">
        <td>Deleted</td>
        <td><a href="showMembers.php?cat=d"><?php echo $dCount;?></a></td>
      </tr>

      <tr valign="top">
        <td>On Hold</td>
        <td><a href="showMembers.php?cat=h"><?php echo $hCount;?></a></td>
      </tr>

      <tr valign="top">
        <td>Pending</td>
        <td><a href="showMembers.php?cat=p"><?php echo $pCount;?></a></td>
      </tr>

      <tr valign="top">
        <td>Not sure</td>
        <td><a href="showMembers.php?cat=n"><?php echo $nCount;?></a></td>
      </tr>

      <tr valign="top">
        <td>Rejected</td>
        <td><a href="showMembers.php?cat=r"><?php echo $rCount;?></a></td>
      </tr>
       <tr valign="top">
        <td>Total</td>
        <td><?php echo $aCount $dCount $hCount $pCount $nCount $rCount;?></a></td>
      </tr>
    </table>

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That looks just fine to me. Not really sure what you want. You can't seperate the php and html as the php must generate the html.

    You could move the code around some, but that looks just fine.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Mar 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    If you want to totally seperate the html and the php then you should create your html pages seperately and where ever you need php to insert values like for example in your code above your using <?php echo $aCount;?> since the html code is in a php file and php will replace that code with the string value. What you could do is that you could write something like {aCount} there instead of that code and then let php read the html file and replace the {} with the variables and then echo everything. This way you'll have php and html totally seperated from each other

    I'd give you a full detailed example once i get back from school. Just let me know that you want it.

  8. #8
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Sparaker, yes that would be great if you could do it thanks a million!

    Heya dj... maybe I'm confusing myself I thought that with a php templating system the html and php were kept completely seperate...

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    "templating" is just as vague/general as saying "using a database", really. I can only help how I think I would do it, though there are many ways.
    You could try to seperate them, but I think that's not gonna help much.

    The template you have is fine.

    One thing you could try is to include the template html in the database too, if you think that would help.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    To put it in "real" terms, I use it for my site. I have the main page, and everything else is completely based on it. I use page includes for each page when the "p" variable is used accordingly.
    - Mike

Bookmarks

Posting Permissions

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