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

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

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 :slight_smile:

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

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

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... ¯\_(ツ)_/¯

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 :stuck_out_tongue:

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”

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

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 :slight_smile:

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...

This could be off topic, but there was this excellent thing called Ferro Ferro - No more Html with Opal-Ferro , now its not maintained I think.