1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

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 sass :stylesheet, :style => :expanded # overridden
end 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 === Inline Templates
get '/' do get '/' do

View file

@ -284,7 +284,7 @@ module Sinatra
# #
# Possible options are: # Possible options are:
# :layout If set to false, no layout is rendered, otherwise # :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 # :locals A hash with local variables that should be available
# in the template # in the template
module Templates module Templates
@ -305,6 +305,11 @@ module Sinatra
render :sass, template, options, locals render :sass, template, options, locals
end end
def less(template, options={}, locals={})
options[:layout] = false
render :less, template, options, locals
end
def builder(template=nil, options={}, locals={}, &block) def builder(template=nil, options={}, locals={}, &block)
options, template = template, nil if template.is_a?(Hash) options, template = template, nil if template.is_a?(Hash)
template = Proc.new { block } if template.nil? template = Proc.new { block } if template.nil?

View file

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