65.9K
CodeProject is changing. Read more.
Home

Pattern for PHP like the ASP site.master Pattern

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (1 vote)

Jun 24, 2019

CPOL
viewsIcon

2580

I wanted to convert a very simple site from ASP to PHP. The original ASP version had a Site.master. For the PHP version, I just wanted to have a simple pattern that mimicked this.

Introduction

I wanted to convert a very simple site from ASP to PHP. It is a static site, but using ASP had been convenient for reusing headers and footers, etc.

Background

The original ASP version had a Site.master, with ContentPlaceHolder(s) and each page had Content(s) to provide the relevant content.

For the PHP version, I just wanted to have a simple pattern that mimicked this.

The pattern is: a site HTML template has a page which defines the content.

Details

A PHP page defines the content on an object page, as functions that return the various content.

my-web-page.php

<?php
include "pagecontentinterface.php"; 
class page1 implements pagecontent {
    function getcontent1(){ return "blah blah" } 
...
}  //end of class
...

The functions that the site master calls are included in an interface, hence it was included and implemented.

pagecontentinterface.php

<?php

interface pagecontent{
  public function getcontent1();
...

}
?>

Once instantiated, the site PHP is included.

my-web-page.php

...

$page1 = new page1();
include "Site.php";
?>

The site HTML template can then pull in the content.

Site.php

...
<article>
   <?php echo $page1->getcontent1(); ?> 
</article>
...

The above has replaced the various ASP controls.

Site.master

<asp:ContentPlaceHolder id="content1PlaceHolder" runat="server" >
    </asp:ContentPlaceHolder>

is now <?php echo $page1->getcontent1(); ?>

my-web-page.aspx

<asp:Content id = "content1" runat = "server" 
    ContentPlaceHolderID = "content1PlaceHolder" >

"blah blah"

</asp:Content>

is now function getcontent1(){ return "blah blah" }

History

  • v1 - Created