2011-07-08 24 views
3

在純文本.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 

若該等行的是(顯然)變量,基於關於已經購買的物品。

我可以在輔助方法中計算所需的空白,但這是一個更優雅的解決方案已解決的問題嗎?

回答

0

我結束了剛剛使用printf()與填充格式選項。對於這種情況,終端表事情看起來有點過於複雜,但對於更復雜的需求(如顯示列表結果集)非常有用。

1

看起來實際上可以將格式屬性傳遞給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 
相關問題