Is it a good idea to avoid ERB usage, when it seems useless.

47 views
Skip to first unread message

David Gautier

unread,
Jun 26, 2018, 1:06:37 PM6/26/18
to Ruby on Rails: Talk
I like ruby and I don't really link ERB templates ...

Why ?

Because when you write ruby:

1 - It's hard to generate malformed HTML (or others output languages) because you use methods that generate the code for you.
With ERB: template.html.erb
<div class="key-value">
<span class="key"><%= key %></span>
<span class="value"><%= value %></span>
</div>
Here I can forget to close the <div> and generate malformed HTML.

With pure ruby and ActionView methods:
<%=
content_tag(:div, class: "key-value"){
concat content_tag(:span, class: "key"){ key }
concat content_tag(:span, class: "value"){ value }
}
%>
Here I cannot do this mistake.

2 - Rubocop can check the code to make it more tidy. (https://github.com/rubocop-hq/rubocop)
Perhaps I am wrong but it seems that Rubocop does not support ERB.

3 - It is ruby so code coverage should work on it (code coverage does not work on Rails views but it is another big issue, for me).

4 - The code can be easily moved to helper or presenter (copy / past).

How ?

If you want to use it, by default, you have to name your views with suffix .ruby
Then, by adding an initializer like  config/initializers/ruby_views.rb
ActionView::Template.register_template_handler(:rb, :source.to_proc)

You can name them with suffix .rb and Rubocop will automatically check them :)

Using this the code above can be write as : template.html.rb
content_tag(:div, class: "key-value"){
concat content_tag(:span, class: "key"){ key }
concat content_tag(:span, class: "value"){ value }
}


Questions

Why do we (Rails coders) write views as templates using ERB ... ?
Is it a go idea to write views using pure ruby ?

Best regards,

David

Karthikeyan A K

unread,
Jun 26, 2018, 1:11:16 PM6/26/18
to rubyonra...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Why not try http://slim-lang.com/

David Gautier <david....@kriom.net> wrote:
>
> I like ruby and I don't really link ERB templates ...
>
> *Why ?*
>
> Because when you write ruby:
>
> 1 - It's hard to generate malformed HTML (or others output
> languages) because you use methods that generate the code for
> you. With *ERB*: template.html*.erb*
>
> <div class="key-value">
> <span class="key"><%= key %></span>
> <span class="value"><%= value %></span>
> </div>
>
> Here I can forget to close the <div> and generate malformed
> HTML.
>
> With pure ruby and ActionView methods:
>
> <%=
> content_tag(:div, class: "key-value"){
> concat content_tag(:span, class: "key"){ key }
> concat content_tag(:span, class: "value"){ value }
> }
> %>
>
> Here I cannot do this mistake.
>
> 2 - Rubocop can check the code to make it more tidy.
> (https://github.com/rubocop-hq/rubocop)
> Perhaps I am wrong but it seems that Rubocop does not support
> ERB.
>
> 3 - It is ruby so code coverage should work on it (code
> coverage does not work on Rails views but it is another big
> issue, for me).
>
> 4 - The code can be easily moved to helper or presenter (copy /
> past).
>
> *How ?*
>
> If you want to use it, by default, you have to name your views
> with suffix *.ruby* Then, by adding an initializer like
> config/initializers/ruby_views.rb
>
> ActionView::Template.register_template_handler(:rb,
> :source.to_proc)
>
>
> You can name them with suffix *.rb* and Rubocop will
> automatically check them :)
>
> Using this the code above can be write as : template.html*.rb*
>
> content_tag(:div, class: "key-value"){
> concat content_tag(:span, class: "key"){ key }
> concat content_tag(:span, class: "value"){ value }
> }
>
>
>
> *Questions*
>
> Why do we (Rails coders) write views as templates using ERB ...
> ? Is it a go idea to write views using pure ruby ?
>
> Best regards,
>
> David
>

- --
- --

- - Karthikeyan A K

Sent using Mailpile, Free Software from www.mailpile.is

-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEE19aIrDNxkBJVboERznHPBBVO1PMFAlsyc40ACgkQznHPBBVO
1PPz1RAAkk1XYv111so2/LtnuGAVBxAYAcXtOFOnJ/LbaPfQuJf0XJKmO3QdWJLM
ulN2+WyKH3x2Ly/vJKzkBsxOtaA5F7WvCvaHOvifYAb/JNkf7qvYHtjZeAvh9l0a
KVCI0vyhENNVnpVoghS7iQP9bw3boDqczJkaOzrrYkBfsGLdKJmnlhGaT4Ucd/pS
EVThYB7M8NuT+nJ+kAJOmI0PNVoE+oDD/V/hh/vAjsiKKsGNg8de760vrPJxhZ0N
Q/0ZVilOS0PxywBCzmuTnqxEOIyKym47FKVdfXKrVar9qhGHVfp1gVwhJgVyCJEY
XiB+xdkBAwi25c5qDPXiNt/sD6cV8VQjET3TXKyq+t3MWNzUh71U76ZQzKhM7QEd
FD+7WjtdbHcaPRhjc0jGBdOhWYvCzt8UObU/YO+BKPR4w96QZphXv40aM28os/fu
MJ7cvQoYOpLuRmNIG+DT/C59Pa1qW5Cjz69LKIwKffPYnI37JT/lEqsWD8PFpBLR
0yWClgWZ7mUXVmLeL8b7n25t8cVYURw3E8Mm0k2GprVduFf6wg6FWMhJMuNt5MaR
9HYd0gBjKkawvUtmWFu8I3rQKmiaJbFqcK+hjFFFTzacnfLT+PN73HHg7JvMngCV
b1PQLt6i0HweVd0FVvsD04mFKLnNHsXWWnAbeO6ulZVCwDweTPc=
=NksA
-----END PGP SIGNATURE-----

Hassan Schroeder

unread,
Jun 26, 2018, 3:44:09 PM6/26/18
to rubyonrails-talk
On Tue, Jun 26, 2018 at 9:03 AM, David Gautier <david....@kriom.net> wrote:

> Why do we (Rails coders) write views as templates using ERB ... ?

If I *have* to do front end work it's a lot easier to eyeball differences
between the ERB sources and `view source` in a browser.

And if views are ERB it's possible to delegate that work to front-end
folks who only do HTML/CSS/JS stuff.

> Is it a go idea to write views using pure ruby ?

It doesn't sound very maintainable to me, but that doesn't mean
there aren't use cases for it. If it works for you... ¯\_(ツ)_/¯

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

David Gautier

unread,
Jun 27, 2018, 12:33:58 PM6/27/18
to Ruby on Rails: Talk
Karthikeyan A K

Thanks for answer.

Because :
- I don't want to add another language to the application,
- Code coverage will not work on it,
- Rubocop will not work on it,
- And, possibly, I want to write Ruby :P

Hassan Schroeder

Thanks for your answer too.

I am a full-stack developer, that can explain why I think ERB templates are useless.

If I'm asking myself, "for who do we write ERB templates with HTML? Who will read the code ?"
I can answer "me ... the team ...So it does not matter, we understand Ruby"

Frederick Cheung

unread,
Jun 27, 2018, 12:42:16 PM6/27/18
to Ruby on Rails: Talk

On Tuesday, June 26, 2018 at 6:06:37 PM UTC+1, David Gautier wrote:
I like ruby and I don't really link ERB templates ...


Why do we (Rails coders) write views as templates using ERB ... ?
Is it a go idea to write views using pure ruby ?



I'm not a massive fan of ERB, however it's not a bad default. Front end developers that are not rubyists can mostly understand it (even if they don't understand the ruby bits they can still work on the markup). I'm also guaranteed that anyone I bring on to the team will understand it.

I don't think of rubocop, code coverage as issues, because I would usually not have enough ruby in the view to make it worthwhile.

Personally I like haml - I find

content_tag(:div, class: "key-value"){ 
   concat content_tag(:span, class: "key"){ key } 
   concat content_tag(:span, class: "value"){ value } 
}

has a lot of noise compared to

.key-value
  %span.key
    key
  %span.value
    value


Fred

Hassan Schroeder

unread,
Jun 27, 2018, 12:59:08 PM6/27/18
to rubyonrails-talk
On Wed, Jun 27, 2018 at 3:10 AM, David Gautier <kri...@gmail.com> wrote:

> I am a full-stack developer, that can explain why I think ERB templates are
> useless.

So am I, so that doesn't really "explain" it :-)

Also, given the ubiquity of templating systems in basically every
language used for web dev, it seems like there are more use cases
than not...

> If I'm asking myself, "for who do we write ERB templates with HTML? Who will
> read the code ?"
> I can answer "me ... the team ...So it does not matter, we understand Ruby"

As always, TMTOWTDI...

Karthikeyan A K

unread,
Jun 28, 2018, 1:46:50 AM6/28/18
to rubyonra...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

This could be off topic, but there was this excellent thing
called Ferro https://easydatawarehousing.github.io/ferro/ , now
its not maintained I think.

Hassan Schroeder <hassan.s...@gmail.com> wrote:
> On Wed, Jun 27, 2018 at 3:10 AM, David Gautier
> <kri...@gmail.com> wrote:
>
> > I am a full-stack developer, that can explain why I think ERB templates are
> > useless.
>
> So am I, so that doesn't really "explain" it :-)
>
> Also, given the ubiquity of templating systems in basically
> every language used for web dev, it seems like there are more
> use cases than not...
>
> > If I'm asking myself, "for who do we write ERB templates with HTML? Who will
> > read the code ?"
> > I can answer "me ... the team ...So it does not matter, we understand Ruby"
>
> As always, TMTOWTDI...
>

- --
- - Karthikeyan A K

Sent using Mailpile, Free Software from www.mailpile.is

-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEE19aIrDNxkBJVboERznHPBBVO1PMFAls0de0ACgkQznHPBBVO
1PP9mA//QdCsKhoUsFK9Vh3z5YcCEdZzqNpZoBpCB1uJz9V51eU4CNVxQg3i2UO3
rhlVRbBuKmU+RmVIrSUULpyBxFdA04N8MgxFnkfzJ8iCZiwzolzmXTam3fL/1NbN
yYVuiSmNr12lYXFHd2xvaE008QRAiFjFIl1plGd09FwAVMkwnim5DbeSWYZMvc8P
HCO8L1kO+x94TDWM75FJmTdtGUk1bfZeOs6rPLsmtTkEWSB8wxFLwn9eFTyhy7Hy
2xQEDt6ZZG34JMtZkDsBQo5YpiwMYDvHfVenO8GDkbzekldUViXTUR1E+5V00KFY
bvhEost5dBnSIDTfgSdwWD+nCs4FMPWaOWa3O3Ic6JvP6vUeThsVD7gHGMkykiJo
mhjclqEUnebfRgOTarBGtKGGIuKJOOuDddgxySkNfyULFYc0+fgk4Y8gRgCyP8Cm
xfoM1cgO1M9LXs6DaffRL7TO1aBSUH0ZQG/OY3of1fOmEoN3SC1B12l2KCZDvx4o
4w3TLSluZdMWewX4Pf7djJsTZPlyNBZ9cXQT+2znCYT86dPt9jqACcZKmnAzcPbd
ivOJFvbNCaqVe1uf6c/a8jFD+zoEYjrblgMMYTirb0VXzMAj8BqtRVW1EZ7eqN1f
IpA5K1XBdaRIUyUsk1A6MDU24hmphgFFU0mZFdRBuN5tutFlkyw=
=9FoE
-----END PGP SIGNATURE-----
Reply all
Reply to author
Forward
0 new messages