PageRenderTime 60ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/version/1.1.2/manual/offline/views.layouts.html

https://bitbucket.org/solarphp/docs
HTML | 609 lines | 597 code | 12 blank | 0 comment | 0 complexity | ed790fb66c5d80ba9c42ada97ea01ab0 MD5 | raw file
Possible License(s): IPL-1.0, LGPL-2.0, Apache-2.0, 0BSD
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <title>5.6. Layouts</title>
  7. <link rel="stylesheet" href="style.css" type="text/css" />
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.75.2" />
  9. <link rel="home" href="index.html" title="The Solar Framework for PHP" />
  10. <link rel="up" href="views.html" title="Chapter 5. Views and Layouts" />
  11. <link rel="prev" href="views.helpers.html" title="5.5. View Helpers" />
  12. <link rel="next" href="form.html" title="Chapter 6. Models and Forms" />
  13. <link rel="part" href="pt01.html" title="Part I. Getting Started" />
  14. <link rel="chapter" href="blog-demo.html" title="Chapter 1. Quick-Start Blog Demo" />
  15. <link rel="chapter" href="dispatch-cycle.html" title="Chapter 2. Dynamic Dispatch Cycle" />
  16. <link rel="chapter" href="model.html" title="Chapter 3. Working With Models" />
  17. <link rel="chapter" href="related.html" title="Chapter 4. Working With Related Models" />
  18. <link rel="chapter" href="views.html" title="Chapter 5. Views and Layouts" />
  19. <link rel="chapter" href="form.html" title="Chapter 6. Models and Forms" />
  20. <link rel="chapter" href="user.html" title="Chapter 7. User Authentication, Roles, and Access Control" />
  21. <link rel="chapter" href="commands.html" title="Chapter 8. Command-Line Tools" />
  22. <link rel="chapter" href="mail.html" title="Chapter 9. Mail" />
  23. <link rel="part" href="pt02.html" title="Part II. Appendices" />
  24. <link rel="appendix" href="appendix-standards.html" title="Appendix A. Project Standards" />
  25. <link rel="appendix" href="appendix-naming.html" title="Appendix B. Naming Conventions" />
  26. <link rel="subsection" href="views.layouts.html#views.layouts.setupdefault" title="5.6.1. Specify the Default Layout" />
  27. <link rel="subsection" href="views.layouts.html#views.layouts.createdefault" title="5.6.2. Create the Default Layout" />
  28. <link rel="subsection" href="views.layouts.html#views.layouts.head" title="5.6.3. Create the _head.php Sub-Layout" />
  29. <link rel="subsection" href="views.layouts.html#views.layouts.body" title="5.6.4. Create the _body.php Sub-Layout" />
  30. <link rel="subsection" href="views.layouts.html#views.layouts.header" title="5.6.5. Create the _header.php and _footer Sub-Layouts" />
  31. <link rel="subsection" href="views.layouts.html#views.layouts.nav" title="5.6.6. Create the _nav.php and _local.php Sub-Layouts" />
  32. <link rel="subsection" href="views.layouts.html#views.layouts.stylesheet" title="5.6.7. Create a Stylesheet" />
  33. <link rel="subsection" href="views.layouts.html#views.layouts.overriding" title="5.6.8. Overriding Layouts" />
  34. </head>
  35. <body>
  36. <div class="navheader">
  37. <table width="100%" summary="Navigation header">
  38. <tr>
  39. <th colspan="3" align="center">5.6. Layouts</th>
  40. </tr>
  41. <tr>
  42. <td width="20%" align="left"><a accesskey="p" href="views.helpers.html">Prev</a> </td>
  43. <th width="60%" align="center">Chapter 5. Views and Layouts</th>
  44. <td width="20%" align="right"> <a accesskey="n" href="form.html">Next</a></td>
  45. </tr>
  46. </table>
  47. </div>
  48. <div class="sect1" title="5.6. Layouts">
  49. <div class="titlepage">
  50. <div>
  51. <div>
  52. <h2 class="title" style="clear: both"><a id="views.layouts"></a>5.6. Layouts</h2>
  53. </div>
  54. </div>
  55. </div>
  56. <p>
  57. As mentioned in the introduction, layouts are typically shared by
  58. the entire application and wrap around a controller's view. A layout
  59. usually contains common page elements such as the header, footer,
  60. navigation, and the content area where the view's output is
  61. displayed. Layouts are often divided into separate sub-layouts. For example, the head
  62. section of your page may be one sub-layout, the global navigation
  63. another, and so forth. A single, parent layout file contains all the references
  64. to the sub-layouts. Your application can specify this parent layout using
  65. the protected <code class="literal">$_layout_default</code>
  66. property.
  67. </p>
  68. <p>
  69. The graphic below shows the relationship between the parent
  70. layout file, represented by the outer, yellow box, and each sub-layout. The
  71. positioning of each container is typically accomplished using Cascading
  72. Style Sheets (CSS).
  73. </p>
  74. <div class="figure">
  75. <a id="id36147977"></a>
  76. <p class="title">
  77. <b>Figure 5.1. 
  78. A graphical representation of a layout containing several sub-layouts.
  79. </b>
  80. </p>
  81. <div class="figure-contents">
  82. <div class="mediaobject">
  83. <img src="figures/views/layout.png" alt="A graphical representation of a layout containing several sub-layouts" />
  84. <div class="caption">
  85. <p>The main layout file is represented by the outer, yellow box. The <code class="filename">_head.php</code>
  86. section would be part of the source, and not visible on the screen.</p>
  87. </div>
  88. </div>
  89. </div>
  90. </div>
  91. <br class="figure-break" />
  92. <p>Let's make the blog demo created in chapter one look like the example graphic above.</p>
  93. <div class="sect2" title="5.6.1. Specify the Default Layout">
  94. <div class="titlepage">
  95. <div>
  96. <div>
  97. <h3 class="title"><a id="views.layouts.setupdefault"></a>5.6.1. Specify the Default Layout</h3>
  98. </div>
  99. </div>
  100. </div>
  101. <p>
  102. The first thing we need to do is specify which layout our blog
  103. application will use. Remember that the <code class="literal">Acme_App_Blog</code>
  104. application extends the <code class="literal">Acme_Controller_Page</code>
  105. class.
  106. </p>
  107. <p>Browse to the <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller</code>
  108. folder and edit the <code class="filename">Page.php</code> file.</p>
  109. <pre class="screen">$ <strong class="userinput"><code>cd <em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/</code></strong>
  110. $ <strong class="userinput"><code>vim Page.php</code></strong></pre>
  111. <p>Add the following text to the class so it looks like the code below.</p>
  112. <pre class="programlisting"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php
  113. </span><span style="color: #007700">abstract class </span><span style="color: #0000BB">Acme_Controller_Page </span><span style="color: #007700">extends </span><span style="color: #0000BB">Solar_Controller_Page
  114. </span><span style="color: #007700">{
  115. </span><span style="color: #FF8000">/**
  116. *
  117. * Sets up the Acme_App environment.
  118. *
  119. * @return void
  120. *
  121. */
  122. </span><span style="color: #007700">protected function </span><span style="color: #0000BB">_setup</span><span style="color: #007700">()
  123. {
  124. </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">_setup</span><span style="color: #007700">();
  125. </span><span style="color: #FF8000">// set the default layout for all applications that
  126. // extend Acme_Controller_Page
  127. </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_layout_default </span><span style="color: #007700">= </span><span style="color: #DD0000">'blog'</span><span style="color: #007700">;
  128. }
  129. }
  130. </span><span style="color: #0000BB">?&gt;</span></span></code></pre>
  131. <div class="note" title="Note">
  132. <table border="0" summary="Note">
  133. <tr>
  134. <td rowspan="2" align="center" valign="top" width="25">
  135. <img alt="[Note]" src="images/note.png" />
  136. </td>
  137. <th align="left">Note</th>
  138. </tr>
  139. <tr>
  140. <td align="left" valign="top">
  141. <p>
  142. You can also accomplish this by defining the protected
  143. <code class="literal">$_layout_default</code> property.
  144. </p>
  145. <p>
  146. <code class="literal">protected $_layout_default = 'blog';</code>
  147. </p>
  148. </td>
  149. </tr>
  150. </table>
  151. </div>
  152. </div>
  153. <div class="sect2" title="5.6.2. Create the Default Layout">
  154. <div class="titlepage">
  155. <div>
  156. <div>
  157. <h3 class="title"><a id="views.layouts.createdefault"></a>5.6.2. Create the Default Layout</h3>
  158. </div>
  159. </div>
  160. </div>
  161. <p>
  162. The default layout is <code class="filename">blog.php</code> and is located in the
  163. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Layout</code> folder.
  164. </p>
  165. <pre class="screen">$ <strong class="userinput"><code>cd <em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Layout</code></strong>
  166. $ <strong class="userinput"><code>vim blog.php</code></strong></pre>
  167. <p>Copy and paste the following into the <code class="filename">blog.php</code> file.</p>
  168. <pre class="programlisting"><code><span style="color: #000000">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  169. &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  170. <span style="color: #0000BB">&lt;?php
  171. </span><span style="color: #FF8000">// generate the &lt;head&gt;
  172. </span><span style="color: #007700">include </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">template</span><span style="color: #007700">(</span><span style="color: #DD0000">'_head.php'</span><span style="color: #007700">);
  173. </span><span style="color: #0000BB">?&gt;
  174. </span>
  175. <span style="color: #0000BB">&lt;?php
  176. </span><span style="color: #FF8000">// generate the &lt;body&gt;
  177. </span><span style="color: #007700">include </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">template</span><span style="color: #007700">(</span><span style="color: #DD0000">'_body.php'</span><span style="color: #007700">);
  178. </span><span style="color: #0000BB">?&gt;
  179. </span>&lt;/html&gt;</span></code></pre>
  180. <p>
  181. This is our main layout script. Within this layout are references
  182. to two sub-layouts, namely <code class="filename">_head.php</code> and
  183. <code class="filename">_body.php</code>.
  184. </p>
  185. </div>
  186. <div class="sect2" title="5.6.3. Create the _head.php Sub-Layout">
  187. <div class="titlepage">
  188. <div>
  189. <div>
  190. <h3 class="title"><a id="views.layouts.head"></a>5.6.3. Create the _head.php Sub-Layout</h3>
  191. </div>
  192. </div>
  193. </div>
  194. <p>
  195. Now you need the <code class="filename">_head.php</code>. Assuming you are still in the
  196. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Layout</code> folder,
  197. create the <code class="filename">_head.php</code> file.
  198. </p>
  199. <pre class="screen">$ <strong class="userinput"><code>vim _head.php</code></strong></pre>
  200. <p>Copy and paste the following text into the file and save.</p>
  201. <pre class="programlisting"><code><span style="color: #000000">&lt;head&gt;
  202. <span style="color: #0000BB">&lt;?php
  203. </span><span style="color: #FF8000">// use the Solar_View_Helper_Head::head() helper
  204. // add a base stylesheet, then set any other head elements
  205. </span><span style="color: #007700">echo </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">head</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">addStyleBase</span><span style="color: #007700">(</span><span style="color: #DD0000">'Acme/Controller/Page/styles/blog.css'</span><span style="color: #007700">)
  206. -&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">();
  207. </span><span style="color: #0000BB">?&gt;
  208. </span>&lt;/head&gt;</span></code></pre>
  209. <p>
  210. This layout script is responsible for the content between the
  211. <code class="literal">&lt;head&gt;&lt;/head&gt;</code> tags. It relies on the
  212. <code class="literal">Solar_View_Helper_Head</code> view helper to set the main stylesheet, and
  213. display any other head elements. See the API documentation on the
  214. <code class="literal"><a class="link" href="http://solarphp.com/class/Solar_View_Helper_Head" target="_top">Solar_View_Helper_Head</a></code> view helper.
  215. </p>
  216. </div>
  217. <div class="sect2" title="5.6.4. Create the _body.php Sub-Layout">
  218. <div class="titlepage">
  219. <div>
  220. <div>
  221. <h3 class="title"><a id="views.layouts.body"></a>5.6.4. Create the _body.php Sub-Layout</h3>
  222. </div>
  223. </div>
  224. </div>
  225. <p>
  226. Assuming you are still in the
  227. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Layout</code>
  228. folder, create the <code class="filename">_body.php</code> file.
  229. </p>
  230. <pre class="screen">$ <strong class="userinput"><code>vim _body.php</code></strong></pre>
  231. <p>Copy and paste the following text into the file and save.</p>
  232. <pre class="programlisting"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #FF8000">// set the id of the body tag. Handy for css and javascript </span><span style="color: #0000BB">?&gt;
  233. </span>&lt;body id="<span style="color: #0000BB">&lt;?php </span><span style="color: #007700">echo </span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">controller</span><span style="color: #007700">}</span><span style="color: #DD0000">-page" </span><span style="color: #0000BB">?&gt;</span>"&gt;
  234. &lt;div id="wrap"&gt;
  235. &lt;div id="header"&gt;
  236. <span style="color: #0000BB">&lt;?php </span><span style="color: #007700">include </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">template</span><span style="color: #007700">(</span><span style="color: #DD0000">'_header.php'</span><span style="color: #007700">); </span><span style="color: #0000BB">?&gt;
  237. </span> &lt;/div&gt;
  238. &lt;div id="nav"&gt;
  239. <span style="color: #0000BB">&lt;?php </span><span style="color: #007700">include </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">template</span><span style="color: #007700">(</span><span style="color: #DD0000">'_nav.php'</span><span style="color: #007700">); </span><span style="color: #0000BB">?&gt;
  240. </span> &lt;/div&gt;
  241. &lt;div id="main"&gt;
  242. <span style="color: #0000BB">&lt;?php </span><span style="color: #FF8000">// Add the content from the application controller </span><span style="color: #0000BB">?&gt;
  243. </span> <span style="color: #0000BB">&lt;?php </span><span style="color: #007700">echo </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">layout_content</span><span style="color: #007700">; </span><span style="color: #0000BB">?&gt;
  244. </span> &lt;/div&gt;
  245. &lt;div id="footer"&gt;
  246. <span style="color: #0000BB">&lt;?php </span><span style="color: #007700">include </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">template</span><span style="color: #007700">(</span><span style="color: #DD0000">'_footer.php'</span><span style="color: #007700">); </span><span style="color: #0000BB">?&gt;
  247. </span> &lt;/div&gt;
  248. &lt;/div&gt;
  249. &lt;/body&gt;</span></code></pre>
  250. <p>
  251. This layout script defines the structure of the page. A cascading
  252. stylesheet will define the styles for each page element. Each
  253. element of the page, such as the header, gets it's own sub-layout script. The
  254. output from an application controller's action is injected into the
  255. layout via the <code class="literal">$this-&gt;layout_content</code>
  256. property.
  257. </p>
  258. </div>
  259. <div class="sect2" title="5.6.5. Create the _header.php and _footer Sub-Layouts">
  260. <div class="titlepage">
  261. <div>
  262. <div>
  263. <h3 class="title"><a id="views.layouts.header"></a>5.6.5. Create the _header.php and _footer Sub-Layouts</h3>
  264. </div>
  265. </div>
  266. </div>
  267. <p>
  268. Assuming you are still in the
  269. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Layout</code>
  270. folder, create the <code class="filename">_header.php</code> file.
  271. </p>
  272. <pre class="screen">$ <strong class="userinput"><code>vim _header.php</code></strong></pre>
  273. <p>Copy and paste the following text into the file and save.</p>
  274. <pre class="programlisting"><code><span style="color: #000000">&lt;div id="branding"&gt;
  275. &lt;h1&gt;The Acme Blog&lt;/h1&gt;
  276. &lt;/div&gt;</span></code></pre>
  277. <p>This simple sub-layout creates the header banner at the top.</p>
  278. <p>
  279. Now create the <code class="filename">_footer.php</code> file in the same folder.
  280. </p>
  281. <pre class="screen">$ <strong class="userinput"><code>vim _footer.php</code></strong></pre>
  282. <p>Copy and paste the following text into the file and save.</p>
  283. <pre class="programlisting"><code><span style="color: #000000">&lt;div id="footer"&gt;
  284. &lt;p&gt;Copyright &amp;copy; 2010 Acme&lt;/p&gt;
  285. &lt;/div&gt;</span></code></pre>
  286. <p>This simple sub-layout creates the footer at the bottom.</p>
  287. </div>
  288. <div class="sect2" title="5.6.6. Create the _nav.php and _local.php Sub-Layouts">
  289. <div class="titlepage">
  290. <div>
  291. <div>
  292. <h3 class="title"><a id="views.layouts.nav"></a>5.6.6. Create the _nav.php and _local.php Sub-Layouts</h3>
  293. </div>
  294. </div>
  295. </div>
  296. <p>
  297. Next, we need some sort of navigation element. Assuming you are still in the
  298. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Layout</code>
  299. folder, create the <code class="filename">_nav.php</code> file.
  300. </p>
  301. <pre class="screen">$ <strong class="userinput"><code>vim _nav.php</code></strong></pre>
  302. <p>Copy and paste the following text and save.</p>
  303. <pre class="programlisting"><code><span style="color: #000000">&lt;div id="nav"&gt;
  304. &lt;ul&gt;
  305. &lt;li&gt;<span style="color: #0000BB">&lt;?php </span><span style="color: #007700">echo </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">action</span><span style="color: #007700">(</span><span style="color: #DD0000">'blog'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Blog Home'</span><span style="color: #007700">); </span><span style="color: #0000BB">?&gt;</span>&lt;/li&gt;
  306. &lt;li&gt;<span style="color: #0000BB">&lt;?php </span><span style="color: #007700">echo </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">action</span><span style="color: #007700">(</span><span style="color: #DD0000">'blog/add'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ACTION_ADD'</span><span style="color: #007700">); </span><span style="color: #0000BB">?&gt;</span>&lt;/li&gt;
  307. &lt;li&gt;<span style="color: #0000BB">&lt;?php </span><span style="color: #007700">echo </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">action</span><span style="color: #007700">(</span><span style="color: #DD0000">'blog/drafts'</span><span style="color: #007700">, </span><span style="color: #DD0000">'View Drafts'</span><span style="color: #007700">); </span><span style="color: #0000BB">?&gt;</span>&lt;/li&gt;
  308. &lt;/ul&gt;
  309. <span style="color: #0000BB">&lt;?php </span><span style="color: #FF8000">// allow for extra local navigation
  310. </span><span style="color: #007700">include </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">template</span><span style="color: #007700">(</span><span style="color: #DD0000">'_local.php'</span><span style="color: #007700">);
  311. </span><span style="color: #0000BB">?&gt;
  312. </span>&lt;/div&gt;</span></code></pre>
  313. <p>
  314. Notice the reference to the <code class="filename">_local.php</code> sub-layout.
  315. Adding this sub-layout lets us optionally add extra navigation for each
  316. controller. This will become clearer later on in this chapter.
  317. </p>
  318. <div class="note" title="Note">
  319. <table border="0" summary="Note">
  320. <tr>
  321. <td rowspan="2" align="center" valign="top" width="25">
  322. <img alt="[Note]" src="images/note.png" />
  323. </td>
  324. <th align="left">Note</th>
  325. </tr>
  326. <tr>
  327. <td align="left" valign="top">
  328. <p>
  329. The second list item in the navigation layout uses a locale
  330. key as the link text.
  331. </p>
  332. <pre class="programlisting"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">echo </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">action</span><span style="color: #007700">(</span><span style="color: #DD0000">'blog/add'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ACTION_ADD'</span><span style="color: #007700">); </span><span style="color: #0000BB">?&gt;</span></span></code></pre>
  333. <p>
  334. Please see the API documentation on the
  335. <code class="literal"><a class="link" href="http://solarphp.com/class/Solar_View_Helper_Action" target="_top">Solar_View_Helper_Action</a></code>
  336. helper, as well as chapter one for a brief introduction to
  337. locales.
  338. </p>
  339. </td>
  340. </tr>
  341. </table>
  342. </div>
  343. <p>
  344. In the same folder, create the <code class="filename">_local.php</code> file.
  345. </p>
  346. <pre class="screen">$ <strong class="userinput"><code>vim _local.php</code></strong></pre>
  347. <p>Copy and paste the following text and save.</p>
  348. <pre class="programlisting"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php
  349. </span><span style="color: #FF8000">// placeholder for local navigation</span></span></code></pre>
  350. </div>
  351. <div class="sect2" title="5.6.7. Create a Stylesheet">
  352. <div class="titlepage">
  353. <div>
  354. <div>
  355. <h3 class="title"><a id="views.layouts.stylesheet"></a>5.6.7. Create a Stylesheet</h3>
  356. </div>
  357. </div>
  358. </div>
  359. <p>
  360. All the layout elements are now in place. A quick view of the
  361. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Layout</code>
  362. folder should show the following files:
  363. </p>
  364. <pre class="screen">Acme/Controller/Page/
  365. Layout/
  366. _body.php
  367. _footer.php
  368. _head.php
  369. _header.php
  370. _local.php
  371. _nav.php
  372. blog.php
  373. </pre>
  374. <div class="note" title="Note">
  375. <table border="0" summary="Note">
  376. <tr>
  377. <td rowspan="2" align="center" valign="top" width="25">
  378. <img alt="[Note]" src="images/note.png" />
  379. </td>
  380. <th align="left">Note</th>
  381. </tr>
  382. <tr>
  383. <td align="left" valign="top">
  384. <p>
  385. Though not a requirement, prefixing partials and nested
  386. views/layouts with an underscore helps you easily identify
  387. them.
  388. </p>
  389. </td>
  390. </tr>
  391. </table>
  392. </div>
  393. <p>
  394. Browsing to the blog application at this point might leave you
  395. visually disappointed. In the <code class="filename">_head.php</code> file, we referenced a
  396. stylesheet called <code class="filename">blog.css</code>. This style sheet is responsible for positioning our
  397. page elements and setting fonts and colors. Let's create it now.
  398. </p>
  399. <p>
  400. First, create a folder called 'styles' in the
  401. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Public</code> folder.
  402. </p>
  403. <pre class="screen">$ <strong class="userinput"><code>cd ../Public</code></strong>
  404. $ <strong class="userinput"><code>mkdir styles</code></strong>
  405. $ <strong class="userinput"><code>cd styles</code></strong></pre>
  406. <p>
  407. Next, create a css file called <code class="filename">blog.css</code> in that newly created
  408. folder.
  409. </p>
  410. <pre class="screen">$ <strong class="userinput"><code>vim blog.css</code></strong></pre>
  411. <p>
  412. Copy and paste the following into the <code class="filename">blog.css</code> file and save.
  413. </p>
  414. <pre class="programlisting"><code><span style="color: #000000">body, html {
  415. margin: 0;
  416. padding: 0;
  417. color: #000;
  418. background-color: #fff;
  419. font-family: sans-serif;
  420. font-size: 10pt;
  421. }
  422. #wrap {
  423. width: 750px;
  424. margin:0 auto;
  425. background-color: #ccc;
  426. }
  427. #header {
  428. background-color: #666;
  429. margin: 0px;
  430. color: #fff;
  431. margin-top: 10px;
  432. }
  433. #header h1 {
  434. margin: 0;
  435. padding: 10px;
  436. }
  437. #nav {
  438. background-color: #ccc;
  439. width: 150px;
  440. float: left;
  441. }
  442. #main {
  443. float: left;
  444. width: 580px;
  445. background-color: #efefef;
  446. padding: 10px;
  447. min-height: 400px;
  448. }
  449. #main h2 {
  450. margin-top: 0px;
  451. padding-top: 0px;
  452. }
  453. #footer {
  454. background-color: #000;
  455. color: #fff;
  456. font-weight: bold;
  457. clear: both;
  458. padding: 5px;
  459. }
  460. #footer p {
  461. padding: 0px;
  462. margin: 0px;
  463. }</span></code></pre>
  464. <p>You should now have the following:</p>
  465. <pre class="screen">Acme/Controller/Page/
  466. Public/
  467. styles/
  468. blog.css
  469. </pre>
  470. <div class="note" title="Note">
  471. <table border="0" summary="Note">
  472. <tr>
  473. <td rowspan="2" align="center" valign="top" width="25">
  474. <img alt="[Note]" src="images/note.png" />
  475. </td>
  476. <th align="left">Note</th>
  477. </tr>
  478. <tr>
  479. <td align="left" valign="top">
  480. <p>
  481. The <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Public</code>
  482. folder is not directly accessible by the web browser. In order for the browser to find assets in the
  483. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Public</code> folder, a
  484. symbolic link was defined when the Acme vendor was created. If you browse to the
  485. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/docroot/public</code> folder, you will see
  486. a subfolder named Acme. The web server can access the public assets via that folder.
  487. </p>
  488. </td>
  489. </tr>
  490. </table>
  491. </div>
  492. <p>
  493. A quick browse to the blog (<a class="link" href="http://localhost/blog" target="_top">http://localhost/blog</a>) should show
  494. a screen similar to the image below.
  495. </p>
  496. <div class="screenshot">
  497. <div class="mediaobject">
  498. <img src="figures/views/blog.png" />
  499. </div>
  500. </div>
  501. </div>
  502. <div class="sect2" title="5.6.8. Overriding Layouts">
  503. <div class="titlepage">
  504. <div>
  505. <div>
  506. <h3 class="title"><a id="views.layouts.overriding"></a>5.6.8. Overriding Layouts</h3>
  507. </div>
  508. </div>
  509. </div>
  510. <p>
  511. Because layouts are shared, they greatly reduce duplication of
  512. code. However, there may be times when you want to alter a page
  513. element for one particular application. The modular approach to the layout and
  514. Solar's class stack (hierarchy of classes) makes this an easy task.
  515. For example, if we wanted to have application-specific links below the main
  516. navigation, we could easily do this by overriding the <code class="filename">_local.php</code>
  517. layout script.
  518. </p>
  519. <p>
  520. Create a new <code class="filename">_local.php</code> layout file in the
  521. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/App/Blog/Layout</code> folder.
  522. </p>
  523. <pre class="screen">$ <strong class="userinput"><code>cd <em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/App/Blog/Layout</code></strong>
  524. $ <strong class="userinput"><code>vim _local.php</code></strong></pre>
  525. <p>Add the following text and save.</p>
  526. <pre class="programlisting"><code><span style="color: #000000">&lt;ul&gt;
  527. &lt;li&gt;&lt;a href="#"&gt;Local Item One&lt;/a&gt;&lt;/li&gt;
  528. &lt;li&gt;&lt;a href="#"&gt;Local Item Two&lt;/a&gt;&lt;/li&gt;
  529. &lt;/ul&gt;</span></code></pre>
  530. <p>
  531. Now, refresh the browser and you should notice how the left
  532. navigation has changed. Here, the original <code class="filename">_local.php</code> layout script
  533. in the <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/Controller/Page/Layout</code>
  534. folder is overridden by the <code class="filename">_local.php</code> layout script in
  535. <code class="filename"><em class="replaceable"><code>SYSTEM</code></em>/source/acme/Acme/App/Blog/Layout</code>.
  536. </p>
  537. <p>
  538. Solar uses the file that is most closely related to the
  539. application, then, if it doesn't find what it's looking for, will
  540. work its way up the class stack (hierarchy).
  541. </p>
  542. <p>
  543. An example of the class stack for layouts would like this:
  544. </p>
  545. <pre class="programlisting"><code><span style="color: #000000">Array
  546. (
  547. [0] =&gt; Acme/App/Blog/Layout
  548. [1] =&gt; Acme/Controller/Page/Layout
  549. [2] =&gt; Solar/Controller/Page/Layout
  550. )</span></code></pre>
  551. <p>
  552. Note that <code class="literal">Acme_App_Blog</code> extends <code class="literal">Acme_Controller_Page</code>, which
  553. extends <code class="literal">Solar_Controller_Page</code>. Knowing this may help you understand
  554. how the class stack is built.
  555. </p>
  556. <div class="note" title="Note">
  557. <table border="0" summary="Note">
  558. <tr>
  559. <td rowspan="2" align="center" valign="top" width="25">
  560. <img alt="[Note]" src="images/note.png" />
  561. </td>
  562. <th align="left">Note</th>
  563. </tr>
  564. <tr>
  565. <td align="left" valign="top">
  566. <p>
  567. You can also turn the layout off by setting <code class="literal">$this-&gt;_layout = false</code>
  568. in your controller, or you can change the layout altogether
  569. using <code class="literal">$this-&gt;_layout = 'someotherlayout'</code>.
  570. </p>
  571. </td>
  572. </tr>
  573. </table>
  574. </div>
  575. </div>
  576. </div>
  577. <div class="navfooter">
  578. <table width="100%" summary="Navigation footer">
  579. <tr>
  580. <td width="40%" align="left"><a accesskey="p" href="views.helpers.html">Prev</a> </td>
  581. <td width="20%" align="center">
  582. <a accesskey="u" href="views.html">Up</a>
  583. </td>
  584. <td width="40%" align="right"> <a accesskey="n" href="form.html">Next</a></td>
  585. </tr>
  586. <tr>
  587. <td width="40%" align="left" valign="top">5.5. View Helpers </td>
  588. <td width="20%" align="center">
  589. <a accesskey="h" href="index.html">Home</a>
  590. </td>
  591. <td width="40%" align="right" valign="top"> Chapter 6. Models and Forms</td>
  592. </tr>
  593. </table>
  594. </div>
  595. <div xmlns="" class="revinfo"></div>
  596. </body>
  597. </html>