Velocity 中的 Rails 风格模板助手

发布于 2024-10-03 01:31:03 字数 812 浏览 10 评论 0原文

我需要将许多非常复杂的动态 HTML 结构添加到我的 Velocity 模板中。如果我尝试直接在模板中编写这些内容,那绝对会一团糟。

让我解释一下我想要做什么...比方说,在我的网站上,用户可以发表评论。我希望能够在我的模板中执行此操作,而不是编写进入评论表和发布表单的所有复杂的动态 HTML 结构:

<div>
    // call to a method that generates the comments table
</div>
<div>
    // call to a method that generates the comments form
</div>

在 Ruby On Rails 中,有一些名为 助手 基本上允许您从模板中进行调用,一种返回动态生成的 HTML 块的方法,将其嵌入到您调用它的模板中。助手的好处是您只需将 CPU 花费在您使用的助手上。使用 VelocityContext.put() 方法,似乎我必须提前生成我需要的所有内容。这是一个问题,因为我最终将拥有大约 50 个不同的助手,并且我需要非程序员网页设计师能够随意将它们换入和换出。

所以我问,

  1. 我可以用 Velocity 做这样的事情吗?
  2. 如果没有,我可以使用其他模板引擎做类似的事情吗?
  3. 有更好的方法来做我想做的事情吗?

I need to add a number of really complex dynamic HTML structures to my Velocity template. If I were to attempt to write these directly in the template, it would be an absolute mess.

Let me explain what I'm trying to do... let's say, on my website, a user can post comments. Rather than writing all the complex dynamic HTML structure that goes into the comments table and the posting form, I would like to be able to do this in my template:

<div>
    // call to a method that generates the comments table
</div>
<div>
    // call to a method that generates the comments form
</div>

In Ruby On Rails, there are things called helpers that allow you, from the templates, to call, basically, a method that returns a dynamically-generated chunk of HTML, to be embedded in the template right where you call it. The nice thing about helpers is that you only spend CPU on the ones you use. With the VelocityContext.put() method, it seems like I have to generate everything I need in advance. This is a problem because I will ultimately have some 50 different helpers, and I need the non-programmer web designer to be able to swap them in and out at will.

So I am asking,

  1. Can I do something like this with Velocity?
  2. If not, can I do something like this with some other template engine?
  3. Is there a better way to do what I'm trying to do?

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

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

发布评论

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

评论(2

清秋悲枫 2024-10-10 01:31:04

您是否已经考虑(或者正在使用)Velocity Tools?我倾向于将它们视为与 Rails 助手非常相似,并且绝对是保留特定于视图的逻辑的最佳位置。根据具体的实现,您可以将助手中的连接视为自定义工具,然后让工具框架无缝地管理实例化等。如果您的助手只是一个 POJO,您可能会发现它已经可以作为工具使用,无需任何更改。

或者,如果您的助手仅构建 HTML 结构并且没有任何复杂的 Java 依赖项,也许您可​​以将它们创建为

Did you consider (or are you using) Velocity Tools already? I tend to see them as pretty analogous to Rails helpers, and definitely the best place to keep view-specific logic. Depending on the exact implementation you could look at wiring in your helper as a custom tool, and then let the Tools framework seamlessly manage the instantiation etc. If your helper is just a POJO you may find it already works as a tool without any changes.

Alternatively, if your helpers are only building HTML structure and don't have any complex Java dependencies, maybe you could just create them as macros?

一身软味 2024-10-10 01:31:04

抱歉,我没有动脑子就提交了这个。您所要做的就是创建一个 Helper 类:

public class HtmlHelper {

    public String getSomeHtml() {
        return "<p>badgers</p>";
    }

}

然后将一个实例添加到上下文中:

VelocityContext context = new VelocityContext();
HtmlHelper helper = new HtmlHelper();
context.put("helper", helper);

然后在模板中:

<div>
    $helper.getSomeHtml()
</div>

最后, 与狗一起跳梅伦格舞以示庆祝。

Sorry, I submitted this without using my brain. All you gotta do is create a Helper class:

public class HtmlHelper {

    public String getSomeHtml() {
        return "<p>badgers</p>";
    }

}

Then add an instance to the context:

VelocityContext context = new VelocityContext();
HtmlHelper helper = new HtmlHelper();
context.put("helper", helper);

Then in the template:

<div>
    $helper.getSomeHtml()
</div>

Finally, dance the merengue with a dog in celebration.

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