Jim
Jim

Reputation: 375

Proper way to use templates on a php based website

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,

Upvotes: 1

Views: 295

Answers (1)

Kevin Stricker
Kevin Stricker

Reputation: 17388

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>

Upvotes: 2

Related Questions