Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Custom asset_path #245

Closed
ervasive opened this Issue Jan 20, 2012 · 5 comments

Comments

Projects
None yet
4 participants

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.

    Owner

    tdreyno commented Jan 20, 2012

    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 Mar 4, 2012

    ajax22 commented Jan 8, 2014

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

    Contributor

    barraponto commented Feb 17, 2014

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

    Owner

    tdreyno commented Feb 18, 2014

    <%=, but yeah, exactly like that.

    Contributor

    barraponto commented Feb 18, 2014

    sorry, too much PHP lately 💩

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment