Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<com:TContent ID="Main">
2
 
3
<h1>Sharing Common Layout</h1>
4
 
5
<p>
6
In this section, we will use the <a href="http://www.pradosoft.com/demos/quickstart/?page=Advanced.MasterContent">master/content</a> feature of PRADO to share common layout among pages. Common layout refers to the area that is the same or largely the same for a set of pages. For example, in our blog system, all pages will share the same header, footer and side-bar containing shortcut links. A straightforward implementation is to repeat the common layout in every page. However, this approach is prone to error and is hard to maintain. The <a href="http://www.pradosoft.com/demos/quickstart/?page=Advanced.MasterContent">master/content</a> feature allows us to treat the common layout as a control which centralizes the logic and presentation of the common layout for every page.
7
</p>
8
 
9
<com:InfoBox>
10
It is also possible to share common layout via <a href="http://www.pradosoft.com/demos/quickstart/?page=Configurations.Templates1">template inclusion</a>, which is like PHP file inclusion. The drawback of template inclusion is that it is not self-contained and does not carry a class to contain the logic for the common layout.
11
</com:InfoBox>
12
 
13
 
14
<h2>Creating Master Control</h2>
15
 
16
<p>
17
We now create the master control <tt>MainLayout</tt> to represent the common layout shared by our blog pages. The <tt>MainLayout</tt> control is a template control extending from <tt>TTemplateControl</tt>. It requires a template file <tt>MainLayout.tpl</tt> and a class file <tt>MainLayout.php</tt> located under the same directory. To facilitate maintenance, we create a new directory <tt>protected/layouts</tt> to hold them.
18
</p>
19
 
20
<img src="<%~ directories3.gif %>" class="output" />
21
 
22
<p>
23
For the moment, <tt>MainLayout</tt> only contains a simple header and a footer, as shown in the following. In future, we will add a side-bar to it. Readers are also encouraged to enhance the layout with other features.
24
</p>
25
 
26
<com:TTextHighlighter CssClass="source" Language="prado">
27
<html>
28
&lt;com:THead />
29
<body>
30
&lt;com:TForm>
31
<div id="page">
32
 
33
<div id="header">
34
<h1>My PRADO Blog</h1>
35
</div>
36
 
37
<div id="main">
38
&lt;com:TContentPlaceHolder ID="Main" />
39
</div>
40
 
41
<div id="footer">
42
&lt;%= PRADO::poweredByPrado() %>
43
</div>
44
 
45
</div>
46
&lt;/com:TForm>
47
</body>
48
</html>
49
</com:TTextHighlighter>
50
 
51
<p>
52
The above shows the content in the template file <tt>MainLayout.tpl</tt>. Three new tags are used:
53
</p>
54
<ul>
55
<li><tt>&lt;com:TContentPlaceHolder&gt;</tt> represents <a href="http://www.pradosoft.com/docs/classdoc/TContentPlaceHolder">TContentPlaceHolder</a> control. It reserves the place in the template where content will be placed at. Here, the content comes from the pages who use this master control.</li>
56
<li><tt>&lt;com:THead&gt;</tt> represents <a href="http://www.pradosoft.com/docs/classdoc/THead">THead</a> control which represents the &lt;head&gt; tag in HTML. It allows PRADO to manipulate the &lt;head&gt; tag as a component (e.g., setting page titles, adding custom CSS styles.)</li>
57
<li><tt>&lt;%= %&gt;</tt> is an <a href="http://www.pradosoft.com/demos/quickstart/?page=Configurations.Templates2#et">expression tag</a>. It displays the evaluation result of the enclosed expression at the place where it appears.</li>
58
</ul>
59
 
60
<p>
61
The class file <tt>MainLayout.php</tt> is very simple:
62
</p>
63
 
64
<com:TTextHighlighter CssClass="source">
65
<?php
66
class MainLayout extends TTemplateControl
67
{
68
}
69
?>
70
</com:TTextHighlighter>
71
 
72
<com:InfoBox>
73
The file extension name for page templates is <tt>.page</tt>, while for non-page templates it is <tt>.tpl</tt>. This is to differentiate pages from other controls. They both use the same <a href="http://www.pradosoft.com/demos/quickstart/?page=Configurations.Templates1">template syntax</a>. For pages, their class files are optional (default to <tt>TPage</tt>), while for non-page controls, their class files are mandatory. Similar to Java, the name of a class file must be the same as the class name. Be careful about the case-sensitivity on Linux/Unix systems.
74
</com:InfoBox>
75
 
76
 
77
<h2>Using Master Control</h2>
78
<p>
79
To use the newly created master control, we will modify <tt>Home.page</tt> and <tt>Contact.page</tt>. In particular, we need to remove the header and footer from them because the master control will be responsible for displaying them; and we need to tell the two pages that they should use <tt>MainLayout</tt> as their master.
80
</p>
81
 
82
<p>
83
The following shows the content in <tt>Contact.page</tt> after the change:
84
</p>
85
 
86
<com:TTextHighlighter CssClass="source" Language="prado">
87
&lt;%@ MasterClass="Application.layouts.MainLayout" Title="My Blog - Contact" %>
88
 
89
&lt;com:TContent ID="Main">
90
 
91
<h1>Contact</h1>
92
<p>Please fill out the following form to let me know your feedback on my blog. Thanks!</p>
93
 
94
...textbox and validator for user's name...
95
 
96
...textbox and validators for user's email...
97
 
98
...textbox and validator for user's feedback content...
99
 
100
&lt;com:TButton Text="Submit" OnClick="submitButtonClicked" />
101
 
102
&lt;/com:TContent>
103
</com:TTextHighlighter>
104
 
105
<p>
106
Content enclosed within the <tt>&lt;com:TContent&gt;</tt> tag will be inserted into the place that is reserved by <tt>&lt;com:TContentPlaceHolder&gt;</tt> in the master template.
107
</p>
108
 
109
<com:InfoBox>
110
It is possible to have multiple <tt>TContentPlaceHolder</tt>'s in a master template and multiple <tt>TContent</tt>'s in a content template. They are matched to each other by their <tt>ID</tt> values. It is also possible to make a content template the master of another content template by placing a <tt>TContentPlaceHolder</tt> in the former. This is called <i>nested master</i>.
111
</com:InfoBox>
112
 
113
<p>
114
Besides <tt>&lt;com:TContent&gt;</tt>, we also see another new tag <tt>&lt;%@ %&gt;</tt> in the above, which is called <a href="http://www.pradosoft.com/demos/quickstart/?page=Configurations.Templates1#tct">template control tag</a>. It contains name-value pairs which are used to initialize the corresponding properties for the template owner, namely, the <tt>Contact</tt> page.
115
</p>
116
 
117
<p>
118
By setting <tt>MasterClass</tt> property as <tt>Application.layouts.MainLayout</tt>, we instruct the <tt>Contact</tt> page to use <tt>MainLayout</tt> as its master. Here, we are using the <a href="http://www.pradosoft.com/demos/quickstart/?page=Fundamentals.Components">namespace format</a> to refer to the <tt>MainLayout</tt> class.
119
</p>
120
 
121
<com:InfoBox>
122
Namespace format is widely used in PRADO programming. It is used together with <a href="http://www.pradosoft.com/demos/quickstart/index.php?page=Fundamentals.Components">path aliases</a>. PRADO defines two path aliases: <tt>System</tt> refers to the <tt>framework</tt> directory of the PRADO installation, and <tt>Application</tt> refers to the <tt>protected</tt> directory. The namespace <tt>Application.layouts.MainLayout</tt> can thus be translated as <tt>protected/layouts/MainLayout</tt> which is exactly the file name (without the extension <tt>.php</tt>) for the <tt>MainLayout</tt> class.
123
</com:InfoBox>
124
 
125
 
126
<h2>Alternative Ways of Specifying Master</h2>
127
 
128
<p>
129
There are several additional ways to specify the master class for a page.
130
</p>
131
 
132
<p>
133
We can specify master in code like the following to enable dynamic change of layout:
134
</p>
135
 
136
<com:TTextHighlighter CssClass="source">
137
<?php
138
class Contact extends TPage
139
{
140
    public function onPreInit($param)
141
    {
142
        parent::onPreInit($param);
143
        $this->MasterClass='Path.To.NewLayout';
144
    }
145
 
146
    // ...
147
}
148
?>
149
</com:TTextHighlighter>
150
 
151
<p>
152
In the above, we specify <tt>MasterClass</tt> in the <tt>onPreInit()</tt> method which is inherited from <tt>TPage</tt>. The method is invoked by PRADO right after the page instance is created. We thus can dynamically determine the layout to use when the page is requested. For example, when the page is requested by a registered user we use layout A, and layout B is used if a guest user is requesting the page.
153
</p>
154
 
155
<p>
156
We can also specify master in <a href="http://www.pradosoft.com/demos/quickstart/?page=Configurations.AppConfig">application configuration</a>  or <a href="http://www.pradosoft.com/demos/quickstart/?page=Configurations.PageConfig">page configuration</a>. The following shows the updated application configuration for our blog system:
157
</p>
158
 
159
<com:TTextHighlighter CssClass="source" Language="xml">
160
<?xml version="1.0" encoding="utf-8"?>
161
<application id="blog" mode="Debug">
162
  <!-- configuration for available services -->
163
  <services>
164
    <service id="page" class="TPageService" DefaultPage="Home">
165
      <!-- initial properties set for all pages -->
166
      <pages MasterClass="Application.layouts.MainLayout" />
167
    </service>
168
  </services>
169
</application>
170
</com:TTextHighlighter>
171
 
172
<p>
173
By doing so, we save the trouble of specifying master in every page template. If we decide to use a different master for the pages, we only need to change the application configuration. For this reason, in our blog system we will use this approach to specify master.
174
</p>
175
 
176
<com:InfoBox>
177
There is an order determining which master is acutally applied when it is specified in multiple places. In particular, <tt>onPreInit()</tt> takes precedence over page template over application/page configuration. Therefore, if we specify <tt>MainLayout</tt> in the application/page configuration and we specify <tt>SpecialLayout</tt> in <tt>Contact.page</tt>, the effective master would be the latter.
178
</com:InfoBox>
179
 
180
</com:TContent>