Master and content tutorial
From PRADO Wiki
Contents |
Introduction
Many of you know concept of includes. Well, Master/Content is not it. It's different. PRADO QuickStart tutorial gives basic cover of what they are. I am going to give practical example of it.
The example
First of all, we need Master page. Master page is considered a control and it must be extended from TTemplateControl and have two files: .tpl and .php one. Let's create simple example Master/Content relationship. I'll call Master page Layout.
Layout.tpl:
MasterContent 1<br /> <com:TContentPlaceHolder ID="Body" /> MasterContent 2
Layout.php:
class Layout extends TTemplateControl { }
Let's say we want to use it from Home.page:
Home.page:
<%@ MasterClass="Layout" %> <com:TContent ID="Body"> Some text here. </com:TContent>
One thing that you might trip is that PRADO will complain about you not having Layout control. You must make sure that you add directory in which your control sits to your application configuration. E.g. if I have protected/controls/MyMenu.php then my protected/application.xml file should look like this:
<?xml version="1.0" encoding="utf-8" ?> <application id="FooBar" mode="Debug"> <paths> <using namespace="Application.controls.*" /> </paths> </application>
This is the most basic use of Master/Content. However, I personally like some extensions to it.
Some useful things
Let's say I want that my Master page could have Title property and if Content page would have title too it would be prepended to Master's one. This can be acomplished in simple way:
Layout.php:
<?php /** * @author ArtÅ«ras Šlajus <x11@arturaz.afraid.org> * @license http://creativecommons.org/licenses/LGPL/2.1/ CC-GNU LGPL */ class Layout extends TTemplateControl { private $_Title = ''; private $_TitleSeparator = ' - '; public function OnInit($param) { parent::OnInit($param); if ($this->Title and $this->Page->Title) { $this->Page->Title = $this->Title . $this->TitleSeparator . $this->Page->Title; } } /** * @return string title */ public function getTitle() { return $this->_Title; } /** * @param string title */ public function setTitle($value) { $this->_Title = TPropertyValue::ensureString($value); } /** * @return string separator */ public function getTitleSeparator() { return $this->_TitleSeparator; } /** * @param string separator */ public function setTitleSeparator($value) { $this->_TitleSeparator = TPropertyValue::ensureString($value); } } ?>
Layout.tpl:
<%@ Title="Test" TitleSeparator=": " %>
Home.page:
<%@ MasterClass="Layout" Title="Text" %>
would generate page with title set to "Test: Text".
Setting MasterClass globally
Writing <%@ MasterClass="Layout" %> to each .page file can get pretty boring after a while. So I sniffed this idea from Demos that PRADO ships with the distribution.
We are going to set MasterClass globally and recursively. Of course we will still be able to override the default.
So we need file called config.xml in our pages/ directory with such contents:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <pages MasterClass="Layout" /> </configuration>
The file should be self explanatory. It's done! Every new page now will have its MasterClass property set to Layout. You can set other page attributes in this manner too (for example <pages MasterClass="Layout" Title="Default Title" />).
Accessing Master from Content
Master page can be accessed with this code: $this->Master.

