Using content_for and yield_content in Ramaze - ruby

Sinatra has sinatra/contrib to asist with this, what's the equivalent in Ramaze? I'm using Erubis by the way. Also, a quick Google search shows up really old pages that insist setting variables in the controllers and using them in the views.
Edit 1:
Taken from the gem documentation (http://www.sinatrarb.com/contrib/content_for.html)
You call content_for, generally from a view, to capture a block of markup giving it an identifier:
# index.erb
<% content_for :some_key do %>
<chunk of="html">...</chunk>
<% end %>
Then, you call yield_content with that identifier, generally from a layout, to render the captured block:
# layout.erb
<%= yield_content :some_key %>

I don't think Ramaze can do this natively. But you could quite easily do this manually, write a helper to do this, or even fill-in a Hash instance.
You might also want to look at partials if you need to render small chunks of HTML in loops.
You could also combine render_partial, store results in a hash, and yield it's content in the layout.
If the use case is something like rendering a sidebar, you probably want to write a helper so you take the logic out of your views.
A trivial example is here : https://github.com/Ramaze/ramaze/wiki/Adding-a-dynamic-sidebar-in-a-layout

Related

Use rspec with capybara-webkit or selenium to measure page load times

I'm hoping to find a way to write rspec scripts (or something else) that can be used to measure page load times. So similar to how firebug can tell when a page has fully loaded, is there a similar way to do that in rspec?
Or even any other ruby gem?
Thanks!
You can use benchmark in your views:
<% benchmark("Showing projects partial") do %>
<%= yield %>
<% end %>
If you do this in your application layout you'll have a log with page loads:
Showing projects partial (0.0ms)

Frontmatter in Padrino

In Middleman, there is a feature called frontmatter. Basically, you can put some YAML in front of your view and access it in the view and the layout like this:
---
my_list:
- one
- two
- three
---
<h1>List</h1>
<ol>
<% current_page.data.my_list.each do |f| %>
<li><%= f %></li>
<% end %>
</ol>
Is there a similar thing in Padrino?
Such feature does not come with padrino, where you usually would let these values be filled by your controllers.
Depending on your use case, you might use the content_for output helper (http://www.padrinorb.com/guides/application-helpers).
This might help if e.g. want to define the title (as in html/head/title) -- which is usually set by the layout template -- in the "nested" template of your page.

rendering Ruby statements inside of rails views?

So in my DB I want stored a ruby/HTML statement in the Database Table such as -
p This site is owned from 2000 - #{Time.now.year} by Acme Widget Co. /p
Or let's say that I want people to be able to include in other code snippits - e.g.
A client wants to run in a show view in the middle of a paragraph some magical partial such as #{render 'my_magical_code'}
On the view I have this being rendered as <%= raw(#page.content) %>
But its like a double rendering and rails will just put on the page #{render 'my_magical_code'} or #{Time.now.year}
So can / how do I solve this?
Thanks!
There are work arounds but I think the most proper way to solve your problem is to store your information in a better way. Add a migration to your model with a year_founded and company rows. You can then call these in your views rather than storing the HTML. Eg. This site is owned from <%= #object.year_founded %> to <%= Time.now.year %> by <%= #object.company %>

Alternative to <%= in Ruby On Rails view?

So I was wandering if there is an equivalent to <%= in a Ruby on Rails view using <% and %>.
PHP allows the use of <?=$str ?> and <? echo $str ?>, do views in RoR allow something like this? Doing <%= in a large block of Ruby On Rails code in a view isn't very nice to see, not to mention a nuisance when doing something like for loops. Is it possible?
That isn't Ruby On Rails construction, that's construction of ERB template engine. If you like, you can use haml or any other template engine. This is rather old, but interesting review many of them.
You are talking about *.html.erb files. Try something else. For instance, HAML. http://haml-lang.com/

Rails 3 unwanted html escaping

I am converting my fat Rails2 application to run on Rails3. After a long intense fight with an army of bugs and my bosses yells, the page is all rendered as an escaped html string. So all the divs, images etc. are written literally for the user.
For some reason this call of a partial renders an escaped string
<%= render :partial => 'something_really_interesting' %>
As all Ruby on Rails application this instruction is not called very much! So how would I handle all these calls to not render normally not as an escaped string?
Use <%= raw bla %> in inside the partial file.
http://github.com/rails/rails/blob/3270c58ebb3143b3ab3b349fe339cdd4587468ee/actionpack/lib/action_view/helpers/raw_output_helper.rb#L13
Rails 3 automatically makes everything safe. You need to put raw to escape the behavior. That also means you don't have to use h() method to make your string safe any more.

Resources