Thinking about abstraction in websites

I ran across Experiences of Using PHP in Large Websites on del.icio.us(you can find my feed at http://del.icio.us/scotty). To be honest, I haven’t actually made it all the way through it yet, though I plan to when I have some more time (maybe tonight).

However, the following passage struck a chord with me:

Divide every PHP page up into two parts: the former performs database queries and does whatever else is needed to calculate the content for the page, before storing the calculated content into a series of variables which - and this is crucial - contain no markup. Then the latter part of the page can simply embed variable values into HTML markup, using loops where appropriate to traverse potentially-complex data structures containing the actual content.

This is particularly timely as I’m in the process of doing some coding for Boulder High School, building a web database in php and mysql to help them match tutors with students who want tutoring. This is particularlly enjoyable, as I haven’t really done any ground up development in months, and forgot how much fun it is.

Anyway, the project has me thinking lots about structure and framework. I’ve been shamelessly ripping off some framework ideas from a software package I’ve been making extensive modifications to for a client, in which every page has a hidden variable called “page”, which designates the next page to be loaded, and a hidden variable “action”, which designates a function to be run before the next page is loaded. All pages are loaded and functions are called out of a central index page, where pages are stored in individual .php files, and all functions are stored in a large library file which is included in the index page.

This provides some nice seperation between program logic and presentation, but I still find myself writing functions called things like “show_student_table”, which prints out a table with a list of students, with all html markup.

Instead, what I take from the passage above, is that I should be writing things like “get_student_list()” which returns an array of the information I want to display, and then loop across the array in the presentation page itself.

I do think there is still some place for functions that create presentation elements that are reused often, such as dropdowns. But again, it’s importatnt to focus on seperating out the data retrieval and manipulation tasks from the presentation.

1 comment

  1. Jon Raphaelson Mar 13

    There are a few good points here, but I would like to point something out. The ’seperation of form and function’, while certainly not a brand new concept, is just now starting to become prevelent in the world of web design (as is evidenced by the explosion in web standards evangelists.) 5 years ago, no one would have batted an eyelash to see the odd empty, seemingly useless table declaration in a site design, because that was the only way to do something layout wise. Scripting languages are the same way.

    The thing about Perl, and C and any other ‘real’ programming language that has a CGI interface is that they are not made for the internet. PHP is, and in its way is better at doing certain things that manage a page itself. Why should I have to write a Perl function to output, in print statements, the DOCTYPE of my page?

Leave a reply