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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

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

David Gautier
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/37ed0eae-fe55-4937-b60a-dd901e7dfeaa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Karthikeyan A K
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

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

David Gautier <[hidden email]> 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-----

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/IBq2URNPcvBcBn6yJ6UYXT2PVRvHjN7KzPc77cfP2296%40mailpile.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Hassan Schroeder-2
In reply to this post by David Gautier
On Tue, Jun 26, 2018 at 9:03 AM, David Gautier <[hidden email]> 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 ------------------------ [hidden email]
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yDaeycea9nR1PfvMR%2BwRz4T6aT2h2eL5p8QZfeGtHkzaA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

David Gautier-2
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"


Le mardi 26 juin 2018 21:44:09 UTC+2, Hassan Schroeder a écrit :
On Tue, Jun 26, 2018 at 9:03 AM, David Gautier <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="upC62N4yCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">david....@...> 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 ------------------------ <a href="javascript:" target="_blank" gdf-obfuscated-mailto="upC62N4yCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">hassan.s...@...
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0fbe706e-d8c5-474f-958d-dcd3b3938e2e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Frederick Cheung-2
In reply to this post by David Gautier

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5b87fb71-eefa-4eb0-b2ef-f87e99aca3ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Hassan Schroeder-2
In reply to this post by David Gautier-2
On Wed, Jun 27, 2018 at 3:10 AM, David Gautier <[hidden email]> 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...

--
Hassan Schroeder ------------------------ [hidden email]
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yAM_R8EJ4BSnWn%2B4MDMZw5fvMjuVzqT9smoryUMEJessw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Karthikeyan A K
-----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 <[hidden email]> wrote:

> On Wed, Jun 27, 2018 at 3:10 AM, David Gautier
> <[hidden email]> 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-----

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/vJkjQLQbHhCaGQzwLk3gpBBbmwIzvmfYcJWPDDva2297%40mailpile.
For more options, visit https://groups.google.com/d/optout.