How do I create root-relative links in a static site?

后端 未结 2 1991
死守一世寂寞
死守一世寂寞 2021-02-05 21:17

When building a static HTML site, you can set the base url like so . Supposedly when you insert, say, an image, y

2条回答
  •  梦谈多话
    2021-02-05 21:32

    I'm aware that I'm a little late to the game on this one, but you should really be using Rails asset tags instead of raw HTML here.

    For instance, instead of using:

    
    

    You should use:

    <%= image_tag 'logo.png' %>
    

    Assuming that:

    1. You're using .erb files for your source pages
    2. You've set the image asset path to /img/ in your config.rb file

    Alternately, you could reference CSS with:

    <%= stylesheet_link_tag 'file.css' %>
    

    Javascript files can be included with:

    <%= javascript_include_tag 'file.js' %>
    

    Since Middleman allows you to control whether or not assets are referenced relatively (by uncommenting some lines in config.rb), using Rails asset tags make much more sense than static HTML ones. I highly recommend switching if you haven't already done so. If you have any further questions about ay of these tags or the ERB syntax, feel free to ask away on here!

提交回复
热议问题