1

在 sinatra 的应用程序中,

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

如何在和中要求部分@@ _form模板?@@ new@@ edit

谢谢你。

4

3 回答 3

3

你看过这个了吗: http ://www.sinatrarb.com/faq.html#partials ?

我创建了 app_helpers.rb 并在我的主应用程序文件中需要它。app_helpers 包含此方法:

  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

结尾

在我看来,我使用:

- partial :file
于 2010-02-24T14:17:40.707 回答
1

如果您使用的是 sinatra,为什么不使用它的内置 haml 方法,如下所示:

= haml :partial_form, layout: false

在铁轨中:

= render 'partial_form'
于 2014-07-13T20:43:49.327 回答
1

我会推荐 Sinatra-Partial 插件:https ://github.com/yb66/Sinatra-Partial

在您的代码中,您只需要 :install gem 'gem install sinatra-partial'

在你的代码中需要部分:'需要'sinatra/partial'

更改您的代码如下:

@@ new
%h1 Add a new item
= partial '_form'

@@ edit
%h1 Edit an existing item
= partial '_form'

如果你想在 _form 部分中添加一些参数,你可以使用 locals 来传递它:

= partial '_form', locals: { param: @param }
于 2013-11-18T21:06:02.547 回答