Forum Moderators: coopster

Message Too Old, No Replies

Been thinking about how I structure my sites lately and was wondering-

         

amnesia440

4:00 am on Jun 23, 2008 (gmt 0)

10+ Year Member



I am trying to reteach myself how to properly structure my websites.

Right now I basically keep the content mixed in with the PHP, but try my best to keep layout (css) as seperate as possible.

With the 'new' (new to me) XML capabilities in PHP5 (and with the wide adoption of PHP5) I am thinking about the best way to structure things.

Is it now 'best practice' to keep content separate from design separate from code? All the layout would be in CSS files, the functionality would be in the PHP files, and the content stored in XML files and accessed via simplexml (still wrapping my head around xpath).

This would also include standardized site wide content such as header info, menus, and copyright information.

And I have also noticed that people are now separating layout from colors in different CSS files.

Is this the best way to do things?

Thanks
Kevin

npwsol

5:44 pm on Jun 23, 2008 (gmt 0)

10+ Year Member



There are arguably many "best practices," and I certainly don't hold the answers myself. Frequently it's based on the project size and its intentions. For example, while making my own site to be used in a portfolio, I made an object-oriented system which loads a template file (using DOMDocument's loadHTML) and inserts content through DOM methods. The result is a site where the root document is only a PHP script, which then serves a template. This ensures all templates display the same data while being independent of the data provided.

However, that's for my site, where development time is not a concern and I want it to show off my application design skills. On a client website, one might be more inclined to embed PHP directly in the documents (a bad idea, I find, but fast). Header info and menus, without question, should be in included files; this will save you hours of work on a large site when someone adds a new page ;)

As for CSS, I use one main stylesheet, one for IE6 if necessary (hasn't been, lately), and only any others if a particular page calls for a massively different structure (headings on this page need to be inline and green! and divs all need a 1px border! And the header? I don't even wanna go there...)

Lately I've received layouts with five or six stylesheets from my designer, each of which is engendered for a specific type of page (form, internal, home, common, etc.). This can be effective if it's implemented correctly, but that's the key; some of the ones in question have not been implemented well, leaving me to sift through each of the stylesheets to, say, change the font face on the header for each page. Doesn't that, in a way, defeat the purpose of CSS? Colors might be a good idea, but going too much further can create a hassle.

Ultimately yes, it is a best practice to keep content and design seperate to the extent of being as general as possible in your tags and layout. Your document, the actual HTML, lays the order of the elements in your page and is thus also very important to your layout (unless you use absolute positioning, lord knows why).

It sounds like you might be doing something similar to my site's structure (using DOMDocument) with the simplexml. I'll comment that it's incredibly easy, and is a smart design (use your own judgement, I'm biased by my incredible intellect). It's also overkill if you don't think the site's layout will be changing much. There's no sense in designing a highly flexible system just to separate content and, well, content if you don't intend to flex it.

amnesia440

10:02 pm on Jun 23, 2008 (gmt 0)

10+ Year Member



Thanks, you make a great point(s)!

I'm still new to PHP and trying out many different ways of doing things. There seems to be a billion different ways to do one thing.

So, what is this loadHTML that you were talking about?

thanks!
Kevin

npwsol

1:39 pm on Jun 24, 2008 (gmt 0)

10+ Year Member



There are a billion ways to do one thing, which is one of the fun parts of PHP. What method you use should depend on what goals you're trying to accomplish on a site

Look up the DOMDocument object. It's basically a DOM implementation for PHP, so you can treat an HTML document as though you were working on it with Javascript (So you can use createElement, appendChild, setAttribute, etc.). In my implementation, I created a template class which implements it; the class itself takes the data from my parent application and applies it to my template document through the DOM functions; IDs are very useful here :P

amnesia440

6:47 pm on Jun 24, 2008 (gmt 0)

10+ Year Member



Hello npwsol -

Thanks for the info and I appreciate you taking the time to answer my posts/questions.

I will php.net the DOMDocument and see if it's beyond my level of understanding at this point. Something like this sounds like a way I would like to approach projects.

npwsol

3:50 pm on Jun 25, 2008 (gmt 0)

10+ Year Member



Hi Amnesia

I can't imagine it should be too difficult; so long as you understand a tree-structure in general (each node has children, or branches) the DOMDocument should be easy to understand. If you've ever used getElementById in JavaScript, you've played with the DOM ;)

That being said, if it eases the process for you I can send you my source code thus far. It's poorly documented (that's one thing I do need to take care of, and I might be inclined to before sending it), but that which helps helps. Plus, I'm an open source kinda guy. Just drop me a sticky mail if you want it and I'll zip it up.