Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a layout exists in Rails?

Is there a standard way to check if a view layout exists from within a Controller in Rails? I'm trying to allow the user to determine the layout, but it needs to exist first.

like image 233
Lance Avatar asked May 17 '10 22:05

Lance


2 Answers

You can use template_exists? which is an alias for exists?

For example: template_exists?("layout_name", "layouts")

like image 172
Joni Avatar answered Oct 01 '22 22:10

Joni


There is no standard public way as far as I know. You could use a rudimentary call like this:

layouts = Dir['app/views/layouts/*'].map {|f|
  File.basename(f, '.html.erb')  # returns 'layout' for 'layout.html.erb'
}
like image 43
Daniel Beardsley Avatar answered Oct 01 '22 23:10

Daniel Beardsley