PDA

View Full Version : Seperating code an layout (general question)


Michiel
08-03-2003, 07:05 PM
Hi,

eversince I started writing classes I keep running into the same sort of problems. They all have to do with seperating code and layout. Before I used classes I just wrote php-scripts and directly printed html inside the script. Though the scripts were working fine, they weren't flexible at all!

From what I know the major advantage of classes is that they can be used very flexible, but somehow I'm not achieving this.

For example, I'm working on a stats class. The class stores data about the visitors and it can return them in an associative array (e.g. $stats["IE5"=>array("unique"=>5, "hits"=>10);). I know how I can display bar-charts out of this data. But I can't seem to figure out how I can display them, in a way that they can be easily be changed, without having to dig into the code of the class.

I have the same problem with a guestbook-class I'm making. Extracting the info from the database and putting it an array is easy. But how do I fit the data into a layout that is easy to edit. Or how can I allow multiple layouts without having to change something in the class itself?

I think it all comes down to a lack of basic knowledge of programming, for I have never had any real lessons. So I would be very happy, if you would share your secrets of flexible php-coding with me. How do you combine/ seperate php-code and layout?

I hope you understand my point and I'm looking forward to your replies!

Thanx, Michiel

Spookster
08-03-2003, 07:43 PM
To seperate your PHP coding from the layout you will need to get into template systems. Template systems basically just parse a file containing your HTML coding and look for markers. These markers identify in the template where certain PHP coding should go or do certain things. For example:



<html>
<head>
<title>{title}</title>
</head>
<body>
{content}
</body>
</html>



The parser would go through and find all occurences of the markers indicated by {} braces and replace them with whatever they are suppose to have.

There are many templating systems already around that you can use unless you wish to write your own.

Michiel
08-03-2003, 08:00 PM
Hi,

I am aware of template systems, but I'm not sure wheter they will solve my problem. With the stats-script I mentioned earlier it's not just a matter of getting some data out of the script and paste them into a template. Some sort of calculating has to be done as well for the relative height of the bars.

What I'm realy interested in, is how you setup your classes. Would you have a class that returns an array with data and would you have another class that is a sort of template engine? And if you need a table start, a table row and a table end to display your data, how and where would you define them?


In other words: how do real php-coders setup their scripts in a way that they are flexible and well organized?

If anyone knows a tutorial on this (general) topic, please post the url! Any personal tips are welcome as well.

Michiel

Spookster
08-03-2003, 09:36 PM
Originally posted by Michiel
Hi,

I am aware of template systems, but I'm not sure wheter they will solve my problem. With the stats-script I mentioned earlier it's not just a matter of getting some data out of the script and paste them into a template. Some sort of calculating has to be done as well for the relative height of the bars.

What I'm realy interested in, is how you setup your classes. Would you have a class that returns an array with data and would you have another class that is a sort of template engine? And if you need a table start, a table row and a table end to display your data, how and where would you define them?


In other words: how do real php-coders setup their scripts in a way that they are flexible and well organized?

If anyone knows a tutorial on this (general) topic, please post the url! Any personal tips are welcome as well.

Michiel

The answer is still templating systems. Your script can still output some HTML coding. Tables are for tabular data so if you need to output tabular data then you would generate the table in your code and then use the template engine to place it in the template.

As far as formatting goes, that is what CSS is for. The HTML is just for setting up the structure of the page. The CSS is for formatting and layout styles and such.

CrAzYCoDeR969
08-03-2003, 10:18 PM
If you are talking about how you structure php using classes...well you can do it however you like, but classes generally represent real objects, i find classes in php useful when i have stuff like an output stack/template engine and i don't want to make the vars global...so i would have something like this:


class Output
{
var $head;
var $body;

function ParseTemplate($name)
{
// string searching stuff done here
}
}


i always just structure scripts i make however i think will be fastest, i usually only use classes for stuff that would otherwise require global variables