DooPHP IRC channel


MasterPages in DooPHP?

Discussion about new desired features.

MasterPages in DooPHP?

Postby RichardM » Sun Sep 20, 2009 1:52 pm

Hey,

Been working on this tutorial and its highlighted to me how much I miss the ASP.NET Master Pages they provide. (Yes I was/still am and ASP.NET developer too).

I like the way you have predefine your main site layout in a single file which other Views can then extend / add to and also the Master Pages ability to load other 'dynamic' content based on the Request data i.e. Loading in the site navigation component which turns a XML / DB tree into a menu and highlights the current document and standard footers.

I know we can make use of the <!-- include... syntax but if I decide I want to update my complicated layout which may be using more than just a standard header and footer template I would need to go through every view template and add more lines of code...which as you might be able to guess I would prefer not to have to go doing. Instead intergrating the likes of PlaceHolders which a View can then specify the content of. This would also be similar to the likes of Joomla but I would rather have more progmatic control over what overriding content was placed into placeholders rather than using the Menu System to decide (I know this works in joomla because the average user never touches the code).

Also the ability to nest one layout inside of another can be useful to i.e. insert master template 2 inside master template 1.

I know some others (including myself) have discussed this a little in some of the other threads but I do not think the feature has been specifically requested...is there a chance this might be something to be added in say 1.3 or 1.4?

Thanks,
Richard
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Re: MasterPages in DooPHP?

Postby leng » Sun Sep 20, 2009 2:08 pm

Can you possibly write down how .NET master page works briefly?
Also what's the difference between the ".Net master page" and the use of a layout (master view) like what some members have been discussing?

Perhaps it is similar to the master page in ASP.NET? It has the header, footer and all other code except the content?
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: MasterPages in DooPHP?

Postby RichardM » Sun Sep 20, 2009 3:43 pm

Hi,

I will try and find some good examples of this as soon as possible. I learnt about them through a quick tutorial a long time back and just been using them ever since.

The basic idea is to define a "Master Page" (like a layout I guess in other frameworks and applications...including the likes of Joomla). This might look something like this:

Code: Select all
<%@ Master %>
<html>
<body>
<h1>Site Name</h1>
<div class="wrapper">
<div class="menu">
<asp:ContentPlaceHolder id="MainMenu" runat="server"></asp:ContentPlaceHolder>
</menu>
<div class="mainContent">
<asp:ContentPlaceHolder id="MainContent" runat="server"></asp:ContentPlaceHolder>
</div>
<div class="sideContent">
<asp:ContentPlaceHolder id="SideContent" runat="server"></asp:ContentPlaceHolder>
</div>
</div>
</body>
</html>


You can then have a View file /protected/view/example.html (following DooPHP locations) which can then redefine what content to place into each placeholder. So you have something like:
Code: Select all
<%@ Page MasterPageFile="master1.master" %>

<asp:Content ContentPlaceHolderId="MainContent" runat="server">
  <h2>My Main Content for this Page</h2>
  <p>Some text</p>
</asp:Content>

<asp:Content ContentPlaceHolderId="SideContent" runat="server">
  <h3>Side Content</h3>
  <p>Some text</p>
</asp:Content>


The nice thing about this is the Views just specify what to show in a place holder. They do not care 'where this will go' within the layout specified in the master page. The master page can also define default content for a place holder so you can opt to override the content or just leave it alone depending on what you want that View file to do. You can also define a placeholder and leave its default content empty and also not define it in the View file and it will essentially appear not to exist but then a SpecialView could still define content for this so the homepage for example could contain an extra advert at the top of the page for example.

Additionally you can add code behind the MasterPage so it could for example determin what Site Theme the user is using and then map all of the css files to /theme/USER_CHOICE/styles.css and provide functions for outputting the page title which views could then call to set there page title.

You can also nest Master Pages within one another so you would define MasterPage2 to set a placeholder of MasterPage1 to now contain 2 placeholders (and some extra markup) then another View say Blog.html would use MasterPage2 which takes the MasterPage1 layout, then applies its changes and then fills the placeholders with the Views Content. This could be handy when you maybe want to have a variation of the main sites layout for a given section of the website.

This page has an introduction which partly demonstrates the basic use of a Master Page using VB.NET : http://weblogs.asp.net/scottgu/archive/2006/01/17/Data-Tutorial-_2300_2_3A00_-Building-our-Master-Page-and-Site-Navigation-Structure.aspx (up until about Figure 7)

I have not used the Zend_Layout (I decided the framework was to bloated and slow for my liking) but I think this has a similar approach and the documentation here : http://framework.zend.com/manual/en/zend.layout.quickstart.html is similar to what I am looking to achieve / see in DooPHP.

Essentially I just want to be able to define a site layout without ever creating a View...and then the View is doing what it should be...which is "The Content For X is ... and the Content for Y is ..." and just the formatting it really must include for achieving this. Maybe some sort of $this->renderLayout('masterLayout.html', 'myPageContent', ...); whereby the render would take the masterLayout.html and its placeholders + default value and then place into it the content from the myContentPage.html file.

Something like protected/layouts/MastePage.html :
Code: Select all
<html>
<body>
<h1>My Site</h2>
<div class="mainContent">
<!-- The content for this placeholder MUST be provided in the view -->
<!-- required_place_holder::XYZ -->
</div>
<div class="advert">
<!-- This placeholder is optional and the View does not have to provide any content. If none is provided we just use the default -->
<!-- optional_place_holder::ABC -->
<img src="defaultAdvertImage" />
<!-- end_optional_place_holder::ABC -->
</div>
</body>
</html>


Then in protected/view/myPage.html
Code: Select all
<!-- content::XYZ -->
<p>The pages content</p>
<!-- endcontent::XYZ -->


And if I wanted to also override the advert I could have
Code: Select all
<!-- content::XYZ -->
<p>The pages content</p>
<!-- endcontent::XYZ -->

<!-- content::ABC -->
<p>Some text advert instead</p>
<!-- endcontent::ABC -->


This would be great when you want to offer different site layouts for say a PC and an iPhone where you can have different site layouts and classes but you would probably still want the same view information.

Hope this helps clear up what I am thinking about?


Richard
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Re: MasterPages in DooPHP?

Postby RichardM » Sun Sep 20, 2009 3:50 pm

On second thoughts I just like the syntax idea I have given albeit it might need some of the items tweaking a little bit in terms of placeholder name types. You can also have the renderLayout method accept the array of content much in the same way as its done right now.

Code: Select all
class MyController extends DooController {

    $data = array("some_default_defintion" => "blah");

    public function example() {
        $this->data["db_records"] = /* Get some data from database */;
        $this->renderLayout('iPhoneLayout', 'example', $this->data);
    }
}


Richard
Last edited by RichardM on Thu Dec 24, 2009 10:07 am, edited 1 time in total.
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Re: MasterPages in DooPHP?

Postby roman » Tue Sep 22, 2009 5:20 pm

Richard, I think PHPTAL (http://phptal.org/) can allow you to do what you want.
roman
 
Posts: 442
Joined: Sat Aug 01, 2009 8:31 pm

Re: MasterPages in DooPHP?

Postby RichardM » Tue Sep 22, 2009 6:55 pm

roman wrote:Richard, I think PHPTAL (http://phptal.org/) can allow you to do what you want.


I'll take a look at it later...I know its got similar features to smarty, dwoo etc just more 'inline' with respect to things going into the (x)HTML markup.

I was/am looking at trying to keep things optionally using just the doophp template engine if possible...but if phptal does support this I might be tempted to use it. Alterntaivly I might well add this to some of the extensions to doophp I have in my list of possible things I would like to add into the framework over time as it will be useful to me (and I would hope others too)


Richard
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Re: MasterPages in DooPHP?

Postby roman » Wed Sep 23, 2009 9:00 pm

I think I use PHPTAL essentially in the way you described that you want. I have a master layout template (in one file) that I use for most pages and one or more files containing "macros" (views whose contents are inserted at places where the master template refers to them).

PHPTAL also has so called slots, but I've never used them or understood what they are about. Maybe I should give them another try.
roman
 
Posts: 442
Joined: Sat Aug 01, 2009 8:31 pm

Re: MasterPages in DooPHP?

Postby RichardM » Wed Sep 23, 2009 9:09 pm

Are you using phptal with DooPHP??? i.e. instead of DooPHPs View/Template engine


Richard
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Re: MasterPages in DooPHP?

Postby roman » Thu Sep 24, 2009 10:29 am

I don't use DooPHP yet. I haven't started a new project for which I could try it yet. But I am pretty sure PHPTAL could be used with the framework easily. See http://www.doophp.com/forum/viewtopic.php?f=7&t=31&p=160&hilit=phptal#p160 for an earlier discussion of using it. Maybe it will be helpful.
roman
 
Posts: 442
Joined: Sat Aug 01, 2009 8:31 pm


Return to Features Request

Who is online

Users browsing this forum: No registered users and 1 guest

cron