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
Related
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 a lot of comments in Rails views.
How i can prevent rendering them ?
If I understand the question correctly, you're asking about Ruby/Rails comments vs HTML comments... Give this a try in your view:
<!-- This is an HTML comment and will show up in the HTML source! -->
Now try this:
<%# This is a comment that won't show up in the HTML source! %>
<%#
You can even use this for commenting multiple lines!
How useful!
%>
Does that help?
use =begin and =end to mark the beginning and end of your comment
There is no easy way to do that. You can monkey patch ERB sources, perhaps, but it is a bit nerdy.
I'm not a Rails programmer, but a quick but of Binging brought up this link:
http://blog.brijeshshah.com/strip-tags-in-rails-javascript-and-php/
The approach he's using is one that I've used in the past where you sanitize the view's output. sanitize being the name of the function you want to use before rendering the view.
Maybe you can use Haml Comments: -# allow to comment your haml code without them appearing in the generated html.
Kind of hackish, but you can wrap it in a helper method
In your view:
<% comment do %>
<%= "this won't be executed" %>
or displayed
<% end %>
in app/helpers/application_helper.rb
module ApplicationHelper
def comment(&block)
## you can do something with the block if you want
end
end
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.