Skip to content
Browse files

Added Less support

Signed-off-by: Simon Rozet <simon@rozet.name>
  • Loading branch information...
1 parent 4d61607 commit 621bfcbd6a863fdb048f875876e5be25f266e178 @Ptico Ptico committed with sr Mar 1, 2010
Showing with 65 additions and 1 deletion.
  1. +14 −0 README.rdoc
  2. +6 −1 lib/sinatra/base.rb
  3. +3 −0 sinatra.gemspec
  4. +37 −0 test/less_test.rb
  5. +5 −0 test/views/hello.less
View
14 README.rdoc
@@ -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
7 lib/sinatra/base.rb
@@ -284,7 +284,7 @@ def back ; request.referer ; end
#
# 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 @@ def sass(template, options={}, locals={})
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
3 sinatra.gemspec
@@ -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"
View
37 test/less_test.rb
@@ -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
View
5 test/views/hello.less
@@ -0,0 +1,5 @@
+@white_colour: #fff;
+
+#main {
+ background-color: @white_colour;
+}

0 comments on commit 621bfcb

Please sign in to comment.
Something went wrong with that request. Please try again.