Class: Liquidbook::Tags::SectionTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/liquidbook/tags/section_tag.rb

Overview

Handles section ‘name’ % tags by rendering the referenced section file

Constant Summary collapse

SYNTAX =
/['"](\w+)['"]/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ SectionTag

Returns a new instance of SectionTag.



9
10
11
12
13
14
15
16
# File 'lib/liquidbook/tags/section_tag.rb', line 9

def initialize(tag_name, markup, tokens)
  super
  if markup.strip =~ SYNTAX
    @section_name = Regexp.last_match(1)
  else
    raise Liquid::SyntaxError, "Invalid syntax for section tag: #{markup}"
  end
end

Instance Method Details

#render(context) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/liquidbook/tags/section_tag.rb', line 18

def render(context)
  theme_root = context.registers[:theme_root] || Liquidbook.root
  section_path = File.join(theme_root, "sections", "#{@section_name}.liquid")

  unless File.exist?(section_path)
    return "<!-- section '#{@section_name}' not found -->"
  end

  source = File.read(section_path)
  parser = SchemaParser.new(source)
  template = Liquid::Template.parse(parser.template_without_schema, environment: Liquidbook.environment)
  template.render(context)
end