SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm currently redesigning my web site using PHP and MySQL and so far so good. The reason why I'm doing this is that I totally want to separate the content from the layout so that I can do changes in just one layout file and the have the changes reflected on all pages. Before I was using SSI, but there where still some HTML tags left in the template that I copied each time I made a new page, meaning that if I wanted a total redesign, I would have to redo all pages.

    The only problem is that I find myself doing almost the same with PHP as I did with SSI, meaning that I put all the tags that make up the design in text files and then include them using the include() function. Having a separate title, keywords, description, top banner, small banners and content on each page, that results in quite a bit of include calls, and though I'm not putting every least bit of HTML into these text files, I can't help but think that there must be a better way to do this so the server won't have to handle all those include requests.

    Is there, and if so, how can I do this? I would love to hear how others are solving this problem.

    Thanks in advance !
    Sincerely,

    Eirik Johansen
    Netmaking AS

  2. #2
    SitePoint Addict
    Join Date
    Nov 2000
    Location
    London, UK
    Posts
    223
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I were you, I'd make a bunch of html files, that have some fields in it that could be replaced with content. For example, use _TITLE_ for the title field, then have your php script replace that with a title taken from a database or something. Also, check out http://www.leonatkinson.com and look at FreeEnergy. It basically is a system that has a bunch of modules like layout, actions, content, etc. Each one generates output into a stack, that is popped by the layout, and then outputted. That way everything could be separated. Good Luck!

    ... what's the world coming to?

  3. #3
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I strongly recommend looking into a template class. Teh best I've come across is the template class included with phpLib - it allows you to COMPLETELY seperate logic from content and is extremely powerful.

    Simply put, you will have an HTML file looking something like this:
    Code:
    <html>
    <head>
    <title>{TITLE}</title>
    </head>
    <body>
    <b><font size=3>{TITLE}</font></b><p>
    <small>Article by {AUTHOR}</small><p>
    {ARTICLE}<p>
    <small>{COPYRIGHT}</small>
    </body>
    </html>
    That file will be saved as-is in your template directory. The PHP script can then generate the content for {TITLE}, {AUTHOR} etc and, via the class, set the content to the different tags. The class can then load up the template, replace all the necessary tags and send it to the browser.

    In practise though phpLib template let you do a great deal more. There's support for "block" templates which allow you to, for example, generate a table of database results with the block template specifying how each table row should be formatted in HTML.

    The only downside is that the class is quite tricky to get the hang of at first. Try the following links:

    http://phplib.netuse.de/
    http://www.phpbuilder.com/columns/david20000512.php3
    http://www.users.f2s.com/faq/phplib_db.php3
    http://www.phpbuilder.com/columns/chad19990414.php3
    http://www.devshed.com/Server_Side/P...Lib/print.html

    Hope that helps,

    Skunk

  4. #4
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys!

    I went with your suggestion, Skunk, and so far it has worked wonders. Thanks again !
    Sincerely,

    Eirik Johansen
    Netmaking AS

  5. #5
    SitePoint Addict
    Join Date
    Nov 2000
    Location
    London, UK
    Posts
    223
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Skunk:

    OMG, I didn't know about that phpLib thingy but you're right its xtremely kewl. Thanks for the resource ... I checked it out a little while ago and am now redesigning my site around it, 'cause it makes everything MUCH easier. Tx!

    ... what's the world coming to?

  6. #6
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sounds cool, but that looks way more difficult and harder to understand than just using include().

    Right now, I have a "template" that has a include(header) at the top and an include(footer) at the bottom. I change those two files and the look of the site is changed.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like Skunk said phplib templates are super sweet. With block level access you can't go wrong. Let's reexamine Skunk's example and add in some code from the phplib db class:

    template file:
    box.ihtml
    Code:
    <!-- start box.ihtml -->
    <table border=1>
    <!-- BEGIN userBlock --> 
    <tr>
    <td>{username}</td>
    <td>{password}</td>
    <td>{perms}</td>
    </tr>
    <!-- END userBlock --> 
    </table>
    <!-- end box.ihtml -->
    That is the template file. It has a block named userBlock which we will use later.


    php script to generate content
    Code:
    <?
    include("template.inc");
    include("db.php");
    $db = new DB;
    $t = new Template;
    $t->set_file("box","box.ihtml");
    $t->set_block('box', 'userBlock', 'uBlock'); 
    $db->query("SELECT * from auth_user");
    while($db->next_record()) {
    	$t->set_var(array(
    	    "username" => $db->f(username),
    	    "password" => $db->f(password),
    	    "perms" =>` $db->f(perms)
    		));
    	$t->parse("uBlock", "userBlock", true);
    	}
    $t->pparse("out", "box");	
    ?>


    It is fairly starightforward and has so many benefits. I hope my little lesson helped break the ice.
    Last edited by freddydoesphp; Mar 23, 2001 at 10:47.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Louie...are you speaking English?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes


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
  •