Class: Stasis::Layout
Instance Method Summary collapse
-
#before_render ⇒ Object
This event triggers before each file renders through Stasis.
-
#initialize(stasis) ⇒ Layout
constructor
A new instance of Layout.
-
#layout_action(path) ⇒ Object
This method is bound to all actions.
-
#layout_controller(hash_or_string) ⇒ Object
This method is bound to all controllers.
-
#reset ⇒ Object
This event resets all instance variables.
Methods inherited from Plugin
#_match_key?, _priority, #_within?, inherited, plugins, priority
Constructor Details
#initialize(stasis) ⇒ Layout
9 10 11 12 |
# File 'lib/stasis/plugins/layout.rb', line 9 def initialize(stasis) @stasis = stasis reset end |
Instance Method Details
#before_render ⇒ Object
This event triggers before each file renders through Stasis. It sets the action layout from the matching layout for path.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/stasis/plugins/layout.rb', line 16 def before_render return unless @stasis.path @stasis.action._layout = nil matches = _match_key?(@layouts, @stasis.path) # Non-HTML extensions. non_html = %w(sass scss less builder coffee yajl) # Find matching layout. [ :same, :similar ].each do |type| matches.each do |(within, layout, non_specific)| layout_ext = File.extname(layout)[1..-1] path_ext = File.extname(@stasis.path)[1..-1] match = case type # Same extension? when :same then layout_ext == path_ext # Similar extension? when :similar then non_html.include?(layout_ext) == non_html.include?(path_ext) end # Set layout if _within?(within) && match @stasis.action._layout = layout end end break if @stasis.action._layout end # If layout not found, try again without extension requirement for specific layout # definitions only. unless @stasis.action._layout matches.each do |(within, layout, non_specific)| if _within?(within) && !non_specific @stasis.action._layout = layout end end end end |
#layout_action(path) ⇒ Object
This method is bound to all actions. Set the action layout.
57 58 59 60 61 |
# File 'lib/stasis/plugins/layout.rb', line 57 def layout_action(path) if path = @stasis.controller._resolve(path) @stasis.action._layout = path end end |
#layout_controller(hash_or_string) ⇒ Object
This method is bound to all controllers. If it receives a String as a parameter, use that layout for all paths. Otherwise, it receives a Hash with the key being the path and the value being the layout to use for that path.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/stasis/plugins/layout.rb', line 66 def layout_controller(hash_or_string) if hash_or_string.is_a?(::String) hash = {} hash[/.*/] = hash_or_string else hash = hash_or_string end @layouts.merge! hash.inject({}) { |hash, (path, layout)| path = @stasis.controller._resolve(path) layout = @stasis.controller._resolve(layout) if layout hash[path] = [ @stasis.path, layout, path == /.*/ ] @stasis.controller.ignore(layout) end hash } end |
#reset ⇒ Object
This event resets all instance variables.
85 86 87 |
# File 'lib/stasis/plugins/layout.rb', line 85 def reset @layouts = {} end |