布局存在于 app/apps/themes/default_theme/views/layouts 中 我有一个前端控制器,它根据参数呈现不同主题的布局。现在,我们假设它呈现default_theme。但是如何设置默认主题呢?
module MyEngine
class FrontendController < ApplicationController
before_action :set_theme_view_path
layout :determine_layout
def index
end
private
def set_theme_view_path
theme = 'default_theme'
# Add both the layouts and views paths for the theme
theme_layout_path = MyEngine::Engine.root.join('app', 'apps', 'themes', theme, 'views', 'layouts')
theme_view_path = MyEngine::Engine.root.join('app', 'apps', 'themes', theme, 'views')
prepend_view_path(theme_layout_path)
prepend_view_path(theme_view_path)
end
def determine_layout
theme = 'default_theme'
"layouts/application"
end
end
end
如何在运行时动态渲染布局?就我而言,只是另一个文件夹中的特定布局,而不是视图文件夹内的常规布局?
您无需添加
theme_layout_path
。布局名称应相对于 views/layouts
:
before_action :set_theme_view_path
layout :determine_layout
private
def set_theme_view_path
theme = "default_theme"
prepend_view_path MyEngine::Engine.root.join("app/apps/themes/#{theme}/views")
end
def determine_layout
# "app/apps/themes/default_theme/views/layouts/default_theme_layout.html.erb"
"default_theme_layout"
end