I saw this recently, thought it was interesting. But I don't really understand what it does?
Ex. I have a rails app and I want to bootstrap some json, so that I don't have to make a second request. Normally I would write something like this.
<%= raw @model.to_json %>
or <%= @model.to_json.html_safe %>
I have to send the message raw
or html_safe
or the json will be html escaped and thus not parsed correctly. However, this seems to work too.
<%== @model.to_json %>
But I can't find any documentation.
Does anyone know what this does exactly? i.e. Is it the exact same as calling html_safe
or raw
? Or is there more to it?
ERB (or Ruby code generated by ERB) returns a string in the same character encoding as the input string. When the input string has a magic comment, however, it returns a string in the encoding specified by the magic comment.
erb stands for "Embedded RuBy".
ERB is a templating engine. A templating engine allows you to mix HTML & Ruby so you can generate web pages using data from your database. ERB is Rails default engine for rendering views. Note: Rails uses an implementation called erubi instead of the ERB class from the Ruby standard library.
Script written in ERB, a templating language for Ruby; may include any type of plain text or source code, but also includes Ruby ERB code that generates additional text into the resulting file when run with the ERB template engine. ERB is often used for templating Web files such as . RB, . RHTML, .
<%==
is equivalent to raw
.
From the Ruby on Rails Guide:
To insert something verbatim use the
raw
helper rather than callinghtml_safe
:<%= raw @cms.current_template %> <%# inserts @cms.current_template as is %>
or, equivalently, use
<%==
:<%== @cms.current_template %> <%# inserts @cms.current_template as is %>
Rails actually uses Erubis instead of ERB, which supports a variety of other stuff.
<%==
is exactly as you expect, though: It emits the value unescaped
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With