Using ERB for Nesta layout and page templates - ruby

I'm trying to get Nesta to use .erb templates for layout and page templates, but confused as to how to go about it.
With partials, it's easy:
= haml :header, :layout => false
becomes:
= erb :header, :layout => false
However, I can't figure out how to have all my pages point to a layout.erb file instead of a layout.haml file.
According to this, .erb templates seem fully supported, though they only describe how to apply them for individual routes. While confusingly in this post it says that "features include support for Erb templates (in the views folder only, not for pages)"?
Does anyone have a better understanding of this?

Related

Using content_for and yield_content in Ramaze

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

Accessing the name of the page in ERB

I am building a website with Sinatra and ERB templates. Within the ERB I would like to access the name of the page being loaded and change a small part of the layout accordingly.
For example, my routes are like:
get '/' do
erb :index
end
I need something like:
<% unless page_is_index %>
<!-- HTML goes here -->
<% end %>
There's no direct relation in Sinatra between routing and controllers, so there isn't current_controller and current_action helpers like in Rails. What you can do is checking request.path.
You can specify a different layout which makes the changes you need. If nothing else, this layout could just include the main layout with more options.
get '/' do
erb :index, :layout => 'index_layout'
end

Sinatra not loading layout file when URL has 2 sublevels

I have this code:
# Users page
get '/admin/users' do
#title = "Admin - Users"
erb :admin_users
end
When the view is rendered, I get the HTML in the ERB file but the layout file does not render. If I change the route to just '/users' everything renders fine. What gives? I originally put the users ERB page in a subdirectory 'views/admin' after finding sample code on how to do that and hit this issue. I thought that was the reason but rather it seems its the URL causing the problem. Anyone else hit this or know a work around?
All my other views work fine as well. This is the first view I've tried the URL pattern with. I also tried this but it didn't affect anything.
# Users page
get '/admin/users' do
#title = "Admin - Users"
erb :admin_users, :layout => :layout
end
Any help is appreciated. Thanks.
I've got the same problem, check paths to yours assets in the layout. You should add / before.
<link href='/stylesheets/application.css' rel='stylesheet' type='text/css'> is correct.
If you use a subdirectory for the admin_users view, you should really save it as views/admin/users.erb and then render it with
get '/admin/users' do
erb :'admin/users'
end
Maybe this will already address your issue. Let me know, if you need more help.
I guess you can get into trouble when you set the view directory for the admin views different from the location of the layout.erb file. Have you tried putting a dummy layout.erb in the views/admin/ folder?
You did not showed the exact error your are getting. I assume it is "No such file or directory" for the template "admin_users.erb".
An option is missing on your "erb" call and you should tell Sinatra where to find the template. This has just worked for me:
# Users page
get '/admin/users' do
#title = "Admin - Users"
erb :'admin/admin_users', :layout_options => { :views => 'views/admin' }
end
It renders the template on views/admin/admin_users.erb using the layout from views/admin/layout.erb

HAML -> Backbone Template, Unescaping HTML Parameters

I'm using HAML to generate templates for a Backbone.js app. I need to be able to insert <%= blah %> as an html attribute a la:
%a{:href => "myresources/<% id %>"} My Resource
and have it output
<a href='myresources/<%= id %>' >My Resource</a>
in the html template. Unfortunately, HAML escapes the html parameters leaving me with
<a href='#myresources/<%= id %>'>My Resource</a>
According to the HAML Reference the '!' operator can be used for unescaping strings, but not within the HTML attributes.
Also, I'd use plaintext to render the anchor tag, but since the anchor tag is the root for this particular view, I lose all of the benefits of using HAML.
Any help?
Update
I didn't mention, but I'm using LiveReload to actually watch my file system and run the haml compiler, and there was a setting in LiveReload to disable HTML escapes in tag attributes. < head slap > If anyone else runs into this issue outside of LiveReload, you can also set the :escape_attrs option to false when configuring your HAML setup.
You can configure HAML to not escape tag attributes using the escape_attrs option in your HAML configuration. See HAML Options.
You can try using html_safe which is a method on String objects. This will escape the html characters in the variable statement (< for example) and will leave the intact for underscore to evaluate at runtime:
%a{:href => "myresources/<% id %>".html_safe} My Resource
Found on answer to Interpolate inside html attributes with Underscore.js

How to create 1 view for 2 layouts in erb?

I make a project on serve and use erb syntax.
I want to compile two files: index.html (compiled with template content) and load.html (only content). I create two layouts (_clear.html.erb (with the only yield) and _default.html.erb) and create a partial (_content_for_page.html.erb).
There are problems with compilation of index.html.erb.
According to Rails manual (3.4.3 Partial Layouts) I have to write index.html.erb:
<%= render :partial => "_content_for_page.html.erb", :layout => "_default.html.erb" %>
But it doesn't work. Only partial is compiled.
What is wrong?
First I just want to point out Serve does not utilize Rails unless the rails gem is included in the Gemfile of your project, but that will do something unexpected considering Serve organizes your project differently than Rails.
Now I don't know if this answers your question, but it may help to know partials should be rendered without the beginning underscore and are usually passed as a symbol.
<%= render :partial => :content_for_page, :layout => :default %>

Resources