A B
2013-May-24 12:53 UTC
How to get rid of blank spaces in templates rendered by Rails 3.2.13 ERB/Erubis?
Whatever I do I get a response page rendered like this: <https://lh3.googleusercontent.com/-G9KpqL6QGKE/UZ9afeLuFpI/AAAAAAAAABk/r5rITlX-kzc/s1600/rails_render.png> Every page is rendered with blank spaces. Even if I put HTML and ERB tags together inline: * * * * *## Not together inline, blank spaces are like on the picture above:* *<div>* *<%= @foo %>* *</div>* *## Together inline, see the result lower:* *<div><%= @foo %></div>* *## Result from t**ogether inline. **Blank spaces exists either but now look like this:* *<div class="foo">* *""* *## see the difference? Two double quotes together, not with a space in-between. But anyway it is rendered!* *</div> * What I''ve learned so far: The text from Rails guide<http://edgeguides.rubyonrails.org/configuring.html#configuring-action-view> : *config.action_view.erb_trim_mode* gives the trim mode to be used by ERB.> It defaults to ''-''. See the ERB documentation for more information.But at the same time they say<http://www.kuwata-lab.com/erubis/users-guide.05.html>that the Erubis is the default template engine in Rails, not ERB: NOTICE: Rails 3 adopts Erubis as default default engine. You don''t need to> do anything at all when using Rails 3. This section is for Rails 2.And here is a code from * gems\actionpack-3.2.13\lib\action_view\template\handlers\erb.rb:* # Specify trim mode for the ERB compiler. Defaults to ''-''. # See ERB documentation for suitable values. class_attribute :erb_trim_mode self.erb_trim_mode = ''-'' ### yes, trim mode is set by dafault. But it is nothing for Erubis. # Default implementation used. class_attribute :erb_implementation self.erb_implementation = Erubis ### yes, Erubis is instead of ERB self.class.erb_implementation.new( erb, :escape => (self.class.escape_whitelist.include? template.mime_type), :trim => (self.class.erb_trim_mode == "-") ### so :trim is true, just because self.erb_trim_mode = ''-''. That''s all. ).src` From official Erubis docs: Erubis deletes spaces around ''<% %>'' automatically, while it leaves spaces> around ''<%= %>''. > If you want leave spaces around ''<% %>'', add command-line property > ''--trim=false''. > Or add option :trim=>false to Erubis::Eruby.new().So it is obvious that :trim => (self.class.erb_trim_mode == "-") will never remove spaces around <%= %>. Because there is no option in Erubis for it. And this is exactly seen on the picture above. This line self.erb_implementation = Erubis informs that Rails implements Erubis instead of ERB. But as written above the only trim mode Erubis supports is around <% %> , just pass true or false only. So any experiments with true ERB options<http://ruby-doc.org/stdlib-2.0/libdoc/erb/rdoc/ERB.html#method-c-new>( *< % > -)* yields nothing. Because Erubis just doesn''t support them. My question: How to get rid of those blank spaces without switching to Slim, Haml, etc? Why Rails say in their guide *See the ERB documentation for more information * if it means nothing for default Erubis? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a7adf722-821a-460c-9a30-f4576e97e1d3%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
mike
2013-May-24 18:11 UTC
Re: How to get rid of blank spaces in templates rendered by Rails 3.2.13 ERB/Erubis?
On Friday, May 24, 2013 8:53:38 AM UTC-4, A B wrote:> > Whatever I do I get a response page rendered like this: > > > <https://lh3.googleusercontent.com/-G9KpqL6QGKE/UZ9afeLuFpI/AAAAAAAAABk/r5rITlX-kzc/s1600/rails_render.png> > Every page is rendered with blank spaces. Even if I put HTML and ERB tags > together inline: > * > * > * > * > *## Not together inline, blank spaces are like on the picture above:* > *<div>* > > *<%= @foo %>* > > *</div>* > > > *## Together inline, see the result lower:* > *<div><%= @foo %></div>* > > > *## Result from t**ogether inline. **Blank spaces exists either but now > look like this:* > *<div class="foo">* > > *""* *## see the difference? Two double quotes together, not with a > space in-between. But anyway it is rendered!* > > *</div> * > > What I''ve learned so far: > > The text from Rails guide<http://edgeguides.rubyonrails.org/configuring.html#configuring-action-view> > : > > *config.action_view.erb_trim_mode* gives the trim mode to be used by ERB. >> It defaults to ''-''. See the ERB documentation for more information. > > > > But at the same time they say<http://www.kuwata-lab.com/erubis/users-guide.05.html>that the Erubis is the default template engine in Rails, not ERB: > > NOTICE: Rails 3 adopts Erubis as default default engine. You don''t need to >> do anything at all when using Rails 3. This section is for Rails 2. > > > > And here is a code from * > gems\actionpack-3.2.13\lib\action_view\template\handlers\erb.rb:* > > # Specify trim mode for the ERB compiler. Defaults to ''-''. > # See ERB documentation for suitable values. > class_attribute :erb_trim_mode > self.erb_trim_mode = ''-'' ### yes, trim mode is set by dafault. But it is nothing for Erubis. > > # Default implementation used. > class_attribute :erb_implementation > self.erb_implementation = Erubis ### yes, Erubis is instead of ERB > > self.class.erb_implementation.new( > erb, > :escape => (self.class.escape_whitelist.include? template.mime_type), > :trim => (self.class.erb_trim_mode == "-") ### so :trim is true, just because self.erb_trim_mode = ''-''. That''s all. > > ).src` > > > From official Erubis docs: > > Erubis deletes spaces around ''<% %>'' automatically, while it leaves spaces >> around ''<%= %>''. >> If you want leave spaces around ''<% %>'', add command-line property >> ''--trim=false''. >> Or add option :trim=>false to Erubis::Eruby.new(). > > > > So it is obvious that :trim => (self.class.erb_trim_mode == "-") will > never remove spaces around <%= %>. Because there is no option in Erubis > for it. And this is exactly seen on the picture above. > > This line self.erb_implementation = Erubis informs that Rails implements > Erubis instead of ERB. > > But as written above the only trim mode Erubis supports is around <% %> , > just pass true or false only. > > So any experiments with true ERB options<http://ruby-doc.org/stdlib-2.0/libdoc/erb/rdoc/ERB.html#method-c-new>( > *< % > -)* yields nothing. Because Erubis just doesn''t support them. > > My question: How to get rid of those blank spaces without switching to > Slim, Haml, etc? > > Why Rails say in their guide *See the ERB documentation for more > information* if it means nothing for default Erubis? >I''m either reading this wrong, or something isn''t making sense. If I read this correctly, you have the following in a html.erb file: <div><%= @foo %></div> and it is rendering the following html: <div class="foo"> "" </div> If that''s correct, there are a couple of things. First, the rendering procedures would not have defined a class for the div tag, either that''s a misprint, or you must have had it in the template code. Second, the <%= ... %> does not insert any spaces or new line characters on its own, nor does it eliminate any. It evaluates the expression and returns it. For example, if @foo is a string with the value ''aaa'', the template code above would return: <div>aaa</div> In the code: <div> <%= @foo %> </div> it would render: <div> aaa </div> If I''m reading your post correctly, the value passed by @foo must include newline characters and the quotation marks. You are correct that erubis strips spaces and newline characters from <% ... %> statements and the config.action_view.erb_trim_mode no longer really does anything. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/73e0a65b-af7f-4e37-b741-32ac2a5f1c50%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.