# Customizing erb's recognized tags

**URL:** <https://www.ruby-forum.com/t/customizing-erbs-recognized-tags/49918>\
**Category:** Ruby\
**Created:** [December 10, 2005, 2:47am UTC](https://www.ruby-forum.com/t/customizing-erbs-recognized-tags/49918 "2005-12-10T02:47:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [December 10, 2005, 2:47am UTC](https://www.ruby-forum.com/t/customizing-erbs-recognized-tags/49918/1 "2005-12-10T02:47:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [December 10, 2005, 3:15am UTC](https://www.ruby-forum.com/t/customizing-erbs-recognized-tags/49918/2 "2005-12-10T03:15:53Z")

</div>

On Dec 9, 2005, at 7:46 PM, Larry W. 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 G. II

---

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [December 10, 2005, 3:36am UTC](https://www.ruby-forum.com/t/customizing-erbs-recognized-tags/49918/3 "2005-12-10T03:36:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [December 10, 2005, 4:00am UTC](https://www.ruby-forum.com/t/customizing-erbs-recognized-tags/49918/4 "2005-12-10T04:00:48Z")

</div>

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

 \<% for column in #{table\_name}.display\_columns %\> \<%= column.name %\> \<% end %\>

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.

---

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [December 10, 2005, 4:09am UTC](https://www.ruby-forum.com/t/customizing-erbs-recognized-tags/49918/5 "2005-12-10T04:09:07Z")

</div>

Larry W. 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](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - Ruby Code & Style: Writers wanted  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools

---

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [December 10, 2005, 4:17am UTC](https://www.ruby-forum.com/t/customizing-erbs-recognized-tags/49918/6 "2005-12-10T04:17:23Z")

</div>

On Dec 9, 2005, at 8:58 PM, Larry W. wrote:

> \<% end %\>
> 
> 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’

 \<% for column in #{table\_name}.display\_columns %\> \<%= column.name %\> \<% end %\> END =\> "\n \<% for column in \#{table\_name}.display\_columns %\>\n \<%= column.name %\>\n \<% end %\>\n\n" \>\> table = Object.new =\> # \>\> class \<\> 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) =\> "\n \<% for column in table.display\_columns %\>\n \<%= column.name %\>\n \<% end %\>\n\n" \>\> require "erb" =\> true \>\> ERB.new(template).result(binding) =\> "\n \n col\_one\n \n col\_two\n \n col\_three\n \n\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 G. II

---

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [December 10, 2005, 4:21am UTC](https://www.ruby-forum.com/t/customizing-erbs-recognized-tags/49918/7 "2005-12-10T04:21:27Z")

</div>

that might be a good solution. i’ll give it a try. thanks.

---

<div class="post-metadata">

**Author:** ![ljw1001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@ljw1001](https://www.ruby-forum.com/u/ljw1001)\
**Post date:** [December 10, 2005, 3:44am UTC](https://www.ruby-forum.com/t/customizing-erbs-recognized-tags/49918/8 "2005-12-10T03:44:29Z")

</div>

On Dec 9, 2005, at 8:33 PM, Larry W. 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 G. II
