包括部分模板

发布于 2024-08-23 11:32:08 字数 455 浏览 7 评论 0原文

在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

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

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分开我的手 2024-08-30 11:32:08

您看过这个了吗: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

end

在我看来,我使用:

- partial :file

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
属性 2024-08-30 11:32:08

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

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

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

更改您的代码如下:

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

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

如果您想在 _form 部分中添加一些参数,您可以使用当地人通过它:

= partial '_form', locals: { param: @param }

I would recommend Sinatra-Partial plugin: https://github.com/yb66/Sinatra-Partial

In your code,you just need :install gem 'gem install sinatra-partial'

require partial in your code: 'require 'sinatra/partial'

change your code as follow:

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

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

if you want add some parameters into the _form partial, you can use locals to pass it:

= partial '_form', locals: { param: @param }
流绪微梦 2024-08-30 11:32:08

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

= haml :partial_form, layout: false

在 Rails 中:

= render 'partial_form'

If you are using sinatra, why not use it's built-in haml method like so:

= haml :partial_form, layout: false

In rails:

= render 'partial_form'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文