Module: Sinatra::FancyHelpers

Defined in:
lib/sinatra/fancyviews.rb

Instance Method Summary collapse

Instance Method Details

#fancyviewsObject



108
109
110
# File 'lib/sinatra/fancyviews.rb', line 108

def fancyviews
  @fancyviews ||= FancyViews.new(self)
end

#haml_formatObject



104
105
106
# File 'lib/sinatra/fancyviews.rb', line 104

def haml_format
  (options.haml && options.haml[:format]) || :xhtml
end

#page(name, options = {}) ⇒ Object

same as ‘view` but has layout by default



66
67
68
69
# File 'lib/sinatra/fancyviews.rb', line 66

def page(name, options = {})
  options[:layout] = true unless options.has_key?(:layout)
  view(name, options)
end

#script_tag(scripts) ⇒ Object



98
99
100
101
102
# File 'lib/sinatra/fancyviews.rb', line 98

def script_tag(scripts)
  capture_haml do
    haml_tag(:script, scripts, :type => ("text/javascript" if haml_format != :html5))
  end.strip
end

#scriptsObject

renders all the scripts captured by the :script filter



92
93
94
95
96
# File 'lib/sinatra/fancyviews.rb', line 92

def scripts
  script_tag(fancyviews.included_scripts.map do |name, js|
    "\n/* -- #{name} -- */\n" + js
  end.join)
end

#style_tag(styles, media = nil) ⇒ Object



85
86
87
88
89
# File 'lib/sinatra/fancyviews.rb', line 85

def style_tag(styles, media=nil)
  capture_haml do
    haml_tag(:style, styles, :type => ("text/css" if haml_format != :html5), :media => media)
  end.strip
end

#styles(options = {}) ⇒ Object

renders all the styles captured by the :style filter



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sinatra/fancyviews.rb', line 72

def styles(options = {})
  imported = options.has_key?(:import) ?
    File.read("#{self.options.views}/#{options[:import]}.sass") : ''

  rendered_styles = fancyviews.included_styles.map do |name, sass| 
    # would be nice if construction took an :offest to go along with the :filename
    eng = Sass::Engine.new(imported + "\n" + sass, :attribute_syntax => :normal)
    "\n/* -- #{name} -- */\n" + eng.render
  end.join

  style_tag(rendered_styles, options[:media])
end

#view(name, options = {}) ⇒ Object

use this instead of ‘haml` when rendering a template, unlike the haml method, layout is false by default



56
57
58
59
60
61
62
63
# File 'lib/sinatra/fancyviews.rb', line 56

def view(name, options = {})
  parent_view = fancyviews.view_name.to_s
  fancyviews.view_name = name.to_s
  options[:layout] = false unless options.has_key?(:layout)
  rendered = haml(name, options)
  fancyviews.view_name = parent_view
  rendered
end