Usage

This is an article that demonstrates how to use the library. Notice that this is a Main App and an App2. This is to demonstrate that you can give links relative to the app, but if they are mounted with an extra prefix (using Rack#map, for example) the helper will respect that and produce the right href attribute.

Installation and loading

Start by installing:

gem 'sinatra-static-assets'

Then require it in your Sinatra app.

require 'sinatra/static_assets'

The helpers

Use these helpers in your views.

stylesheet_link_tag

The code:

stylesheet_link_tag "/css/screen.css"

Output:

<%= Rack::Utils.escape_html( stylesheet_link_tag "/css/screen.css") %>

javascript_script_tag

The code:

javascript_script_tag "http://code.jquery.com/jquery-1.9.1.min.js"

Output:

<%= Rack::Utils.escape_html( javascript_script_tag "http://code.jquery.com/jquery-1.9.1.min.js") %>

Also known as:

  • javascript_include_tag
  • js_tag
  • script_tag

image_tag

The code:

image_tag "http://farm3.staticflickr.com/2474/3609420787_f7fc0e53c7.jpg", width: "500", height: "275", alt: "Magic Ball"

Output:

<%= Rack::Utils.escape_html( image_tag "http://farm3.staticflickr.com/2474/3609420787_f7fc0e53c7.jpg", width: "500", height: "275", alt: "Magic Ball") %>

image_tag

The code:

favicon_tag

Output:

<%= Rack::Utils.escape_html( favicon_link_tag ) %>