在 Tapestry 5 中重用页面块

发布于 2024-09-15 19:07:22 字数 61 浏览 0 评论 0原文

如何在页面之间重复使用 TML 标记块?我想将重复代码重构为组件,类似于标记文件或 jsp include。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヤ经典坏疍 2024-09-22 19:07:22

要创建 Tapestry 组件,您需要在 Tapestry 应用程序的组件包中创建一个组件类和(通常)一个 .tml 文件。

在博客应用程序中呈现单个帖子的示例组件类:

package my.tapestry.basepackage.components;

...

public class Post {

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

    public BlogPost getPost() {
        return post;
    }

}

相应的 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>

然后您可以在任何页面中使用您的组件,就像使用 Tapestry 的内置组件一样:

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

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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文