tags:

views:

18

answers:

1

How can I re-use chunks of TML markup between pages? I want to refactor repetitive code out into a component, similar to a tag file or a jsp include.

+2  A: 

To create a Tapestry component, you create a component class and (usually) a .tml file in the components package of your Tapestry application.

An example component class that renders a single post in a blogging application:

package my.tapestry.basepackage.components;

...

public class Post {

    @Parameter(allowNull = false, required = true, 
            defaultPrefix = BindingConstants.PROP)
    private BlogPost post;

    public BlogPost getPost() {
        return post;
    }

}

The corresponding Post.tml:

<t:container xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
        xmlns:p="tapestry:parameter">
    <h2>${post.title}></h2>
    <p>
        <span t:type="ck/dateFormat" t:value="post.created" 
                t:pattern="d/M/yyyy" />
    </p>
    <div>
        ${post.text}
    </div>
</t:container>

You can then use your component in any of your pages, just like you use Tapestry's built-in components:

<div t:type="Post" t:post="post" />
Henning

related questions