Thursday, 15 July 2010

Tapestry: Block Delegation

The following technique allows a page to inject content directly into a layout component. In our case, we're injecting metatags into the <head> element, but it could also be used for a breadcrumb trail, navigation panel, etc.

Layout.java contains this:
/*
* An odd but useful tapestry construct - note that the markup for this block is
* in the page itself - see ...
*/
@Property
@Parameter(defaultPrefix = BindingConstants.LITERAL)
private Block metatags;
Layout.tml contains this:


This leaves the page free to implement the metatags however it sees fit. For example in AtoZ.tml we have this:

<meta name="description" content="Find all of your favourite TV shows beginning with ${initial} online."/>
<meta name="keywords" content="${initial} shows, ${initial} shows watch, watch tv online"/>
Just remember you need to define the 'p' namespace as being xmlns:p="tapestry:parameter".

No comments: