Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / PHP
Tip/Trick

Pattern for PHP like the ASP site.master Pattern

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
24 Jun 2019CPOL 2.5K   1  
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
<?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
<?php

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

}
?>

Once instantiated, the site PHP is included.

my-web-page.php

PHP
...

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

The site HTML template can then pull in the content.

Site.php

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

The above has replaced the various ASP controls.

Site.master

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

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

my-web-page.aspx

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

"blah blah"

</asp:Content>

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

History

  • v1 - Created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --