LoginSignup
3
1

More than 5 years have passed since last update.

Padrinoで複数のアプリケーションをマウントしている時に別のアプリケーションのviewを使う

Posted at

Padrino 0.13.2でのお話です。Padrinoは複数のアプリケーションをマウントして用いることができます。

#{Padrino.root}/config/apps.rb
# Mounts sub applications.
Padrino.mount('MyApp::SubApp0', :app_file => Padrino.root('subapp0/app.rb')).to('/subapp0')
Padrino.mount('MyApp::SubApp1', :app_file => Padrino.root('subapp1/app.rb')).to('/subapp1')

# Mounts the core application for this project
Padrino.mount('MyApp::App', :app_file => Padrino.root('app/app.rb')).to('/')

この時、夫々のアプリケーションのviewやlayoutは別々の名前空間に存在しますが、例えばadmin用途と一般ユーザ用途でよく似たviewを使いたいことがあります。その場合は、renderにオプション引数でviewとlayoutのパスを指定します。

#{Padrino.root}/subapp0/controllers/something.rb
get :index do
  ...
  render "something/index", views: MyApp::App.view_path(""), layout: MyApp::SubApp0.layout_path(:application)
end

これでメインのアプリケーションの something/index.haml を、SubApp0のレイアウトを用いて描画することができます。ヘッダやフッタはSubAppの用途によって違うこともあるので、レイアウトも別途指定できるのは便利だと思います。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1