证明纯文本列式布局合理性的最简洁方法?

发布于 2024-11-19 03:26:06 字数 456 浏览 2 评论 0原文

在纯文本 .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 技术交流群。

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

发布评论

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

评论(3

春花秋月 2024-11-26 03:26:06

看起来实际上可以将格式属性传递给 ERB 模板来执行此操作。我最近在朗姆酒的拉取请求中使用了这个技巧。

我的示例如下:

首先,erb 模板如下所示:

 ID                Name
 ==                ====
<% this.each do |flavor| %>
  <%= '%-17s %-7s' % [flavor.id, flavor.name] %> 
<% end %>

这输出的数据如下所示:

ID                Name
==                ====
2                 512MB Standard Instance
3                 1GB Standard Instance
4                 2GB Standard Instance
5                 4GB Standard Instance
6                 8GB Standard Instance
7                 15GB Standard Instance
8                 30GB Standard Instance
performance1-1    1 GB Performance
performance1-2    2 GB Performance
performance1-4    4 GB Performance
performance1-8    8 GB Performance
performance2-120  120 GB Performance
performance2-15   15 GB Performance
performance2-30   30 GB Performance
performance2-60   60 GB Performance
performance2-90   90 GB Performance

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:

 ID                Name
 ==                ====
<% this.each do |flavor| %>
  <%= '%-17s %-7s' % [flavor.id, flavor.name] %> 
<% end %>

This outputs data that looks like this:

ID                Name
==                ====
2                 512MB Standard Instance
3                 1GB Standard Instance
4                 2GB Standard Instance
5                 4GB Standard Instance
6                 8GB Standard Instance
7                 15GB Standard Instance
8                 30GB Standard Instance
performance1-1    1 GB Performance
performance1-2    2 GB Performance
performance1-4    4 GB Performance
performance1-8    8 GB Performance
performance2-120  120 GB Performance
performance2-15   15 GB Performance
performance2-30   30 GB Performance
performance2-60   60 GB Performance
performance2-90   90 GB Performance
愿与i 2024-11-26 03:26:06

听起来您需要一个纯文本表格生成器。我唯一能想到的 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.

夜清冷一曲。 2024-11-26 03:26:06

我最终只使用 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.

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