Page 1 of 1

Templates and styling.

Posted: Sun Jan 03, 2010 5:16 pm
by SniperTowers
Hello

I'm currently building a site for myself and in the future I would be looking at releasing it. I want to have it so it is easy to customize the template without having masses of PHP knowledge. I usually structure my sites like this:

Code: Select all

<?php
session_start();
require ("connect.php");
require("functions.php");
include("template/header.php");
 
echo "This is my site";
 
include("template/footer.php");
?>
 
I have thought about storing the header.php and footer.php in a database and calling it back so it can be edited through the admin panel.
The trouble is I have many parts of code where the styling is included in the PHP, like so:

Code: Select all

<?php
session_start();
require ("connect.php");
require("functions.php");
include("template/header.php");
 
if(isset($_GET['r'])){
    $_SESSION['refer'] = $_GET['r'];
}
 
    $result = mysql_query("SELECT * from gifts ORDER BY `id`");
 
    while($list = mysql_fetch_array( $result )){
            echo "<div id=\"gifts\">";
        echo "<img src=\"admin/images/";
        echo $list['image'];
        echo "\" width=\"120px\" height=\"120px\" alt=\"GiftImage\" />";
        echo "<br/>";
        echo "<h5>";
        echo $list['title'];
        echo "</h5>";
        echo "<br/>";
        echo $list['description'];
        echo "<br/>";
        echo $list['referrals'];
        echo " Referrals";
        echo "<br/>";
        echo "</div>";
    }
 
include("template/footer.php");
?>
As you can see I've included a div in the while statement to style each individual item coming back from the database. I am aware this isn't the best way to style it.

I don't really want to use a template engine like smarty.
So my question is what is the best way to make a simple template editor? and what is the best way to structure my files above so there isn't design code and php code mixed up?

Thanks.

Re: Templates and styling.

Posted: Mon Jan 04, 2010 7:39 am
by jason
If you aren't interested in using a template engine like Smarty, then your best bet is to go about outputting a proper HTML page with id and name attributes littered across the page, and allow styling using CSS.

You could also come up with a simple replacement template engine. Basically, you output a bunch of div tags with code inside them as you do now, but each part is a bundle of HTML. In your main template, you might have a bunch of simple tags, like

Code: Select all

 
...
<div id="navigation">
<div id="main_nav"></div>
<div id="nav_advertising"></div>
<div id="nav_login"></div>
</div>
...
 
Your PHP code would replace each of the div's with it's own code with something simple like str_replace. In the above case, all you'd really need to do is replace the opening DIV, and just make sure in your PHP, you don't close the DIV. Anyways, this lets someone move everything around, and you are basically inputting special "modules" of HTML. You'd still want them styling in CSS, though. This just gives them more flexibility in ordering.