Small ERB Scriptlet Shorthand Example?


This example is from the documentation for ERB (with a small error corrected: added argument binding to line 36):

$ cat -n t.rb
     1  require "erb"
     2
     3  # Create template.
     4  template = %q{
     5    From:  James Edward Gray II 
     6    To:  <%= to %>
     7    Subject:  Addressing Needs
     8
     9    <%= to[/\w+/] %>:
    10
    11    Just wanted to send a quick note assuring that your needs are being
    12    addressed.
    13
    14    I want you to know that my team will keep working on the issues,
    15    especially:
    16
    17    <%# ignore numerous minor requests -- focus on priorities %>
    18    % priorities.each do |priority|
    19      * <%= priority %>
    20    % end
    21
    22    Thanks for your patience.
    23
    24    James Edward Gray II
    25  }.gsub(/^  /, '')
    26
    27  message = ERB.new(template, trim_mode: "%<>")
    28
    29  # Set up template data.
    30  to = "Community Spokesman "
    31  priorities = [ "Run Ruby Quiz",
    32                 "Document Modules",
    33                 "Answer Questions on Ruby Talk" ]
    34
    35  # Produce result.
    36  email = message.result(binding)
    37  puts email
Enter fullscreen mode

Exit fullscreen mode

The embedded scriptlet at lines 16-18 uses a shorthand notation (and works!):

    18    % priorities.each do |priority|
    19      * <%= priority %>
    20    % end
Enter fullscreen mode

Exit fullscreen mode

Its longhand form would be:

    18    <% priorities.each do |priority| %>
    19      * <%= priority %>
    20    <% end %>
Enter fullscreen mode

Exit fullscreen mode

My problem: I have been unable to craft a small shorthand example
that actually works.

Any help appreciated!



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *