Velocity 中的 Rails 风格模板助手
我需要将许多非常复杂的动态 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 个不同的助手,并且我需要非程序员网页设计师能够随意将它们换入和换出。
所以我问,
- 我可以用 Velocity 做这样的事情吗?
- 如果没有,我可以使用其他模板引擎做类似的事情吗?
- 有更好的方法来做我想做的事情吗?
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,
- Can I do something like this with Velocity?
- If not, can I do something like this with some other template engine?
- Is there a better way to do what I'm trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否已经考虑(或者正在使用)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?
抱歉,我没有动脑子就提交了这个。您所要做的就是创建一个 Helper 类:
然后将一个实例添加到上下文中:
然后在模板中:
最后, 与狗一起跳梅伦格舞以示庆祝。
Sorry, I submitted this without using my brain. All you gotta do is create a Helper class:
Then add an instance to the context:
Then in the template:
Finally, dance the merengue with a dog in celebration.