Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: scrollbar
Div
stylefloat:right
titleRelated Articles
classaui-label
Content by Label
showLabelsfalse
showSpacefalse
titleRelated Articles
cqllabel in ("component-templates","component-classes") and space = currentSpace()

You'll

Layout Component

You may see frequent reference to a Layout Component in Tapestry documentation, but you won't find it such a component in the component reference. The Layout isn't a component, it's a component pattern.A Layout component exists component is a component that you create to provide common content elements across all of your pages in your application.

In traditional servlet development, you may be familiar with the use of a JSP include to include a banner across the top of your page and a copyright message across the bottom.

Tapestry doesn't have a mechanism for such includes, nor does it have the need.

Instead, you can create a component that acts like a template for your pages.

Layout.tml

In Tapestry, you could implement those recurring page elements as components (a banner component, a copyright component, etc.) and then add those components to every page.

But there's an even better way. Just create a layout component that provides the overall structure and recurring content for your pages:

Code Block
languagexml
titleLayout.tml (a template for a Layout component)
No Format

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_04.xsd">
    <head>
        <title>My Nifty Web Application</title>
    </head>
    <body>
        <div class="nav-top">
            Nifty Web Application
        </div>
    
        <t:body/>

        <div class="nav-bottom">
            (C) 20082012 NiftyWebCo, Inc.
        </div>
    </body>
</html>

Layout is a standard component, with a standard component template. Like all component templates, it will be stored on the classpath (i.e., under src/main/resources).

The magic is in the <t:body/> element in the center; this will be replaced by the page's content, whatever that is.

The In a real-world example, the two <div> elements above and below the <t:body> are, in this example, placeholders for the typical might contain the typical recurring content you'll see in across the pages of a web application: banners (and banner ads!), menus, login forms and so forth. Often these layout components get very complex ... in fact, in most many applications , the Layout component grows can grow to be more as complex than almost any page in the application.

Remember that if you include a link to a resource such as an image or a stylesheet, you must use an absolute URL. The same component will be used for pages in many different folders, or with many different activation contexts, meaning that relative URLs are not only different for different pages, but may shift unexpectedly.

Layout.java

No Format

@IncludeStylesheet("context:css/site.css")
public class Layout
{
}

Components must always have a Java class. In this trivial example, the Layout component does not have much logic. We can save ourselves some typing using the @IncludeStylesheet annotation (as opposed to directly adding the <link> element to the template.

Index.tml

as any other component.

Using the Layout in a Page

To use your layout component, just have each page in your application wrap itself in the layout, like this:

Code Block
languagexml
titleWelcome.tml (the template for a page)
No Format

<html t:type="layout" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_04.xsd">

   <h1>Welcome to the Nifty Web Application!</h1>

   <p>
        Would you like to <t:pagelink page="login">Log In</t:pagelink>?
   </p>
</html>

Note the "t:type="layout" part. That says, in effect, "wrap the layout component around my content".

The magic is in the <t:body/> element of the layout template; this will be replaced by each page's content, whatever that is.

Info

Remember that if your layout component includes a link to a resource such as an image or a stylesheet, you must use an absolute URL. The same component will be used for pages in many different folders, or with many different activation contexts, so relative URLs won't work. The best approach is to use the context binding prefix.

To keep our Welcome.tml page template relatively preview-ableThis is an example of using the Layout component. To keep our Index.tml template relatively previewable, we are using an <html> element and the t:type attribute to specify that it is a component. The At render time, the page's <html> tag will be removed, and replaced with the content from the Layout.tml template (which convieniently conveniently starts with an <html> element). The <t:body> element in Layout.tml will be replaced with the page-specific content here: the <h1> and <p> tags.

Any page in the application that follows this pattern, using the Layout component, will have the same look and feel.

Layout is a regular component like other, with an ordinary component template. Like all component templates, it will be stored on the classpath (typically under src/main/resources).

Components must always have a Java class. But in this trivial example, the Layout component doesn't need any logic:

Code Block
languagejava
titleLayout.java
@Import(stylesheet="context:css/site.css")
public class Layout
{
}

We use the @Import annotation (in 5.2 or later), as opposed to directly adding the <link> element to the template, for significant performance benefits described elsewhere. (For 5.0 and 5.1, use the deprecated @IncludeStyleSheet annotation instead.)

You may find that your application has more than one look and feel: perhaps user registration pages have one look, while administrative pages have another. This can be accomplished by having multiple Layout components layout components (using any names you choose) and using those different layout types for different pages.

Nested Layouts

Layouts are really just ordinary components, so they can be nested to any level needed. You can have, for example, a "CommonLayout" component that provides the peripheral elements for all your pages, and then a more specialized "AdminLayout" component that provides the layout only for the administrative pages, and make the AdminLayout component wrap itself in the CommonLayout component. So then the administrative pages would start with <html t:type="adminLayout" ...> and the other pages (and the AdminLayout component itself) would start with <html t:type="commonLayout" ...>.

Code Block
languagexml
titleAdminLayout.tml
<html t:type="commonLayout" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd">

    <h1>Administrative Functions</h1>

    <t:body/>

</html>

A more advanced example

Here's an example of a Layout component with a few more features. It has a "title" parameter, so that every page can pass in its own title to be rendered in the <title> tag and in an <h1> tag at the top of the HTML. There is also a "style" parameter that allows each page to pass in a block of CSS rules to be rendered in the <head> section of the page (useful for those few CSS rules that can't be put into a static CSS file). Notice the HTML5-style DOCTYPE declaration at the top, the charset definition as UTF-8, and the addition of an "alerts" component.

Code Block
languagexml
titleLayout.tml (a template for a Layout component)
<!DOCTYPE html>
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd">
    <head>
        <meta charset="UTF-8" />
        <title>${title} - NiftyWebCo, Inc</title>
        <style type="text/css">
            <t:delegate to="style" />
        </style>
    </head>
    <body>
        <div class="nav-top">
            Nifty Web Application
        </div>

        <h1>${title}</h1>

        <t:alerts/>
   
        <t:body/>

        <div class="nav-bottom">
            (C) 2012 NiftyWebCo, Inc.
        </div>
    </body>
</html>

The Alerts component above (Tapestry 5.3 and later) allows the application to present alert messages to the client in a consistent way. If you want alerts to always appear in the banner of your web site, it may make sense to put it in the layout component's template, as above.

The corresponding component class is still very simple, adding support for the "title" and "style" parameters:

Code Block
languagejava
titleLayout.java
@Import(stylesheet="context:css/site.css")
public class Layout
{
    /** The page title, for the <title> element and the <h1> element. */
    @Property
    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
    private String title;

    /** Optional CSS rules to place into the page head */
    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private Block style;
}

Here's how you might use the above layout component for a UserList page:

Code Block
languagexml
titleUserList.tml
<html t:type="layout" title="List of Users"
    xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd"
    xmlns:p="tapestry:parameter">
<p:style>
    TD.profile { background: url('${userPhoto}') }
</p:style>

<div>
    Imagine a table of user account information here.
</div>

</html>

The <p:style> element (and its contents) are passed to the layout component as a style parameter (a block parameter, in this case, so you must have the xmlns:p="tapestry:parameter" namespace declared, as above).

The rendered HTML would look like the following (whitespace aside, and assuming UserList.java has a backgroundImage property whose value is the string "http://www.example.com/fuzzy.gif"):

Code Block
languagexml
titleThe rendered HTML
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>List of Users - NiftyWebCo, Inc</title>
        <style type="text/css">
            TD.profile { background: url('http://www.example.com/fuzzy.gif') }
        </style>
    </head>
    <body>
        <div class="nav-top">
            Nifty Web Application
        </div>

        <h1>List of Users</h1>

        <div id="alerts"></div>

        <div>
            Imagine a table of user account information here.
        </div>

        <div class="nav-bottom">
            (C) 2012 NiftyWebCo, Inc.
        </div>
    </body>
</html>
 

Scrollbar