Adding a template
This page continues the Creating pages with LemonStand article.
When you create multiple pages with LemonStand you will soon find that you repeat the same HTML code in all pages – the HEAD and BODY tags declarations, page headers and footers. Templates allow you to reuse the common code. Templates act as wrappers for your pages. They usually contain the required HTML tags like HEAD, TITLE and BODY, as well as common blocks of your pages – website navigation, footer, header, sidebar etc. You can have as many templates as you need. For simple websites two templates usually are enough – for the Home page and for inner pages. The following image illustrates the idea behind templates.
Let's create a simple template for our Hello World example. Go to the CMS/Templates page and click the Add template button. Specify any name in the Name field. The template we are creating will contain all required HTML tags, and the simple header and footer elements. The page itself will be rendered between the header and footer.
Paste the following code to the HTML Code field.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title><?= h($this->page->title) ?></title> </head> <body <? if (isset($body_class)): ?>class="<?= $body_class ?>"<? endif ?>> <p>This is a header</p> <? $this->render_page() ?> <p>This is a footer</p> </body> </html>
The key element in the page template code is the $this->render_page() function call. It outputs the current page content. You can use a single template with multiple pages, and this function call will always output the content of a specific page which your visitor views at the moment. The $this variable refers the Cms_Controller class object, which you can use for obtaining important information about the current page and the page execution environment.
Also, please note how the page title in the TITLE tag is displayed. It uses a piece of PHP code which refers to the $title field of the current page object. The current page object is always assigned to the $this->page field. This field contains a reference to the current Page object (an object of Cms_Page class). The h function which wraps the page title converts HTML entities to a safe representation (< becomes < and so on). You can find information about the most useful functions and classes in the Handbook block which is available in the Page, Template and Partial editors.
Click the Create button and then the Close button. Now it is time to assign the just create template to the Hello World page. Return to the CMS/Pages page and click the Hello World page. Select the template you just created in the Template drop-down list and click the Save button.
Now you can click the page link above the form and check how the page looks now. As you can see the header and footer declared in the template are displayed around the page content.
Next: Creating a partial
Previous: Creating your first page
Return to Creating Pages Basics
Comments
No comments posted so far.
Add your comment
Loading form...