【问题标题】:Can I access Sintra settings outside of the website's application class?我可以在网站的应用程序类之外访问 Sintra 设置吗?
【发布时间】:2013-02-21 12:12:57
【问题描述】:

Website 类中,我可以获得settings.root 的root。在类之外,我看不到如何获取类对象的句柄。

我可以在路由块中插入一个实例变量@root = settings.root,这将使root 可以使用HAML。这是正确的方法吗?

 class Website < Sinatra::Base
    configure do
      set :root, File.dirname(__FILE__)
    end
    get '/' do
      haml :index, :layout => :base
    end
 end

【问题讨论】:

    标签: ruby sinatra


    【解决方案1】:

    我认为正确的方法是使用:locals 哈希作为haml 调用的参数,如下所示:

    class Website < Sinatra::Base
      configure do
        set :root, File.dirname(__FILE__)
      end
      get '/' do
        haml :index, :layout => :base, :locals => {:root_path => settings.root}
      end
    end
    

    在您的视图模板中,您可以访问 root_path 变量。

    【讨论】:

    • 太棒了!我可以使用locals[:root_path] 访问它
    猜你喜欢
    • 2011-11-10
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多