Class: Layout
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- Layout
- Includes:
- Liquid::Tag::Disableable
- Defined in:
- lib/tags/layout.rb
Constant Summary collapse
- SYNTAX =
/(#{Liquid::QuotedFragment}+)/o.freeze
- @@layout_cache =
{}
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#layout_name_expr ⇒ Object
readonly
Returns the value of attribute layout_name_expr.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(tag_name, markup, tokens) ⇒ Layout
constructor
A new instance of Layout.
- #layout_path ⇒ Object
- #load_layout(layout_name) ⇒ Object
- #parse(tokens) ⇒ Object
- #render_to_output_buffer(context, output) ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ Layout
Returns a new instance of Layout.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tags/layout.rb', line 24 def initialize(tag_name, markup, tokens) super @page_content = [] raise LayoutError, 'Invalid layout syntax' unless markup =~ SYNTAX layout_name = Regexp.last_match(1) @layout_name_expr = parse_expression(layout_name) @attributes = {} markup.scan(Liquid::TagAttributes) do |key, value| @attributes[key] = parse_expression(value) end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
18 19 20 |
# File 'lib/tags/layout.rb', line 18 def attributes @attributes end |
#layout_name_expr ⇒ Object (readonly)
Returns the value of attribute layout_name_expr.
18 19 20 |
# File 'lib/tags/layout.rb', line 18 def layout_name_expr @layout_name_expr end |
Class Method Details
.reset_cache ⇒ Object
20 21 22 |
# File 'lib/tags/layout.rb', line 20 def self.reset_cache @@layout_cache = {} end |
Instance Method Details
#layout_path ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tags/layout.rb', line 39 def layout_path base_path = Dir.pwd layout_dir = 'layout' layout_path = File.join(base_path, layout_dir) unless File.exist? layout_path raise LayoutError, "Layout dir #{layout_path} not found" end layout_path end |
#load_layout(layout_name) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/tags/layout.rb', line 50 def load_layout(layout_name) return @@layout_cache[layout_name] if @@layout_cache[layout_name] found_layout = nil Dir.entries(layout_path).each do |f| next unless File.file? File.join( layout_path, f ) next unless File.basename(f, '.*') == layout_name if found_layout raise LayoutError, "More than one layout named #{layout_name} found." end found_layout = File.join(layout_path, f) end if found_layout.nil? raise LayoutError, "No layouts named #{layout_name} found." end layout = File.read(found_layout) @@layout_cache[layout_name] = Liquid::Template.parse(layout) @@layout_cache[layout_name] end |
#parse(tokens) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/tags/layout.rb', line 76 def parse(tokens) t = tokens.shift while t @page_content.push t t = tokens.shift end end |
#render_to_output_buffer(context, output) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tags/layout.rb', line 84 def render_to_output_buffer(context, output) layout_name = context.evaluate(@layout_name_expr) raise LayoutError, 'Bad layout name argument' unless layout_name layout = load_layout(layout_name) old_template_name = context.template_name old_partial = context.partial begin context.template_name = layout_name context.partial = true context.stack do @attributes.each do |key, value| context[key] = context.evaluate(value) end rendered_page = Liquid::Template.parse(@page_content.join) .render(context) context['page_content'] = rendered_page layout.render_to_output_buffer(context, output) end ensure context.template_name = old_template_name context.partial = old_partial end output end |