Forum Moderators: coopster
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
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.
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
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.