【问题标题】:Padrino: sharing layout between main project and sub appsPadrino:在主项目和子应用之间共享布局
【发布时间】:2012-02-26 15:31:59
【问题描述】:

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

控制器有一个布局选项.. 但是我应该设置什么值才能让它遍历回父项目并使用它的 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 的情况下尝试过,使用 etc.. 返回的路径是正确的.. 所以我不确定它为什么不使用它?

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

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

【问题讨论】:

    标签: layout padrino


    【解决方案1】:

    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
    

    【讨论】:

      【解决方案2】:

      当您指定一个 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中设置布局似乎没有任何作用,我稍后会尝试调查。

      【讨论】:

      • 我希望你有一个替代解决方案,因为当我升级 sinatra 或 padrino 时,它会再次崩溃。我想我总是可以从 github 上分叉,然后继续合并任何更新。
      【解决方案3】:

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

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

      这将呈现:

      # project/parent_app/views/application.haml
      

      【讨论】:

      • 默认布局是应用程序,这是我现在拥有的。但是我找不到一种方法来告诉我的子应用程序使用我的项目 application.haml 而不是它自己的?当我设置路径时,它总是将我的子应用程序名称放在路径中。你能告诉我一个如何设置布局路径以指向位于当前应用上方一个目录中的布局的示例吗?
      • 发布结果。将其添加到控制器会引发异常。将其添加到子应用的 app.rb 不会引发异常,而只会呈现视图结果。它不包括布局。
      猜你喜欢
      • 2019-12-22
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 2012-11-29
      • 2014-04-13
      • 2017-03-07
      相关资源
      最近更新 更多