Masterpage in php
Asked Answered
O

5

7

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

Ott answered 23/11, 2011 at 21:20 Comment(2)
So many questions: What type of site? How diverse is the content? Have you looked at how wordpress does it(great php coding)?Rapturous
You may want to take a look at smarty.Bummalo
B
9

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

Boswall answered 22/6, 2013 at 13:23 Comment(0)
I
3

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!!

Isotron answered 23/11, 2011 at 21:22 Comment(3)
How could you insert your remote web page there ?Bummalo
@Nacereddine: Register globals, possibly.Blouin
ever heard of MVC? you dont insert a page, to a template. look at zend framework controller and use of smarty as well. if you are not carefull, anyone can insert a page to your site.Isotron
Y
2
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');
?>
Yoakum answered 18/5, 2015 at 10:24 Comment(0)
F
0

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.

Freshman answered 23/11, 2011 at 21:23 Comment(0)
F
0

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

The feature you may like Template Inheritance

Frostbitten answered 23/11, 2011 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.