views:

173

answers:

1

Hi

In sinatra's app,

require 'rubygems'
require 'sinatra'
require 'haml'

get '/new' do
  haml :new
end

get '/edit' do
  haml :edit
end

__END__

@@ layout
%html
  %head
    %title
  %body
  = yield

@@ _form
# partial form

@@ new
%h1 Add a new item
# require partial _form

@@ edit
%h1 Edit an existing item
# require partial _form

How to require the partial @@ _form template in @@ new and @@ edit?

Thank you.

+1  A: 

Have you looked at this yet: http://www.sinatrarb.com/faq.html#partials?

I created app_helpers.rb and required it in my main app file. app_helpers contains this method:

  def partial(template, *args)
    options = args.last.is_a?(Hash) ? args.pop : { }
    options.merge!(:layout => false)
    if collection = options.delete(:collection) then
        haml_concat(collection.inject([]) do |buffer, member|
          buffer << haml(template, options.merge(
                                  :layout => false,
                                  :locals => {template.to_sym => member}
                                )
                     )
      end.join("\n"))
    else
      haml_concat(haml(template, options))
    end

end

In my views, I use:

- partial :file
Brian Deterling
thanks, this helper method is exactly what I also needed!
korch