This question already has answers here:
What does <<DESC mean in ruby?
(3 answers)
Closed 8 years ago.
I saw the following code in deviser_helper.rb. What does <<-HTML ... HTML mean here:
html = <<-HTML
<div id="error_explanation" class="alert">
<h2>#{sentence}</h2>
<ul>#{messages}</ul>
</div>
HTML
Surely a great way to write embedded HTML code in ruby. But why this works and where it comes from?
This is just a multiline string in Ruby. Usually it's called a heredoc or here document. From the linked documentation:
Following a << you can specify a string or an identifier to terminate the string literal, and all lines following the current line up to the terminator are the value of the string.
The - after << means that you can indent the terminator, so that the HTML at the end can have spaces or tabs before it.
Related
This question already has answers here:
Thymeleaf: Concatenation - Could not parse as expression
(4 answers)
Closed 3 years ago.
I am new to thyme leaf , and i am trying to concatenate the text from the java code with the html . i show below.
<h1 th:utext="${message}" id="font">
Hello does not concat with message.
</h1>
That's just not how thymeleaf works. th:text and th:utext replace the contents of the tag.
The th:text attribute ... sets the result as the body of the host tag, effectively replacing the ... text we see in the code.
There are plenty of ways to accomplish what you want though. For example, to append at the end:
<h1 id="font">
Hello does concat with message.
<span th:utext="${message}" />
</h1>
Or
<h1 th:utext="|Hello does concat with message. ${message}|" id="font" />
This question already has answers here:
Accessing a Session object from Razor _Layout.cshml
(2 answers)
Closed 7 years ago.
I am trying to add a session variable to html markup using MVC Razor, I tried this:
<div class="panel-body">
<p>It might suprise you to know that you can upgrade from #Session("CurrentProvider") to blah blah....</p>
</div>
I tried wrapping it in code tags and all sorts. How can I fix this?
Seems the answer is to append with ToString
#Session("CurrentProvider").ToString
This question already has answers here:
Preserve newline in text area with Ruby on Rails
(3 answers)
Closed 8 years ago.
It seems like rails treat every post with line break by change the line break into a space.
for example:
when I type
hi
hi
hi
in my form, what I get is just a simple "hi hi hi". How to change that?
I assume that in post form for description field you may have taken textarea
as Rustam commented you can use simple_format helper method: Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in <p> tags.
You can use like
<%= simple_format(#post.description) %>
I hope this work out for you..
This question already has answers here:
Embedding Ruby code in HTML?
(3 answers)
Closed 9 years ago.
Is it possible to put Ruby code in the HTML page which has a extension ".rhtml"?
For example, I want to use a HTML form and then some Ruby code. Does it have to be encapsulated in the <% tags?
The file extension should be ".html.erb" for Rails 3 or above.
The Ruby code should be between <% %> for logic statements, or between <%= %> if you want to display the results of the code.
".erb" is what you are looking for. Rails uses ".erb" files and it is possible to embed Ruby code inside them.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Difference between -%> and %> in rails
I need to know what is the difference between <%= expression %> and <%= expression -%> on rails, please help me to make good foundation on Ruby On Rails
The '-%>' means, that no linebreak will be appended to the output of the expression. It's usefull if you want to control the amount of whitespace you have in the generated HTML but do not want to put all the code in a single line.
As mentioned before, the -%> is used to control output whitespace. I you're at all concerned with how your HTML looks, use HAML. HAML is way more clear and readable when coding and it generates clear, valid formatted HTML. No more forgotten close tags!
I say don't bother with '-%>' If you are using layouts and partials with your views it is difficult control the output anyway, things like indentation will likely be messed up. Just focus making your ERb look good and don't worry too much what the generated output looks like.