Module: Sinatra::FancyHelpers

Defined in:
lib/sinatra/fancyviews.rb

Instance Method Summary collapse

Instance Method Details

#fancyviewsObject



110
111
112
# File 'lib/sinatra/fancyviews.rb', line 110

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

#haml_formatObject



106
107
108
# File 'lib/sinatra/fancyviews.rb', line 106

def haml_format
  (options.member?(: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



100
101
102
103
104
# File 'lib/sinatra/fancyviews.rb', line 100

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



94
95
96
97
98
# File 'lib/sinatra/fancyviews.rb', line 94

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

#style_tag(styles, media = nil) ⇒ Object



87
88
89
90
91
# File 'lib/sinatra/fancyviews.rb', line 87

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
84
85
# File 'lib/sinatra/fancyviews.rb', line 72

def styles(options = {})
  imported = if options[:import]
    [*options[:import]].map { |name| File.read("#{self.options.views}/#{name}.sass") }.join("\n")
  end

  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,
                           :load_paths => [self.options.views])
    "\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