No commit activity in last 3 years
No release in over 3 years
Helpers for writing the HTML and caching of static assets. A fork of Sinatra Static Assets.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

<= 3.0.0, >= 1.0.0
 Project Readme

Sinatra Exstatic Assets

Master branch build status

Master branch: Build Status

Develop branch: Build Status

Preamble

This is a fork/reworking of wbzyl's library. I had many of the same requirements that the original library catered for, but some different ones too, and the beauty of open source code is you get to scratch your own itch! Many thanks to the contributors to that library for all their hard work and sharing the code.

What does it do?

It's a Sinatra extension that has some handy helpers for dealing with static assets, like stylesheets and images, in your views.

What's different from the other library?

  • There's no link_to method (it doesn't handle assets so it was cut).
  • There was a mutex in the library to handle timestamps and race conditions around that. That's gone.
  • The helpers now look at the timestamp for the file they're linking to, and add that as a querystring parameter to the link displayed in the view. This will help client browsers cache the file (add something like Rack Cache to aid with this).
  • There are some new options to give more control over whether the script_tag environment variable is prepended.
  • More aliases, and shorter aliases.
  • The tests are now a mixture of integration and unit test, but written using RSpec. There's also test coverage via SimpleCov, which is close to 100%.
  • More API docs via Yardoc.

Version numbers

This library uses semver to version the library. That means the library version is not an indicator of quality but a way to manage changes.

Installation

via Rubygems

gem install sinatra-exstatic-assets

and in your code:

require 'sinatra/exstatic_assets'

via Bundler

Put this in your Gemfile:

gem "sinatra-exstatic-assets", :require => "sinatra/exstatic_assets"

Usage

Here's a quick example, but there are more in the examples directory:

  require 'sinatra'
  require 'haml' # the lib doesn't rely on Haml, it's engine agnostic:)
  require 'sinatra/exstatic_assets'

  enable :inline_templates # the interesting bit below
  
  get "/" do
    haml :index
  end
  
  __END__
  
  @@layout
  
  !!!
  %title Example
  = favicon
  = css_tag "/css/screen.css"
  = js_tag "/js/helpers.js"
  = js_tag "http://code.jquery.com/jquery-1.9.1.min.js"
  %body
    = yield
  
  @@index
  
  %dt
    %dd This is an interesting photo
    %dl
      %a{ href: "http://www.flickr.com/photos/redfernneil/1317915651/" }
        = img "http://www.flickr.com/photos/redfernneil/1317915651/", width: 500, height: 250, alt: "Something about the photo"

There is also more detailed documentation on each helper in the {Sinatra::Exstatic::Helpers} API docs.

Formats

The time format is the result of the file's mtime. If you wish for a different kind of format, SHA1 of the file is available (and I may add more). To use:

  require 'sinatra/exstatic_assets/formats'
  register Sinatra::Exstatic

  configure do
    set :timestamp_format, :sha1
  end

And now the value returned will be the SHA1 hash of the file. You can override the choice by passing the timestamp_format to the method:

  = css_tag "/css/screen.css", timestamp_format: :mtime

TODO

  • Make it easy to pass in caching options.
  • Default dirs set up for things like /css, /images etc.
  • An image link tag.
  • Caching of the timestamps (but I'm not sure it's needed or worth it).

Licence

See the LICENCE file.