Lithium: Building Views Using Layouts


lithium-large

The last time I wrote about the Lithium framework, the focus was on getting information out of a MySQL database.  Once you have that information, in many cases you will want to show it to the user.  In this installment, we are going to look at how Lithium implements layouts for your output in an effort to improve reusability of code, which ultimately improves the developer’s ability to quickly get applications up and running and it makes maintenance down the road that much easier.

Lithium Layouts Defined

A layout is the basic skeleton of an HTML page.  Within a layout file, you would typically have a header, footer, and any other HTML code that should appear on every page that uses the layout.  Lithium has a file structure that makes it pretty easy to figure out what goes where.  For layouts, all files should be located in the \app\views\layouts  directory.  When naming an HTML layout file, the convention is  {layout_name}.html.php , so the default template in Lithium would be found at  \app\views\layouts\default.html.php .  You can also have layouts for other formats, such as XML, and you can name the file accordingly.

A layout file looks like what you might typically expect of an HTML file.  Below is the code for the default template that ships with Lithium:

All of the basic things you might expect are there.  In the  <head> section, you find Lithium’s code for displaying titles, linking CSS files & JS files, setting the Charset, etc.  There is a page header, and also the closing tags for body and html round out the layout file’s code.  When it comes to displaying the content of the page, you simply have to include a statement to echo  $this->content() in the layout wherever the content is going to be displayed, as shown in the code example above.

Using a Layout

Now that we have a layout, we need to apply it to a page. Layouts can be specified when the controller method/command is called and the output rendered to the user.  For example, if we have a layout named purchaseorder.html.php and we want to use that layout to display a purchase order on screen, we would do it as follows from the controller:

What is important to understand is that the content for this page is in the  /app/views/{controller_name}/index.html.php  file.  This is where we would format and display the html specific to the purchase order and it’s data.  By specifying the purchaseorder layout in the controller when we call the render() method, we are telling Lithium to load the correct view, and apply the specified layout to it.  If we did not want a layout applied to the page at all, that can be done by setting the layout to  false .

 Setting up Default Layouts

In the spirit of trying not to repeat code over and over again, there may be instances where you want to apply a layout globally across all methods in a controller.    To do this, you can create an initialization method in the controller and specify the default layout within it.  This is accomplished creating the protected _init()  method in the controller.  An example of how to create the method and specify the default layout is shown below:

This has been a basic primer on how to use layouts in Lithium that can be used with your application’s views.  Layouts are a key feature in Lithium that promotes code reusability and simplification of future maintenance of the application.  I hope you found this short tutorial useful and I hope it helps get you on the path to development success when using Lithium as your application’s framework.

-Andy

, , ,

  1. No comments yet.

Comments are closed.