Hello friends!

Alas, there are no normal docks on ERB, pages scattered on an Internet do not answer questions normally. There either the rails are either slag. Here I have a question. I will give you a list of commands that I use in the Template Toolkit. And if you can re-see me on the ERB.

[% INCLUDE paging.tmpl %] - подключение внешнего шаблона [% FOREACH key IN hash.keys %] - Мы итерируем хеш, и потом используем его [% key %] is [% hash.$key %] данные где надо [% END %] [% data.format(myvarwithdate,'%d.%m.%Y') %] - а вот так слабо? [% FOREACH i IN top_menu %] [% i.data %] [% FOREACH i.child %] [% data %] [% END %] [% END %] 

Closed due to the fact that the essence of the issue is incomprehensible by the participants Vladimir Gamalyan , Dmitriy Simushev , vp_arth , rjhdby , ermak0ff Mar 9 '17 at 5:56 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • [% data.format(myvarwithdate,'%d.%m.%Y') - а вот так слабо? - what, straight without ] ? Oh-wei, yes, probably weak. - D-side
  • yes, probably weakly, if instead of answering a man got to the bottom of the bracket. Oh well. - Denis Antonov
  • one
    I am already writing the answer, but you will translate the template yourself. I will only describe the ERB syntax. - D-side

1 answer 1

ERB documentation is really so-so, but it is simple in itself.

Much better documentation from Erubis , another language from the same family, but it is more general, supports several languages ​​and syntax sugar in it a little more. There is something to document at least.


But back to the ERB. He is dumb as a cork. He takes a pattern and collects Ruby code from it, using a few simple rules.

  • <% инструкция %> - just executes the instruction, ignores the return value, and indeed the instruction is not obliged to have it, for example, <% end %> does not have it: this is how the expression started earlier ends.
    • The generated code is output "as is", only supplied ; in the end. Therefore, let's say, <% 2 + %><% 2 %> of 2 +; 2 will be released 2 +; 2 2 +; 2 , syntax error. But sometimes in the middle of expression ; permissible, for example, in calling a method with a block, this allows you to separate the body of the block: <% (1..5).each do |i| %><% puts i %><% puts '---' %><% end %> <% (1..5).each do |i| %><% puts i %><% puts '---' %><% end %>
  • <%= выражение %> - calculates the value of the expression and outputs the result to the document.

    • выражение must be independent. <%= 42.tap do %><% end %> will fall.
    • The generated code is output as "writing to the template output stream":

       _erbout.concat((выражение).to_s) 
  • <%# комментарий %> - ignores what should be done with the comment.

    • The generated code is not displayed at all.
    • Despite the fact that in Ruby comments also begin with # , this is not the same as <% # комментарий %> , it is better not to use this record, since it forms Ruby comment in the generated code until the very end of the line. Therefore, this: <% # foo %><%= 42 > generate the code:

       # hi ; _erbout.concat \" 42 >\"; 

      Pol-template has become a comment and does not display anything, yy-oops!
      <%# foo %><%= 42 > but it works correctly.

For cases when ERB needs to generate code on ERB (a kind of meta-ERB), <%% taken as an indication to output <% , which allows you to screen all kinds of tags for output, and even (by chance, by chance) something else:

  <%% %> -> <% %> <%%= %> -> <%= %> <%%# %> -> <%# %> <%% -> <% <%%> -> <%> при этом <% %> -> ~пустота~ 

Everything. There is nothing else in the ERB. All the other features in it are already implemented in Ruby: the inclusion of other patterns, hashmap traversals , arrays and much more , the calculation of complex expressions.

  • Well, a very comprehensive answer. Thank. I will try to work with him. It’s a pity, it’s not clear that instead of ERB it would be better to connect another template, and which one is better, I don’t know. And it’s a pity that you can’t connect the native Template Toolkit to it :) for so many years used to it. - Denis Antonov
  • And why the banalism introduces him into a stupor. minimal reproducible example <% @ data.each do | header, link | %> <li> <% = header%> </ li> <li> <% = link </ li> <% end%> minimum reproducible example ` eval': (erb):8: unterminated regexp meets end of file (SyntaxError) (erb):8: syntax error, unexpected end-of-input, expecting ')' <% end ).to_s); _erbout.force_encoding(__ENCODING__) ^ from /usr/lib/ruby/2.3.0/erb.rb:864:in eval': (erb):8: unterminated regexp meets end of file (SyntaxError) (erb):8: syntax error, unexpected end-of-input, expecting ')' <% end ).to_s); _erbout.force_encoding(__ENCODING__) ^ from /usr/lib/ruby/2.3.0/erb.rb:864:in result 'from news.rb: 66: in <main>' Dumb with comment formatting. - Denis Antonov
  • @DenisAntonov ERB can be used in anything. They generate HTML, YAML, even Ruby. If you do not have such requirements for output formats, you can take something more specialized: HAML, Slim, Rabl, arbre, JBuilder, AMS (although it’s not really a template system ...) or something else, Ruby is full of them in the world. - D-side
  • @DenisAntonov because you again, along the way, forgot the bracket. Only angular this time, with a percentile. See <%= link ? - D-side
  • Well, one day I'll sit down by the rails. And there I will have to use ERB. So it is necessary, however, to endure and get used. - Denis Antonov