something
something

Reputation: 537

Masterpage in php

I'm trying to incorporate something like Masterpages into a website, and have been wondering about something.

Should you have one template site where you require the header, footer and content(by passing a variable which points to a content file).

example of template.php:

require(header.php);
require($content.php);
require(footer.php);

or should you just require the header and footer template in every site.

example of sonething_site.php:

require(header.php);
//content here
require(footer.php); 

Thanks

Upvotes: 7

Views: 33720

Answers (5)

Raghavendra Devraj
Raghavendra Devraj

Reputation: 457

First: Create a template page, I called mine master.php.

<body>
<div><?php include('menu.php');?></div>
<div><?php include($page_content);?></div>
<div><?php include('footer.php');?></div>
</body>

Second: Create a content piece, for example about_text.php.
<p>This is information about me.</p>

Third: Create the page with the actual name you want to use:
<?php
$page_content = 'about_text.php';
include('master.php');
?>

Upvotes: 2

Firnas
Firnas

Reputation: 1695

Simple Solution would be to have a file for master page and then calling it from your other pages, if you do not want to use any third-party template engine.

Example: master.php

<html>
   <head>
      <title>Title at Masterpage</title>
   </head>

   <body>

      <div id="menu">
         <a href="index.php">Home</a>
         <a href="about.php">About Us</a>
         <a href="contact.php">Contact Us</a>
      </div>

      <div><?php echo $content; ?></div>

      <div id="footer">Footer to be repeated at all pages</div>

   </body>
</html>

In your page for example about.php

<?php
   $content = 'This is about us page content';
   include('master.php');
?>

NOTE: If you want to have page contents in a separate file rather than setting $content variable, you can include the file in your page and assign it to $content. Then in master.php use include($content) instead of echo $content

Upvotes: 9

Vamsi Krishna B
Vamsi Krishna B

Reputation: 11490

Have you considered using a PHP template engine like Dwoo or Smarty .

The feature you may like Template Inheritance

Upvotes: 0

regality
regality

Reputation: 6554

Usually it is better to go with option A. There is one template file and all of the pages only have content. The reason for this is that if you need to change the architecture of your site or the way that your template page behaves, you aren't screwed because you have to edit 1000 pages.

Unless you have a reason to do otherwise it is also not a bad idea to use a CMS.

Upvotes: 0

DarthVader
DarthVader

Reputation: 55052

your first approach $content.php doesnt seem very secure, i can insert my remote page there.

So your second approach is better, take a look at smarty. it s a great template engine.

and be careful!!

Upvotes: 3

Related Questions