Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 887
something Avatar asked Nov 23 '11 21:11

something


People also ask

What is the use of master page?

What are master pages? Master pages are used to create consistency from page to page in a document. Master pages typicially contain page headers, footers, margin and column guides, and other elements that occur on multiple pages in your document.

What is difference between master page and content page?

The master page establishes a layout and includes one or more ContentPlaceHolder controls for replaceable text and controls. The content page includes only the text and controls that are merged at run time with the master page's ContentPlaceHolder controls.

What is master page explain?

A master page is a defined set of formatting that is applied to the sections of your document-style report. In a template, you can specify a master page that includes a header element, a footer element, and layout properties, such as orientation and borders.

What is master page explain with example?

A master page is an ASP.NET file with the extension . master (for example, MySite. master) with a predefined layout that can include static text, HTML elements, and server controls. The master page is identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary . aspx pages.


2 Answers

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

like image 198
Firnas Avatar answered Oct 04 '22 09:10

Firnas


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

like image 27
DarthVader Avatar answered Oct 04 '22 10:10

DarthVader