Class: Superfluid::Tags::Template

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/superfluid/tags/template.rb

Overview

Define custom templates within the context of the current template.

Constant Summary collapse

NAME_PATTERN =
%r(\A[0-9a-zA-Z_/]+\z)

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ Template

Returns a new instance of Template.



9
10
11
12
13
14
# File 'lib/superfluid/tags/template.rb', line 9

def initialize tag_name, markup, options
  super

  @name = markup.strip
  @content = +""
end

Instance Method Details

#parse(tokens) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/superfluid/tags/template.rb', line 16

def parse tokens
  content.clear

  while (token = tokens.shift)
    break if token.strip == "{% endtemplate %}"

    content << token
  end
end

#render(context) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/superfluid/tags/template.rb', line 26

def render context
  unless NAME_PATTERN.match? name
    return "Liquid error: Invalid template name #{name.inspect} - template names " \
           "must contain only letters, numbers, underscores, and forward slashes."
  end

  context.registers[:file_system].register name, content.strip
  ""
end