From 621bfcbd6a863fdb048f875876e5be25f266e178 Mon Sep 17 00:00:00 2001 From: Andrey Savchenko Date: Mon, 1 Mar 2010 14:13:10 +0200 Subject: [PATCH] Added Less support Signed-off-by: Simon Rozet --- README.rdoc | 14 ++++++++++++++ lib/sinatra/base.rb | 7 ++++++- sinatra.gemspec | 3 +++ test/less_test.rb | 37 +++++++++++++++++++++++++++++++++++++ test/views/hello.less | 5 +++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/less_test.rb create mode 100644 test/views/hello.less diff --git a/README.rdoc b/README.rdoc index 41141db6..8422916b 100644 --- a/README.rdoc +++ b/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 ./views/stylesheet.less. + === Inline Templates get '/' do diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index fee222b8..f027a960 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -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) diff --git a/sinatra.gemspec b/sinatra.gemspec index 9804c57d..427083ed 100644 --- a/sinatra.gemspec +++ b/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" diff --git a/test/less_test.rb b/test/less_test.rb new file mode 100644 index 00000000..9ce9aa9b --- /dev/null +++ b/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 diff --git a/test/views/hello.less b/test/views/hello.less new file mode 100644 index 00000000..9eb99ddc --- /dev/null +++ b/test/views/hello.less @@ -0,0 +1,5 @@ +@white_colour: #fff; + +#main { + background-color: @white_colour; +} \ No newline at end of file