Question

I have several microsites, each with its own stylesheet assets, within a larger Middleman project like so:

project/
  source/
    microsite1.com/
      stylesheets/
      index.haml
    microsite2.com/
      stylesheets/
      index.haml
    stylesheets/
    index.haml
  config.rb

Now, in production, each microsite is accessed via a domain root, e.g. http://microsite1.com/. But the above directory structure is what's required by my webhost to manage these microsites, so in development it's ideal to access these at http://localhost:4567/microsite1.com/.

However, the paths that asset helpers output aren't relative. For example, in microsite1.com/index.haml:

= stylesheet_link_tag "screen"

yields

<link href="/stylesheets/screen.css" media="screen" rel="stylesheet" type="text/css">

with :relative_assets unset, and yields

<link href="../stylesheets/screen.css" media="screen" rel="stylesheet" type="text/css">

with it set. The former output is correct in the production case; the latter is correct in neither production nor development.

Is there a way to configure Middleman so that I can test at http://localhost:4567/microsite1.com/? Alternatively, is there some way I can simulate http://microsite1.com/? (I thought to try modifying /etc/hosts, though that doesn't seem to work since I'm not pointing at an IP address)

Was it helpful?

Solution 2

Here is my hacky but actually pretty functional solution:

# microsite1.com/index.haml
- if development? then $asset_base = "/microsite1.com" end

# config.rb
configure :development do
  helpers do
    alias_method :original_asset_path, :asset_path
    def asset_path(*args)
      path = original_asset_path(*args)
      if not path =~ ABSOLUTE_URL_PATTERN && defined? $asset_base
        path = File.join($asset_base, path)
      end
      path
    end
  end
end

tl;dr I'm hooking asset_path to guarantee that all relative assets (stylesheets, javascripts, images) are prefixed with some $asset_base path if specified. (If someone better at Ruby + Middleman than me wants to advise how I might do this without a global variable, I'm all ears.)

OTHER TIPS

Why do you need to touch the css_dir setting at all? You should be able to use the stylesheet_link_tag helper as follows ...

<%= stylesheet_link_tag "../microsite1.com/stylesheets/microsite1" %>

... within your templates residing in source/microsite1.com. This should give you ...

<link href="/stylesheets/../microsite1.com/stylesheets/microsite1.css" media="screen" rel="stylesheet" type="text/css" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top