Forum: Ruby Customizing erb's recognized tags

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 ljw1001 (Guest)
on 2005-12-10 02:47
(Received via mailing list)
Hi

Does anyone know if it's possible to change the tags that erb
recognizes so that for instance, I can have it replace on #{} instead
of <%= %>  ?

If so, could you point me an example of how it's done?

thanks.

thanks
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-10 03:15
(Received via mailing list)
On Dec 9, 2005, at 7:46 PM, Larry White wrote:

> Hi
>
> Does anyone know if it's possible to change the tags that erb
> recognizes so that for instance, I can have it replace on #{} instead
> of <%= %>  ?

You mean normal String interpolation?

 >> str = <<'END'
I have a #{2 - 1} line string here!
END
=> "I have a \#{2 - 1} line string here!\n"
 >> eval %Q{"#{str}"}
=> "I have a 1 line string here!\n"

James Edward Gray II
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 ljw1001 (Guest)
on 2005-12-10 03:36
(Received via mailing list)
I need to read in a template file and apply replacements in two steps
- so i can't use the same tags for each pass.

I don't think string interpolation works on strings read from files
which are single quoted by default.  If there's a way around that it
might be ok.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-10 03:44
(Received via mailing list)
On Dec 9, 2005, at 8:33 PM, Larry White wrote:

> I need to read in a template file and apply replacements in two steps
> - so i can't use the same tags for each pass.
>
> I don't think string interpolation works on strings read from files
> which are single quoted by default.  If there's a way around that it
> might be ok.

I just showed how to use delayed String interpolation on any String.
My example was single quoted.

Perhaps I don't understand the question.  Can you post a trivial
example of what you want to work?

James Edward Gray II
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 ljw1001 (Guest)
on 2005-12-10 04:00
(Received via mailing list)
i want to do a series of replacements on a file in two passes - one at
'compile time', one at runtime.   For a simple example, if i'm
creating 2 copies of a dynamic web page - one for each of two tables -
i would replace references to the table at 'compile time' and display
the particular values of a record from the table at runtime.It might
look like this

<body>
  <% for column in #{table_name}.display_columns %>
    <th class="th1"><%= column.name %></th>
  <% end %>
</body>

So i want to replace #{table_name} in the first pass and the rest of
the stuff in the second pass.  Since there could be a large number of
replacements at either time, I thought a solution using erb with
different tag sets might be reasonably maintainable.

thanks for your help.
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 james_b (Guest)
on 2005-12-10 04:09
(Received via mailing list)
Larry White wrote:
> Hi
>
> Does anyone know if it's possible to change the tags that erb
> recognizes so that for instance, I can have it replace on #{} instead
> of <%= %>  ?
>
> If so, could you point me an example of how it's done?

I wrote a hack that overrides IO.read.  When IO grabs the file, it
checks the file extension;  if it's an erb template, it replaces <? ?>
(with some variants) markup with <% %> before passing along the content.

It was a Good Enough sort of thing for the circumstances.


class IO
   class <<self
     alias real_read read
   end

   def IO.read( *args )
     return IO.real_read( *args ) unless args[0].to_s =~ /\.rhtml/i
     text = IO.real_read( *args )
     s = ''
     text.each_line(  ) { |l|
       next if l =~ /<?xml/
      l.gsub!( '<?eq', '<%=')
      l.gsub!( '<? ', '<% ')
      l.gsub!( '?>', '%>')
      s << "#{l}\n"
     }
     s
   end
end


James



--

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-10 04:17
(Received via mailing list)
On Dec 9, 2005, at 8:58 PM, Larry White wrote:

>   <% end %>
> </body>
>
> So i want to replace #{table_name} in the first pass and the rest of
> the stuff in the second pass.  Since there could be a large number of
> replacements at either time, I thought a solution using erb with
> different tag sets might be reasonably maintainable.

My example pretty much works for that, with minor tweaks:

 >> template = <<'END'
<body>
   <% for column in #{table_name}.display_columns %>
     <th class="th1"><%= column.name %></th>
   <% end %>
</body>
END
=> "<body>\n  <% for column in \#{table_name}.display_columns %>\n
<th class=\"th1\"><%= column.name %></th>\n  <% end %>\n</body>\n"
 >> table = Object.new
=> #<Object:0x321188>
 >> class << table
 >>   def display_columns
 >>     names = %w{col_one col_two col_three}
 >>   end
 >> end
=> nil
 >> class String
 >>   def name; self end
 >> end
=> nil
 >> table_name = "table"
=> "table"
 >> template = eval(%Q{"#{template.gsub('"', '\\"')}"}, binding)
=> "<body>\n  <% for column in table.display_columns %>\n    <th
class=\"th1\"><%= column.name %></th>\n  <% end %>\n</body>\n"
 >> require "erb"
=> true
 >> ERB.new(template).result(binding)
=> "<body>\n  \n    <th class=\"th1\">col_one</th>\n  \n    <th class=
\"th1\">col_two</th>\n  \n    <th class=\"th1\">col_three</th>\n  \n</
body>\n"

I wouldn't do it though.  What's the harm of sticking the table name
in a variable each tim before you run the template through ERb?
Heck, run the templates like this:

ERB.new(template).result(table.instance_eval { binding })

And then just call display_columns directly.

Hope the helps.

James Edward Gray II
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 ljw1001 (Guest)
on 2005-12-10 04:21
(Received via mailing list)
that might be a good solution.  i'll give it a try. thanks.
This topic is locked and can not be replied to.