1

I am currently building a web application in PHP that will allow users to create content and upload to the website.

My website has differnet types of pages, and i'm looking for the best way (most efficient CPU wise/speed/load etc..) to load different parts of each page.

So for instance, my main page currently includes this part at the top:

<html>
<head></head>
<title>this is a unique title</title>
<body>
    Menu buttons, some header text, intro etc..

and this at the bottom:

<div>some random div</div>
<span>some copyright text</span>
</body>
</html>

I can use includes and place each part in a different php file, but i'm not sure it's the best way to do it, I can also build a function that prints those parts to the page but again, not really sure if that's the way to do it,

How do you guys do it?

Thanks,

Jim
  • 375
  • 2
  • 4
  • 11
  • 1
    Tip: Don't worry about the speed of the solution yet. Implement something that's easy to understand and easy to maintain. When it's working, only then determine if you need to speed it up (because it's not fast enough, or is two heavy). But worrying about speed now will only lead to a solution that's needlessly complicated and hard to understand... – ircmaxell Nov 07 '10 at 15:49
  • what is that content you are talking about? – Your Common Sense Nov 07 '10 at 15:59

1 Answers1

2

I don't think there's anything wrong with that approach efficiency-wise. The problem with splitting the header and footer up is that it makes things difficult maintenance-wise. It's a lot easier to maintain templates if you don't split them up.

So, in each page you would insert your content unique to that page into a variable (or a callback function) then output the template.

example what a template might look like:

<html>
<head><title><? //Code to get actual page title ?></title></head>
<title>this is a unique title</title>
<body>
<? //Code to get actual page body ?>
<div>some random div</div>
<span>some copyright text</span>
</body>
</html>
Kevin Stricker
  • 17,178
  • 5
  • 45
  • 71
  • it's not actually a template. – Your Common Sense Nov 07 '10 at 15:58
  • 1
    @Col. That's really useless feedback, and obviously refers to a more specialized definition of template than even the person asking the question was using. – Kevin Stricker Nov 07 '10 at 16:06
  • it would be a template if there will be ` include $body ?>` to include actual page's template. otherwise it would be the same mess. – Your Common Sense Nov 07 '10 at 16:12
  • Sorry, I was going for demonstrating how the overall design being easier to maintain, not suggest specific code. Updated to reflect that. – Kevin Stricker Nov 07 '10 at 16:19
  • See what do I mean. Everyone is taking template for the overall site design. While most important part is the particular page template. Your code is Ok. especially for the title part. But there ought to be a mention for the page template. Like this http://stackoverflow.com/questions/3988627/using-template-on-php/3989380#3989380 – Your Common Sense Nov 07 '10 at 16:24
  • So basically the solution that you are offering is to have 1 page which loads the content dynamically? How would I load the content? I have many different pages to display (not only different content, but different structures as well). – Jim Nov 07 '10 at 17:44
  • The link Col. Shrapnel provided does explain a simple way of implementing this strategy. – Kevin Stricker Nov 07 '10 at 18:08
  • 1
    @Jim page content being loaded into page template. Each page have to have it's own template. While its being included into common template. – Your Common Sense Nov 07 '10 at 18:56