What is the best way to manage a layout in PHP?

I'deas i've had are to cut up the template then include sections as i need them. or is it better to have a few main templtes and inlclude the content?

Need a few pointers on this... Tutorials or guides would be great if anybody knows or any?

Thanks
Dougal

    3 years later

    I realise, the question was asked two years ago. Still, someone may have the same Q.

    This is a very broad question. Modern webpages are dynamically created. This means the content is in a database. The user makes a request to see something. The request goes to a (php) page that makes decisions what to create: is it a new or known user, is there history to the user, is it a member ... which part of the site the request is targetting ... what banner needs to be displayed, what language, which sponsors/advertisers will be displayed - if any - you continue the list.

    When all is decided, the page will be created on the fly.
    This is a CONTENT MANAGEMENT SYSTEM or CMS.

    You need to design the structure of the system, then code it.
    Or get an off-the-self one like Drupal or others.
    If you design it, you have full control and it will be EXACTLY what you want. You can fully control security. With off-the-shelf systems this is unlikely. Many has huge security issues.

    Cheers,
    Bill

      dougal85;10717989 wrote:

      What is the best way to manage a layout in PHP?

      I'deas i've had are to cut up the template then include sections as i need them. or is it better to have a few main templtes and inlclude the content?

      Need a few pointers on this... Tutorials or guides would be great if anybody knows or any?

      Thanks
      Dougal

      There are so many ways to do this that your question really is very broad. However, if you are going to be maintaining the website alone and it will have a uniform layout (pretty much every site that doesn't suck is uniform) you can make life easy on yourself and create header/footer/head/etc includes as you suspect. I am a big fan of keeping out anything that is not layout logic out of the url that will render layout (thus the reason for the MVC pattern.) In other words, perhaps the index.php page has the logic needed to decide which layout page to render. Data for that template or layout page is queried in the page that calls it. These orphaned variables in the template or layout pages can be a little messy. I used to be of the opinion that this was very poor practice, but it does give you maximum flexibility with the layout. I guess if you have a good way to organize this approach, it can be very effective. Check out one of the many PHP MVC frameworks (which often use URL rewriting to determine what function you want to call.) You don't have to use the framework but the separation of Model (database objects), View (Your layout), and Controller (the function that you call that loads the Model and sends the data to the View) is a wonderful approach to this conundrum.

      At the moment, I'm very into the CodeIgniter framework. Check out this tutorial on how easy it is to create a templating system using CodeIgniter.

        Write a Reply...