Print in ERB without <%=?

前端 未结 3 1265
萌比男神i
萌比男神i 2020-12-14 00:57

Sometimes it\'s more convenient to print in <%%>. How to do it in Rails?

3条回答
  •  -上瘾入骨i
    2020-12-14 01:45

    erb has two method to evaluate inline ruby expressions. The <% which evaluates the expression and the <%= which evaluates and prints. There is no global object to print to within the binding context.

    As mentioned by Omar, there is a concat method, which is part of ActionView. This will do what you want.

    Unlike a scripting language escape, there is no default output for erb. Since erb is simply a function, and given a template and binding will return a variable, it returns the values of text and functions recursively.

    There is hot debate as to how much logic should be allowed in a view, but as little as possible is what most people aim for. If you are putting more code than text in the view, you may want to consider refactoring your code.

提交回复
热议问题