This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
smarter Last-Modified header based on template mtime
- Loading branch information
Showing
2 changed files
with
42 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'sinatra/base' | ||
|
||
# Adds functionality to template rendering methods to call last_modified with | ||
# the template's timestamp before actually rendering it. | ||
# | ||
# Examples | ||
# | ||
# erb :index, auto_last_modified: true | ||
# | ||
# # with extra timestamp: | ||
# erb :show, auto_last_modified: @item.updated_at | ||
module AutoLastModified | ||
module TiltExt | ||
def mtime | ||
@mtime ||= File.mtime file | ||
end | ||
end | ||
|
||
module SinatraExt | ||
private | ||
def compile_template(engine, data, options, views) | ||
set_mtime = options.delete :auto_last_modified | ||
template = super | ||
if set_mtime | ||
mtime = template.mtime | ||
mtime = set_mtime if set_mtime.respond_to? :hour and set_mtime > mtime | ||
last_modified mtime | ||
end | ||
template | ||
end | ||
end | ||
|
||
Tilt::Template.send :include, TiltExt | ||
Sinatra::Base.send :include, SinatraExt | ||
end |