At the start of the chapter, we saw an example of something that we might write a template for: a purchase ledger for a fictional bar:
| The Bistro Illegal |
| Sector RT 74 |
| Lazgar Beta |
| |
| Purchase ledger, October 2014 |
| |
| Generated: 4 October 2014, 12:49pm |
| |
| Product Units Price |
| |
| Ol' Janx Spirit 282 $27,918.00 |
| Eau de Santraginus V 300 $600.00 |
| Arcturan Mega-Gin 150 $7,500.00 |
| Fallian Marsh Gas 1,000 $2,000.00 |
| Qalactin Hypermint Extract 25 $500.00 |
| Algolian Suntiger, teeth of 300 $30,000.00 |
| Zamphuor 3 $120.00 |
| Olives 200 $24.00 |
| |
| Total $68,662.00 |
Let’s write an example ERB template for this type of file. We won’t worry about where the data is coming from just yet; we’ll write our idealized template and then investigate how to pass data into it afterward.
First, let’s deal with the header. We have some static content—the address of the bar—followed by two dates, which should be dynamic:
erb/purchase-ledger.erb | |
| The Bistro Illegal |
| Sector RT 74 |
| Lazgar Beta |
| |
| Purchase ledger, <%= Time.now.strftime("%B %Y") %> |
| |
| Generated: <%= Time.now.strftime("%-d %B %Y, %l:%M%P") %> |
Just as we saw earlier, we dynamically output the date, using the strftime method to format it differently in the two places that we use it.
Next, we generate the line items within the purchase ledger. Let’s assume that we’ve been passed these in a variable called @line_items:
erb/purchase-ledger.erb | |
| Product Units Price |
| |
| <% @line_items.each do |item| %> |
| <%= "%-40s%5d%15.2f
" % [item.name, item.units, item.price / 100] %> |
| <% end %> |
Hopefully the only somewhat cryptic part here is the format string, which aligns the values so that the output resembles a table. (We looked at these formatting strings in Formatting Output with printf, if you need a refresher.) Here we specify a left-aligned, 40-character string for the product name; followed by a right-aligned, 5-character integer for the quantity; and then finally a right-aligned, 15-character floating-point number rounded to two decimal places for our price. (We’re dividing by 100 here because we’re assuming prices will be passed in as cents, rather than dollars.) This will ensure that, unless our values overspill these generous allocations, everything will line up nicely in the final report.
Finally, we output the total value of all the items. In keeping with the theme of separating business logic from presentation logic, we’ll assume that this is being calculated elsewhere and passed in as another variable, this time called @total:
erb/purchase-ledger.erb | |
| <%= "Total%55.2f" % @total / 100 %> |
Hopefully this example illustrates not only the power of ERB, but also its simplicity. We haven’t had to learn new syntax for a new language: the logic we’ve written is Ruby, the same as we’d write in an ordinary Ruby script. We use each to loop over the items in the @line_items collection. We access properties on the line item to output information about it.
The key advantage, though, is that we’ve successfully separated our presentation logic from our business logic. If we suddenly needed to generate this same report in HTML format, to publish on the web, we could do so easily: it would just mean creating a new template, without touching the logic at all.
3.139.109.92