Skip to content

Commit

Permalink
add stylus template support
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan David Pastas committed Jul 30, 2012
1 parent 91bce0b commit b18310f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.es.rdoc
Expand Up @@ -536,6 +536,12 @@ Dependencias:: {coffee-script}[https://github.com/josh/ruby-coffee-s
Extensiones de Archivo:: <tt>.coffee</tt> Extensiones de Archivo:: <tt>.coffee</tt>
Ejemplo:: <tt>coffee :index</tt> Ejemplo:: <tt>coffee :index</tt>


=== Plantillas Stylus

Dependencias:: {ruby-stylus}[https://github.com/lucasmazza/ruby-stylus]
Extensiones de Archivo:: <tt>.styl</tt>
Ejemplo:: <tt>stylus :index</tt>

=== Plantillas Yajl === Plantillas Yajl


Dependencias:: {yajl-ruby}[https://github.com/brianmario/yajl-ruby] Dependencias:: {yajl-ruby}[https://github.com/brianmario/yajl-ruby]
Expand Down
6 changes: 6 additions & 0 deletions README.rdoc
Expand Up @@ -520,6 +520,12 @@ Dependency:: {coffee-script}[https://github.com/josh/ruby-coffee-script]
File Extensions:: <tt>.coffee</tt> File Extensions:: <tt>.coffee</tt>
Example:: <tt>coffee :index</tt> Example:: <tt>coffee :index</tt>


=== Stylus Templates

Dependency:: {ruby-stylus}[https://github.com/lucasmazza/ruby-stylus]
File Extensions:: <tt>.styl</tt>
Example:: <tt>stylus :index</tt>

=== Yajl Templates === Yajl Templates


Dependency:: {yajl-ruby}[https://github.com/brianmario/yajl-ruby] Dependency:: {yajl-ruby}[https://github.com/brianmario/yajl-ruby]
Expand Down
5 changes: 5 additions & 0 deletions lib/sinatra/base.rb
Expand Up @@ -598,6 +598,11 @@ def less(template, options={}, locals={})
render :less, template, options, locals render :less, template, options, locals
end end


def stylus(template, options={}, locals={})
options.merge! :layout => false, :default_content_type => :css
render :styl, template, options, locals
end

def builder(template=nil, options={}, locals={}, &block) def builder(template=nil, options={}, locals={}, &block)
options[:default_content_type] = :xml options[:default_content_type] = :xml
render_ruby(:builder, template, options, locals, &block) render_ruby(:builder, template, options, locals, &block)
Expand Down
90 changes: 90 additions & 0 deletions test/stylus_test.rb
@@ -0,0 +1,90 @@
require File.expand_path('../helper', __FILE__)

begin
require 'stylus'

begin
Stylus.compile '1'
rescue Exception
raise LoadError, 'unable to find Stylus compiler'
end

class StylusTest < Test::Unit::TestCase
def stylus_app(options = {}, &block)
mock_app do
set :views, File.dirname(__FILE__) + '/views'
set(options)
get('/', &block)
end
get '/'
end

it 'renders inline Stylus strings' do
assert false, 'waiting response on how stylus can be rendered inline'
stylus_app { stylus "a margin auto\n" }
assert ok?
assert body.include?("a {\n margin: auto;\n}\n")
end

it 'defaults content type to css' do
stylus_app { stylus :hello }
assert ok?
assert_equal "text/css;charset=utf-8", response['Content-Type']
end

it 'defaults allows setting content type per route' do
stylus_app do
content_type :html
stylus :hello
end
assert ok?
assert_equal "text/html;charset=utf-8", response['Content-Type']
end

it 'defaults allows setting content type globally' do
stylus_app(:styl => { :content_type => 'html' }) do
stylus :hello
end
assert ok?
assert_equal "text/html;charset=utf-8", response['Content-Type']
end

it 'renders .styl files in views path' do
stylus_app { stylus :hello }
assert ok?
assert_include body, "a {\n margin: auto;\n}\n"
end

it 'ignores the layout option' do
stylus_app { stylus :hello, :layout => :layout2 }
assert ok?
assert_include body, "a {\n margin: auto;\n}\n"
end

it "raises error if template not found" do
mock_app {
get('/') { stylus :no_such_template }
}
assert_raise(Errno::ENOENT) { get('/') }
end

it "passes stylus options to the stylus engine" do
stylus_app { stylus :hello, :no_wrap => true }
assert ok?
assert_body "a {\n margin: auto;\n}\n"
end

it "passes default stylus options to the stylus engine" do
mock_app do
set :stylus, :no_wrap => true # default stylus style is :nested
get('/') { stylus :hello }
end
get '/'
assert ok?
assert_body "a {\n margin: auto;\n}\n"
end
end

rescue LoadError
warn "#{$!.to_s}: skipping stylus tests"
end
2 changes: 2 additions & 0 deletions test/views/hello.styl
@@ -0,0 +1,2 @@
a
margin auto

0 comments on commit b18310f

Please sign in to comment.