Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Custom asset_path #245

Closed
dbooom opened this Issue · 5 comments

4 participants

@dbooom

Middleman has few options for customize pathes to images, styles and js scripts:

:css_dir
:js_dir
:images_dir

But what if I want to register custom path (for videos or documents for example) with ability to have :relative_assets routings?

Is it possible to register custom pathes or it is better to use helpers for such a feature?

Thanks.

@tdreyno
Owner

Probably better to use a helper. It's not well documented, but you can "register" your own asset_url method which is what image_tag and other asset helpers use to find files. This method acts as a stack so you could register your handler at the first position and then the other handlers should run after (adding cache busting, relative paths, etc).

Here's an example:

module MyAssetHandler
  class << self
    def registered(app)
      app.send :include, InstanceMethods
    end
    alias :included :registered
  end

  module InstanceMethods
    def asset_url(path, prefix="")
      if prefix == "video"
        # Do something fancy
      else
        # super (get the results of higher up the stack, may not be necessary)
      end
    end

    def video_path(my_video)
      asset_path :video, my_video
    end

    def video_tag(my_video)
      tag(:video, :src => video_path(my_video))
    end
  end
end

activate MyAssetHandler
@tdreyno tdreyno closed this
@ajax22

can u plz post an example of how to actually use this helper in code?

@barraponto

Would it be something like <?= video_tag('relative/path/to/video) ?> ?

@tdreyno
Owner

<%=, but yeah, exactly like that.

@barraponto

sorry, too much PHP lately :poop:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.