Skip to content

Commit e0d50fe

Browse files
committed
Merge pull request liferay#210 from jhinkey/dg-templates
LRDOCS-230 Add Layout Templates Chapter to DevGuide
2 parents 8d676d3 + 60098af commit e0d50fe

23 files changed

+248
-0
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
2+
# Creating Liferay Layout Templates [](id=lp-6-1-dgen05-creating-liferay-layout-templates-0)
3+
4+
Surely, you've added many a portlet to your pages by dragging them from the *Add* menu and dropping them into place with great satisfaction. But are there times when you find yourself limited by Liferay's layouts? Maybe your Feng Shui (pronounced *fung SHWAY*) senses are picking up on some negative energy? Or perhaps you find yourself adding the same portlets over and over again onto the same types of pages? Yikes! You can easily overcome these nuisances by creating your own custom layout templates. With layout template plugins you can design layouts that flow nicely, embed commonly used portlets and apply CSS, Velocity, and HTML to make your pages pop.
5+
6+
In this chapter, you'll learn about:
7+
8+
- Creating custom layout templates using the Plugins SDK
9+
- Designing templates with Liferay Developer Studio
10+
- Embedding portlets in layout templates
11+
12+
Let's get our "spatial relation" juices flowing as we create a custom layout template!
13+
14+
## Creating a Layout Template [](id=lp-6-1-dgen05-creating-a-layout-template-0)
15+
16+
Our Plugin SDK supports layout templates as plugins. And creating layout templates with Liferay Developer Studio is easier than ever.
17+
18+
In Developer Studio, go to *File* → *New* → *Liferay Project* and do the following:
19+
20+
1. Fill in the *Project* and *Display* names
21+
3. Select the appropriate *Plugins SDK* and *Portal Runtime*
22+
4. Select *Layout* as your plugin type
23+
5. Click *Finish*
24+
25+
![Figure 5.1: Creating a custom layout template project in Studio](../../images/layout-template-new-project.png)
26+
27+
Alternatively, in your terminal, you can create a layout template from your Plugins SDK's `layouttpl` folder, by executing the *create* script.
28+
29+
./create.[sh|bat] <project name> "<layout template title>"
30+
31+
Example on Linux and Mac OS X:
32+
33+
./create.sh 1-4-1-columns "1-4-1 Columns"
34+
35+
Example on Windows:
36+
37+
create.bat 1-4-1-columns "1-4-1 Columns"
38+
39+
Both Studio's *New Project* wizard and the *create* scripts generate layout template projects in your Plugin SDK's `layouttpl` folder. Great! Now, let's look at our newly generated layout template project.
40+
41+
## Anatomy of a Layout Template Project [](id=lp-6-1-dgen05-anatomy-of-a-layout-template-0)
42+
43+
In this section we'll go over the directory structure of the layout template project and learn about its various files.
44+
45+
Directory structure:
46+
47+
- *layout-template-name*-layouttpl/
48+
- docroot/
49+
- WEB-INF/
50+
- liferay-layout-templates.properties
51+
- liferay-plugin-package.properties
52+
- *layout_template_name*.png
53+
- *layout_template_name*.tpl
54+
- *layout_template_name*.wap.tpl
55+
- build.xml
56+
57+
The SDK automatically appends "-layouttpl" to the project name. You can create multiple layout templates in a layout template project. Each layout template should have its own thumbnail image (PNG), regular template (TPL) and mobile device template (WAP TPL). The "liferay-" files help to describe the layout templates for packaging and deployment.
58+
59+
Here is a snapshot of files for a layout template project named "Columns 1 4 1" as shown in Developer Studio's *Package Explorer*.
60+
61+
![Figure 5.2: Example layout template named "Columns 1 4 1" editor](../../images/layout-template-dirs.PNG)
62+
63+
We'll discuss the project files next.
64+
65+
### Layout template files [](id=lp-6-1-dgen05-layout-template-files-0)
66+
67+
One or more layout template plugins can reside in a layout template project. Each layout template plugin has the following files:
68+
69+
- ***layout_template_name*.tpl**: Velocity template that generates the HTML structure of the template
70+
71+
- ***layout_template_name*.wap.tpl**: Variant template for mobile devices. WAP stands for wireless application protocol.
72+
73+
- ***layout_template_name*.png**: Representation of the template. It is the thumbnail that you see in Liferay Portal when browsing from the Page Layout screen from the Dockbar. You are responsible for creating the actual thumbnail, but can use this default PNG as a starting point.
74+
75+
![Figure 5.3: Default layout template thumbnail](../../images/blank_columns.png)
76+
77+
### Liferay configuration files [](id=lp-6-1-dgen05-liferay-configuration-files-0)
78+
79+
A layout template project has the following Liferay configuration files:
80+
81+
- **liferay-layout-templates.properties**: Specifies the name of the layout templates and the location of their TPL and PNG files
82+
83+
- **liferay-plugin-package.properties**: Describes the plugin project to Liferay's hot deployer
84+
85+
Now that you are familiar with the layout template's files and directory structure, let's deploy a layout template on the server.
86+
87+
### Deploying Layout Templates [](id=lp-6-1-dgen05-deploying-a-layout-template-0)
88+
89+
Deploy your layout templates from either of the following environments:
90+
91+
- From *Developer Studio* - Drag your layout template project onto your server.
92+
93+
- From *Terminal* - Execute `ant deploy` from your layout template project directory.
94+
95+
Upon deploying your template, your server will output messages indicating that your template was read, registered and is now available for use.
96+
97+
Example server output:
98+
99+
Reading plugin package for columns-1-4-1-layouttpl
100+
Registering layout templates for columns-1-4-1-layouttpl
101+
1 layout template for columns-1-4-1-layouttpl is available for use, let's start *designing* our layout template.
102+
103+
Now that you know how to deploy your template, let's design your layout.
104+
105+
## Designing a layout template [](id=lp-6-1-dgen05-designing-a-template-0)
106+
107+
The layout template's generated TPL files are initially empty, leaving you with a fresh canvas to design your layout templates as you like. This can seem overwhelming; but don't worry. Devleoper Studio's *Layout Template Editor* with handy palette makes designing templates fun!
108+
109+
Open your layout template's TPL file in Studio's *Layout Template Editor* to see it displayed in *Visual (Experimental)* mode.
110+
111+
![Figure 5.4: TPL file in *Layout Template Editor* (palette is hidden)](../../images/layout-template-tpl-blank.png)
112+
113+
Then click the triangle in the upper left corder of the editor to bring the Palette into view.
114+
115+
![Figure 5.5: TPL file in *Layout Template Editor* with pallet in view](../../images/layout-template-tpl-blank-with-palette.PNG)
116+
117+
From the palette you can drag rows, columns, and even existing templates onto your canvas.
118+
119+
Start designing your new layout template by dragging an entire *existing* layout template into the editor. Then modify it by adding, deleting, and adjusting elements as you like.
120+
121+
![Figure 5.6: Example - Starting with existing template *2 Column (50/50)*](../../images/layout-template-tpl-start.png)
122+
123+
After adding rows and columns, drag their edges vertically and horizontally to achieve the desired dimensions. Remember, your layout is not etched in stone. In *Visual (Experimental)* mode, you can start over as many times as you like as you're working out your layout designs.
124+
125+
![Figure 5.7: Visually adjusted template](../../images/layout-template-tpl-visual-almost.png)
126+
127+
To fine tune the dimensions in your design, switch to *Source* view and edit your TPL files as necessary.
128+
129+
![Figure 5.8: Source view of the template](../../images/layout-template-tpl-src-almost.png)
130+
131+
For example, if you wanted each of the middle-row columns in the above template to be 25%, you'd simply adjust that row's first column (i.e. `column-2`) from 20% to 25% as follows ...
132+
133+
Change ...
134+
135+
<div class="aui-w20 portlet-column portlet-column-first" id="column-2">
136+
$processor.processColumn("column-2", "portlet-column-content portlet-column-content-first")
137+
</div>
138+
139+
... to ...
140+
141+
<div class="aui-w25 portlet-column portlet-column-first" id="column-2">
142+
$processor.processColumn("column-2", "portlet-column-content portlet-column-content-first")
143+
</div>
144+
145+
Notice we changed the column's class `aui-w20` value to `aui-w25`.
146+
147+
Ta-da! The rows and columns of the "Columns 1 4 1" layout template are arranged and sized evenly, as I envisioned them!
148+
149+
![Figure 5.9: Layout design of template "Column 1 4 1" completed!](../../images/layout-template-tpl-visual-complete.png)
150+
151+
Now that we have some "positive energy" flowing in the design of our layout, let's spruce things up a bit by embedding some portlets in the template.
152+
153+
## Embedding portlets in a layout template [](id=lp-6-1-dgen05-embedding-portlets-in-a-layout-template-0)
154+
155+
You can embed portlets in layout templates to assure specific portlets always display in consistent locations on pages. Users may minimize these embedded portlets but are not permitted to move or remove the portlets from the page. Core portlets and custom portlets you create with the Plugins SDK can be embedded in the templates. Both instanceable and non-instanceable portlets can be embedded.
156+
157+
Let's embed the *navigation portlet* and *search portlet* in the first and last columns of our layout template's middle row. And while we're at it, we'll embed a couple of custom portlets in the upper and lower rows of the template.
158+
159+
**Embedded portlet attributes:**
160+
161+
- **Portlet ID** - Set this to the value of the portlet's `<portlet-name>` found in `docroot/WEB-INF/portlet.xml`. For core portlets, this is `liferay-portal/portal-web/docroot/WEB-INF/liferay-portlet.xml`.
162+
- **Core vs. Custom** - Whether the portlet is a core portlet or custom
163+
- **Instanceable** - Whether multiple instances of the portlet can exist in the portal
164+
- **Web Application Context** - The portlet's context (required for *custom* portlets only). Go to the portlet's *Look and Feel* &rarr; *Advanced Styling* to find the context in the Fully Qualified Portlet ID (FQPI). The context is the portion of the Portlet ID string that follows `WAR_`. The *Web Application Context* in the following figure is *myhelloworldportlet*.
165+
166+
![Figure 5.10: Fully Qualified Portlet ID (FQPI) for a custom portlet](../../images/layout-template-custom-portlet-look-n-feel.PNG)
167+
168+
The following table describes the portlets we'll embed in the layout:
169+
170+
| Portlet | &nbsp;ID | &nbsp;Row | &nbsp;Column | &nbsp;Type | &nbsp;Instanceable | &nbsp;Context |
171+
--------- | ----------| --------- | ------------- | ---------- | ------------ | ------------- |
172+
Navigation | &nbsp;`71` | &nbsp;2 | &nbsp;1 | &nbsp;core | &nbsp;yes | &nbsp;N/A |
173+
Search | &nbsp;`3` | &nbsp;2 | &nbsp;4 | &nbsp;core | &nbsp;no | &nbsp;N/A |
174+
Hello World | &nbsp;`my-hello-world-portlet` | &nbsp;1 | &nbsp;1 | &nbsp;custom | &nbsp;no | &nbsp;myhelloworldportlet |
175+
Goodbye World | &nbsp;`my-goodbye-world-portlet` | &nbsp;3 | &nbsp;1 | &nbsp;custom | &nbsp;yes | &nbsp;myhelloworldportlet |
176+
---
177+
178+
Here is the TPL that implements embedding these portlets:
179+
180+
<div class="columns_1_4_1" id="main-content" role="main">
181+
<div class="portlet-layout">
182+
<div class="portlet-column portlet-column-only" id="column-1">
183+
$processor.processPortlet("my-hello-world-portlet_WAR_myhelloworldportlet")
184+
$processor.processColumn("column-1", "portlet-column-content portlet-column-content-only")
185+
</div>
186+
</div>
187+
<div class="portlet-layout">
188+
<div class="aui-w25 portlet-column portlet-column-first" id="column-2">
189+
$processor.processPortlet("71_INSTANCE_xyz1")
190+
$processor.processColumn("column-2", "portlet-column-content portlet-column-content-first")
191+
</div>
192+
<div class="aui-w25 portlet-column" id="column-3">
193+
$processor.processColumn("column-3", "portlet-column-content")
194+
</div>
195+
<div class="aui-w25 portlet-column" id="column-4">
196+
$processor.processColumn("column-4", "portlet-column-content")
197+
</div>
198+
<div class="aui-w25 portlet-column portlet-column-last" id="column-5">
199+
$processor.processPortlet("3")
200+
$processor.processColumn("column-5", "portlet-column-content portlet-column-content-last")
201+
</div>
202+
</div>
203+
<div class="portlet-layout">
204+
<div class="portlet-column portlet-column-only" id="column-6">
205+
$processor.processPortlet("my-goodbye-world-portlet_WAR_myhelloworldportlet_INSTANCE_jkl1")
206+
$processor.processColumn("column-6", "portlet-column-content portlet-column-content-only")
207+
</div>
208+
</div>
209+
</div>
210+
211+
The following figure shows a page with the portlets embedded in the "Columns 1 4 1" layout template.
212+
213+
![Figure 5.11: Page with portlets embedded in layout template](../../images/layout-template-embed-portlets-visual.PNG)
214+
215+
See how simple it is to embed portlets in your pages?
216+
217+
## Available variables [](id=lp-6-1-dgen05-available-variables-0)
218+
219+
For your convenience, the variables listed in the following table are available to use in the TPL files.
220+
221+
| Variable | &nbsp;Type | &nbsp;Description |
222+
| -------- | ---------- | ----------------- |
223+
$processor | &nbsp;com.liferay.portlet.layoutconfiguration.util.velocity.TemplateProcessor | &nbsp;[Javadoc](http://docs.liferay.com/portal/6.1/javadocs-all/com/liferay/portlet/layoutconfiguration/util/velocity/TemplateProcessor.html) |
224+
$request | &nbsp;javax.servlet.http.HttpServletRequest| |
225+
$themeDisplay | &nbsp;com.liferay.portal.theme.ThemeDisplay | &nbsp;[Javadoc](http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/theme/ThemeDisplay.html) |
226+
$company | &nbsp;com.liferay.portal.model.Company | &nbsp;[Javadoc](http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/model/Company.html) |
227+
$user | &nbsp;com.liferay.portal.model.User | &nbsp;[Javadoc](http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/model/User.html) |
228+
$realUser| &nbsp;com.liferay.portal.model.User | &nbsp;[Javadoc](http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/model/User.html) |
229+
$layout| &nbsp;com.liferay.portal.model.Layout | &nbsp;[Javadoc](http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/model/Layout.html) |
230+
$layouts| &nbsp;java.util.List<com.liferay.portal.model.Layout> ||
231+
$plid| &nbsp;java.lang.Long ||
232+
$layoutTypePortlet| &nbsp;com.liferay.portal.model.LayoutTypePortlet | &nbsp;[Javadoc](http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/model/LayoutTypePortlet.html) |
233+
$portletGroupId| &nbsp;java.lang.Long ||
234+
$locale| &nbsp;java.util.Locale ||
235+
$timeZone| &nbsp;java.util.TimeZone ||
236+
$theme| &nbsp;com.liferay.taglib.util.VelocityTaglib | &nbsp;[Javadoc](http://docs.liferay.com/portal/6.1/javadocs-all/com/liferay/taglib/util/VelocityTaglib.html) |
237+
$colorScheme| &nbsp;com.liferay.portal.model.ColorScheme | &nbsp;[Javadoc](http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/model/ColorScheme.html) |
238+
$portletDisplay| &nbsp;com.liferay.portal.theme.PortletDisplay | &nbsp;[Javadoc](http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/theme/PortletDisplay.html) |
239+
---
240+
241+
Excellent! Your layout template toolbox is complete.
242+
243+
## Conclusion [](id=lp-6-1-dgen05-conclusion-0)
244+
245+
In this chapter you've learned how to create layout templates, arrange their rows and columns, and embed portlets. Congratulations on mastering the fundamentals of Liferay's layout templates! Hmm, are you a master of Feng Shui, too? Hey, if you get real good at arranging portlets on pages, your friends may ask you to re-arrange their living room furniture!
246+
247+
Next, we'll take a look at how to customize core Liferay portlets using hooks -- sounds "catchy" right?!
248+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)