证明纯文本列式布局合理性的最简洁方法?
在纯文本 .erb 模板中,您建议我使用什么方法来生成最可维护/可读的代码,如下所示:
ITEM DESCRIPTION QTY PRICE
Product Name One 1 $10.00
Another Product With a Longer Name 2 $5.00
Yet Another Item 1 $30.00
其中每一行(显然)都是可变的,基于已购买的商品。
我可以在辅助方法中计算所需的空白,但这是否已经通过更优雅的解决方案解决了?
In a plain-text .erb template, what method would you suggest I use to produce the most maintainable/readable code for something like this:
ITEM DESCRIPTION QTY PRICE
Product Name One 1 $10.00
Another Product With a Longer Name 2 $5.00
Yet Another Item 1 $30.00
Where each of those rows is (obviously) variable, based on the items that have been purchased.
I could calculate the needed whitespace in a helper method, but is this already a solved problem with a more elegant solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来实际上可以将格式属性传递给 ERB 模板来执行此操作。我最近在朗姆酒的拉取请求中使用了这个技巧。
我的示例如下:
首先,erb 模板如下所示:
这输出的数据如下所示:
Looks like one can actually pass a format attribute to the ERB template to do this. I used this trick recently in a pull request for rumm.
My example follows:
Fist the erb template looks like the following:
This outputs data that looks like this:
听起来您需要一个纯文本表格生成器。我唯一能想到的 ruby 是这样的: https://github.com/visionmedia/terminal-table 但如果这个不能满足您的需求,可能还有其他的。
Sounds like you need a plain text table generator. The only one I can think of for ruby is this: https://github.com/visionmedia/terminal-table but there may be others if this one doesn't fit your needs.
我最终只使用
printf()
和填充格式选项。对于这种情况,终端表看起来有点过于复杂,但对于更复杂的需求(例如显示表格结果集)很有用。I ended up just using
printf()
with padding format options. The terminal table thing looked a bit over-complex for this situation, but useful for more complex requirements, such as displaying tabulated result sets.