I am working on a code base which as VBScript code embedded in HTML. I've noticed the following two different tags around said lines of code
<%= MyFunc(val1) %>
and
<% MyFunc(val1) %>
What is the difference in using the "=" character at the beginning of these sections?
<% evaluates an expression in server code but doesn't emit output.
<%= also evaluates the expression but wraps the result in Response.Write, so it produces output.
When you see:
<%= MyFunc() %>
it really means:
<%
Response.Write( MyFunc() )
%>
Its short hand for writting output to the response.
<%
MyFunc()
%>
The above will just run the code but won't write it to the response unless it has some Response.Write's inside the Function/Sub itself.
Related
When I attempt to loop through sorted_articles() in Ruby, the embedded code just stays the same and doesn't get compiled in the output when I run nanoc compile. Why is this? Am I embedding it wrong? I've tried all the variations below:
<%= for item in sorted_articles()
<p>item[:title]</p>
end %>
also
<% for item in sorted_articles() %>
<p>item[:title]</p>
<% end %>
and
<% sorted_articles().each do |item| %>
<p>item[:title]</p>
<% end %>
and finally
<%= sorted_articles().each do |item|
<p>item[:title]</p>
end %>
I also followed a tutorial here pretty much exactly as I found it, but still no luck! The routing is working, so are the rules, and everything else. Just this damn loop!
Thanks. The source can be found on gitlab and the live site can be found here
The compile rule for all .html files wasn't being passed through the erb filter, and as Sergio mentioned in the comments, the code was being ignored. Adding the filter :erb fixed the issue
My .erb file does not execute the code inside <%= %> as ruby code. For example::
<%= puts "almost"%>, when rendered is just <%= puts "almost"%>. The erb engine does not even process anything. This means that NOTHING inside <%= %> is being processed .Any help will be appreciated.
You are confusing erb expressions with Ruby code. Use <% %> to have arbitrary Ruby code executed.
<% puts "almost" %>
Anything inside <% %> will be evaluated as ruby code, and anything inside <%= %> will be outputed as html. So when you do <%= puts "almost" %> it is outputded directly as HTML. You can solve this using
<% puts "almost" %>
or
<%= "almost" %>.
Essentially I want something like the following:
<code class="snippet">
<%= html_escape do %>
My markup displayed to user
<% end %>
</code>
However the html_escape method does not accept a block. If this is not built into Rails API somewhere else, perhaps using some helper, does anyone have advice on how to make a custom helper where the yield statement output is captured into a string that I can then escape myself?
Thanks,
Keith
Rails' capture and escape_once helper methods can create a String from a block in an erb template and then output an escaped version of it:
<% snippet = capture do %>
My markup displayed to user
<% end %>
<code><%= escape_once snippet %></code>
content_for is another helper that provides similar functionality to capture, that you may consider using depending on the situation.
To explain, snippet is an ActiveSupport::SafeBuffer, and is why escape_once is needed. You could achieve the same by calling snippet.to_str instead of escape_once snippet (However .to_s will not work as that is different to .to_str in ActiveSupport::SafeBuffer).
I've got a method in my view that decides which HTML to print out depending on the class of the object it gets:
<% def laybricks(c,stream) %>
<% if c.is_a?(Post) %>
<article>
// Tons more HTML unique to posts
</article>
<% elsif c.is_a?(Photograph) %>
<article>
// Tons more HTML unique to photographs
</article>
<% end %>
<% end>
But I now know more about Rails since writing it. How can I transfer this to my model so that I can call the method in the view and have it print the HTML the same?
Method in a view would definitely be better moved to the helper.
But for your need the best solution is partials utilizing some rails magic:
<%= render #c %>
will magically render partial _post.html.erb if #c is an instance of Post and _photograph.html.erb if it is instance of Photograph. These partials should be in the same view folder, and you can reference your object as post and photograph in each partial respectively.
For more info see Rails Rendering Guide.
ADDON:
How can I transfer this to my model so that I can call the method in the view and have it print the HTML
It is usually not a responsibility of a model to generate HTML, but if you ultimately want to do this, the best way is to use a design pattern that is usually referred to as Presenter or Decorator. E.g. draper gem exists for this, but also many other. And there is many discussions in Rails community how this pattern should be called, how it should be implemented and is it worth it at all.
UPDATE:
Application Helper
def laybricks(c,stream)
content = []
if c.is_a?(Post)
content << "<article> </article>"
elsif c.is_a?(Photograph)
content << "<article> </article>"
end
end
Your View
<%= raw laybricks %>
OR
<%= laybricks.html_safe %>
NOTE: The raw helper will allow the plain html to get interpreted. You can also use html_safe method to interpret the same.
i have the t function witch return some text.
so on my erb file i have something like that:
<%= t 'content1' %>
<%= t 'content2' %>
and the html output is something like that:
"text of the content 1"
"text of the content 2"
I would like to output something like that:
"text of the content 1" "text of the content 2"
thanx
ERB has an option in the closing tag whether to have a newline after it. Just add a dash:
<%= t 'content1' -%>
<%= t 'content2' %>
Erb is a little overrated compared to other template languages out there. But, the good thing is it allows you to control the output. As #DMG pointed out add a '-' to your tags but there is also another point I wanted to show.
<%- method(...) -%>
Will not show any lines on output. While this will create a blank line:
<% method(...) -%>
Just something to keep in mind if your writing templates for scripts, unit tests, rake task, or anything else outside of Rails/ActionView
<%= "#{t('content1')} #{t('content2')}" %>
just do:
<%= "#{t('content1')}#{t('content2')}" %>