sinatra-contrib icon indicating copy to clipboard operation
sinatra-contrib copied to clipboard

Namespace loses inherited layout setting?

Open julie-ng opened this issue 12 years ago • 4 comments

I have a modular app setup with a base Controller like this:

module Controllers
  class Base < Sinatra::Application

    configure do
      set :root, File.expand_path('../../../', __FILE__)
      set :views, settings.root + '/app/views'
      set :erb, layout: :'layouts/layout'
    end

  end
end

Then I have a controller which inherits from that base controller:

module Controllers
  class Content < Base
    namespace '/foo' do

      # this has no layout
      get '/bar' do
        erb :'content/bar'
      end

      # this works if we force the layout
      get '/bar' do
        erb :'content/bar', layout: :'layouts/layout'
      end

    end
  end
end

Am I doing something wrong? If I remove namespace, my layout setting is properly inherited.

For now, I'm just going to avoid using name spacing and add the prefix manually to my routes. Seems more sensible than specifying layouts for each route.

Thanks

julie-ng avatar Aug 11 '14 14:08 julie-ng

Hello, this one is probably a bug that is pretty hard to solve.

I checked your problem and found a better solution: inherit from Sinatra::Base instead of Sinatra::Application and use require 'sinatra/base' instead of require 'sinatra' and it will work.

Here is a code for you:

require 'sinatra/base'
require 'sinatra/namespace'

module Controllers
  class Base < Sinatra::Base
    register Sinatra::Namespace
    configure do
      set :erb, layout: :layout
    end
  end

  class Content < Base
    namespace '/foo' do
      get '/bar' do
        # renders inside layout!
        erb :bar
      end
    end
  end
end

Controllers::Content.run!

lukateras avatar Aug 12 '14 17:08 lukateras

Hi Yegor, thank you for the suggestion. I tried your solution but it still doesn't work. It's probably due to something more complicated in my setup, which is modified based on the trevi gem, which I admittedly don't fully understand.

In case it's helpful for debugging, I also have

module Controllers
  autoload :Base,     './app/controllers/application_controller'
  autoload :Content,  './app/controllers/content_controller'
end

And in the app.rb, I have:

class App < Sinatra::Base
  ...
  use Controllers::Base
  use Controllers::Content
 ...
end

I'm going to assume it's me though and probably close the ticket.

julie-ng avatar Aug 12 '14 21:08 julie-ng

I have the same issue with erb.

I want multiple routes using different layouts, At the same time, I also using namespace, But It doesn't work!

This is config.ru

require './app'

# Map the application for this project
map('/') do
  run Test::WebApp
end

map('/admin') do
  run Test::AdminApp
end

This is app.rb

require 'sinatra/base'
require 'sinatra/contrib'

module Test
  class BaseApp < Sinatra::Base
    register Sinatra::Contrib

    configure do
      set :root, ::File.expand_path(".")
      set :views, settings.root + "/views"
    end
  end

  class WebApp < BaseApp
    configure do
      set :erb, :layout => :'/web/layout'
    end

    # This is ok!!
    get '/' do
      erb :'/web/index'
    end

    # This is not work!!!
    namespace '/test' do
      get '/' do
        erb :'/web/test'
      end
    end
  end

  class AdminApp < BaseApp
    configure do
      set :erb, :layout => :'/admin/layout'
    end

    # This is ok!!
    get '/' do
      erb :'/admin/index'
    end

    # This is not work!!!
    namespace '/test' do
      get '/' do
        erb :'/admin/test'
      end
    end
  end
end

zhangjx avatar Aug 27 '14 10:08 zhangjx

This should be fixed in master by #139, thank you!

zzak avatar May 22 '15 23:05 zzak