Added Less support

Signed-off-by: Simon Rozet <simon@rozet.name>
This commit is contained in:
Andrey Savchenko 2010-03-01 14:13:10 +02:00 committed by Simon Rozet
parent 4d61607176
commit 621bfcbd6a
5 changed files with 65 additions and 1 deletions

View File

@ -203,6 +203,20 @@ and overridden on an individual basis.
sass :stylesheet, :style => :expanded # overridden
end
=== Less Templates
The less gem/library is required to render Less templates:
## You'll need to require less in your app
require 'less'
get '/stylesheet.css' do
content_type 'text/css', :charset => 'utf-8'
less :stylesheet
end
Renders <tt>./views/stylesheet.less</tt>.
=== Inline Templates
get '/' do

View File

@ -284,7 +284,7 @@ module Sinatra
#
# Possible options are:
# :layout If set to false, no layout is rendered, otherwise
# the specified layout is used (Ignored for `sass`)
# the specified layout is used (Ignored for `sass` and `less`)
# :locals A hash with local variables that should be available
# in the template
module Templates
@ -304,6 +304,11 @@ module Sinatra
options[:layout] = false
render :sass, template, options, locals
end
def less(template, options={}, locals={})
options[:layout] = false
render :less, template, options, locals
end
def builder(template=nil, options={}, locals={}, &block)
options, template = template, nil if template.is_a?(Hash)

View File

@ -38,6 +38,7 @@ Gem::Specification.new do |s|
test/haml_test.rb
test/helper.rb
test/helpers_test.rb
test/less_test.rb
test/mapped_error_test.rb
test/middleware_test.rb
test/public/favicon.ico
@ -64,6 +65,7 @@ Gem::Specification.new do |s|
test/views/hello.haml
test/views/hello.sass
test/views/hello.test
test/views/hello.less
test/views/layout2.builder
test/views/layout2.erb
test/views/layout2.erubis
@ -81,6 +83,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'haml'
s.add_development_dependency 'builder'
s.add_development_dependency 'erubis'
s.add_development_dependency 'less'
s.has_rdoc = true
s.homepage = "http://sinatra.rubyforge.org"

37
test/less_test.rb Normal file
View File

@ -0,0 +1,37 @@
require File.dirname(__FILE__) + '/helper'
require 'less'
class LessTest < Test::Unit::TestCase
def less_app(&block)
mock_app {
set :views, File.dirname(__FILE__) + '/views'
get '/', &block
}
get '/'
end
it 'renders inline Less strings' do
less_app { less "@white_color: #fff; #main { background-color: @white_color }"}
assert ok?
assert_equal "#main { background-color: #ffffff; }\n", body
end
it 'renders .less files in views path' do
less_app { less :hello }
assert ok?
assert_equal "#main { background-color: #ffffff; }\n", body
end
it 'ignores the layout option' do
less_app { less :hello, :layout => :layout2 }
assert ok?
assert_equal "#main { background-color: #ffffff; }\n", body
end
it "raises error if template not found" do
mock_app {
get('/') { less :no_such_template }
}
assert_raise(Errno::ENOENT) { get('/') }
end
end

5
test/views/hello.less Normal file
View File

@ -0,0 +1,5 @@
@white_colour: #fff;
#main {
background-color: @white_colour;
}