Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add stylus template support

  • Loading branch information...
commit b18310f73635c063410f53885c74c08f3b5bb273 1 parent 91bce0b
Juan David Pastas authored
View
6 README.es.rdoc
@@ -536,6 +536,12 @@ Dependencias:: {coffee-script}[https://github.com/josh/ruby-coffee-s
Extensiones de Archivo:: <tt>.coffee</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
Dependencias:: {yajl-ruby}[https://github.com/brianmario/yajl-ruby]
View
6 README.rdoc
@@ -520,6 +520,12 @@ Dependency:: {coffee-script}[https://github.com/josh/ruby-coffee-script]
File Extensions:: <tt>.coffee</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
Dependency:: {yajl-ruby}[https://github.com/brianmario/yajl-ruby]
View
5 lib/sinatra/base.rb
@@ -598,6 +598,11 @@ def less(template, options={}, locals={})
render :less, template, options, locals
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)
options[:default_content_type] = :xml
render_ruby(:builder, template, options, locals, &block)
View
90 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
View
2  test/views/hello.styl
@@ -0,0 +1,2 @@
+a
+ margin auto
Please sign in to comment.
Something went wrong with that request. Please try again.