Class: Liquid::InheritedBlock

Inherits:
Block
  • Object
show all
Defined in:
lib/liquid/tags/inherited_block.rb

Overview

Blocks are used with the Extends tag to define the content of blocks. Nested blocks are allowed.

{% extends home %}
{% block content }Hello world{% endblock %}

Constant Summary collapse

Syntax =
/(#{QuotedFragment}+)/o

Instance Attribute Summary collapse

Attributes inherited from Tag

#line_number, #nodelist, #options, #warnings

Instance Method Summary collapse

Methods inherited from Block

#blank?, #block_delimiter, #block_name, #nodelist, #unknown_tag, #warnings

Methods inherited from Tag

#blank?, parse, #raw

Methods included from ParserSwitching

#parse_with_selected_parser

Constructor Details

#initialize(tag_name, markup, options) ⇒ InheritedBlock

Returns a new instance of InheritedBlock.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/liquid/tags/inherited_block.rb', line 18

def initialize(tag_name, markup, options)
  super

  if markup =~ Syntax
    @name = $1.gsub(/["']/o, '').strip
  else
    raise(SyntaxError.new(options[:locale].t("errors.syntax.block")), options[:line])
  end

  prepare_for_inheritance
end

Instance Attribute Details

#descendantObject

linked chain of inherited blocks included in different templates if multiple extends



16
17
18
# File 'lib/liquid/tags/inherited_block.rb', line 16

def descendant
  @descendant
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/liquid/tags/inherited_block.rb', line 12

def name
  @name
end

#parentObject

linked chain of inherited blocks included in different templates if multiple extends



16
17
18
# File 'lib/liquid/tags/inherited_block.rb', line 16

def parent
  @parent
end

Instance Method Details

#call_super(context) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/liquid/tags/inherited_block.rb', line 83

def call_super(context)
  if parent
    # remove the block from the linked chain
    parent.descendant = nil

    parent.render(context)
  else
    ''
  end
end

#inherited_blocksObject



94
95
96
97
98
99
100
# File 'lib/liquid/tags/inherited_block.rb', line 94

def inherited_blocks
  # initialize it in the case the template does not include an extend tag
  options[:inherited_blocks] ||= {
    all:    {},
    nested: []
  }
end

#parse(tokens) ⇒ Object



54
55
56
57
58
59
# File 'lib/liquid/tags/inherited_block.rb', line 54

def parse(tokens)
  super

  # when the parsing of the block is done, we can then remove it from the stack
  inherited_blocks[:nested].pop
end

#prepare_for_inheritanceObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/liquid/tags/inherited_block.rb', line 30

def prepare_for_inheritance
  # give a different name if this is a nested block
  if block = inherited_blocks[:nested].last
    @name = "#{block.name}/#{@name}"
  end

  # append this block to the stack in order to
  # get a name for the other nested inherited blocks
  inherited_blocks[:nested].push(self)

  # build the linked chain of inherited blocks
  # make a link with the descendant and the parent (chained list)
  if descendant = inherited_blocks[:all][@name]
    self.descendant   = descendant
    descendant.parent = self

    # get the value of the blank property from the descendant
    @blank = descendant.blank?
  end

  # become the descendant of the inherited block from the parent template
  inherited_blocks[:all][@name] = self
end

#render(context) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/liquid/tags/inherited_block.rb', line 63

def render(context)
  context.stack do
    # look for the very first descendant
    block = self_or_first_descendant

    # the block drop is in charge of rendering "{{ block.super }}"
    context['block'] = InheritedBlockDrop.new(block)

    block.render_without_inheritance(context)
  end
end

#render_without_inheritanceObject



61
# File 'lib/liquid/tags/inherited_block.rb', line 61

alias_method :render_without_inheritance, :render

#self_or_first_descendantObject

when we render an inherited block, we need the version of the very first descendant.



77
78
79
80
81
# File 'lib/liquid/tags/inherited_block.rb', line 77

def self_or_first_descendant
  block = self
  while block.descendant; block = block.descendant; end
  block
end