Stylesheet depends on hostname

Sorry my English

I'm working on multi face site, that runs on Sintra. I need to change CSS stylesheet in depend of given URL. Like:

get '/*' do
    case request.host
    when 'site1.com'
        @style = 'style1'
    when 'site2.com'
        @style = 'style2'
    end
    erb :index
end

Then in index.erb view:

<link rel="stylesheet" src="../<%= @style %>.css">

But if place code like this in action file, all pages content is "style1" or "style2". To prevent this I need to place case code in every get-do action. I think this is not the Ruby way. Is there exists any possible way to place that "stylesheet rewrite" in single action or another way to implement css rewriting in this case? Thank you in advance.

Answers


This is a very good reason for using a helper method. First add the helper:

helpers do
  def include_css(host)
    case host
    when 'site1.com'
      'style1'
    when 'site2.com'
      'style2'
    end
  end
end

Then in your index.erb:

<link rel="stylesheet" src="../<%= include_css(request.host) %>.css">

Disclaimer: I don't use ERB so it might not work out-of-the-box. Also, I don't know whether index.erb is a template common to all your pages, so maybe you'll have to put that line in each .erb file you serve.


better call stylesheet by layouts it will b the easy way


Need Your Help

Modify XSLT using C# Code

c# xml xslt visual-studio-2012

I am Working on Visual-studio 2012 in C#.

UIwebview delays loading

iphone uiwebview

I have Login screen and second view which will be shown after login. On second view I have UIWebview which loads the url.