0

如何将我的主项目布局用于我的子应用程序?

控制器有一个布局选项..但是我应该将值设置为什么以便它遍历回父项目并使用它的 application.haml 代替?

我尝试添加:

File.expand_path('../../app/views/layouts/application.haml', __FILE__)

不幸的是,在控制器中,当前应用程序布局文件夹的路径似乎总是添加到它的前面,所以你最终会得到类似的东西

c:/sites/demo/app01/views/layouts/c:/sites/demo/app/views/layouts/application.haml

此外, .haml 已经添加,所以如果你将它添加到控制器中,你最终会得到

application.haml.haml

这只是控制器的情况。

基于这些结果,我将代码移到了子 app.rb 中,这实际上更适合我的情况。

但是,没有渲染主布局,所以我只看到当前控制器操作的结果。它不输出布局。

我在没有文件 ext 的情况下尝试了它,等等。返回的路径是正确的..所以我不确定它为什么不使用它?

至少在控制器中,它抛出了一个错误,因为它是一个无效的参数。

在我的子应用程序的 app.rb 中包含代码不会产生错误,但它也不会呈现布局。只是查看结果。

4

3 回答 3

0

当您指定 abs 文件路径时,我花了一点时间查看控制器在当前应用程序布局文件夹的布局之前出现的问题。

文件:padrino-core/application/rendering.rb

方法:fetch_layout_path

我添加了 if / else 来检查是否有 abs 文件路径。

return cached_layout_path if cached_layout_path
if File.exists?(layout_name.to_s)
    layout_path = layout_name.to_sym
else
    has_layout_at_root = Dir["#{views}/#{layout_name}.*"].any?
    layout_path = has_layout_at_root ? layout_name.to_sym : File.join('layouts', layout_name.to_s).to_sym
    end
@_cached_layout[layout_name] = layout_path unless reload_templates?
layout_path

但是,它仍然出错。

文件:sinatra-1.3.2/lib/sinatra/base.rb

方法:find_template

我添加了一个

yield name.to_s if File.exists?(name.to_s)

就在之前

yield ::File.join(views, "#{name}.#{@preferred_extension}")

这工作没有任何错误。

我对 ruby​​ 很陌生,对 sinatra / pandrino 更是如此,所以这可能不是正确的方法,但它有效,并且可以让我继续我目前的开发。

DAddYE:我很想听听您关于更好的选择的回复?我不确定为什么在子应用程序的 app.rb 中设置布局似乎没有任何作用,我稍后会尝试调查。

于 2012-02-06T01:25:20.197 回答
0

您可以在主应用程序或控制器中使用:

# in project/parent_app/sub_app/app.rb
layout File.expand_path('../../parent_app/views/application.haml', __FILE__)

这将呈现:

# project/parent_app/views/application.haml
于 2012-02-04T10:17:56.650 回答
0

fetch_layout_path必须是相对路径。所以你应该在padrino-core/application/rendering.rb中这样做:

它对我有用。但是如果 padrino 允许嵌套子应用程序,这不是完美的方法。

  def fetch_layout_path(given_layout=nil)
    layout_name = given_layout || @layout || :application
    @_cached_layout ||= {}
    cached_layout_path = @_cached_layout[layout_name]
    return cached_layout_path if cached_layout_path
    has_layout_at_root = Dir["#{views}/#{layout_name}.*"].any?
    layout_path = has_layout_at_root ? layout_name.to_sym : File.join('layouts', layout_name.to_s).to_sym
    # Check the layout file is exists in sub-app? 
    # try to use the root project's layout if not
    # added via riceball
    has_layout = Dir["#{views}/#{layout_path}.*"].any?
    layout_path = has_layout ? layout_path : File.join('..', '..', 'app', 'views', layout_path.to_s).to_sym
    @_cached_layout[layout_name] = layout_path unless reload_templates?
    layout_path
  end
于 2012-03-06T06:49:00.387 回答