Class: Liquid::InheritedBlock
- 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
-
#descendant ⇒ Object
linked chain of inherited blocks included in different templates if multiple extends.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
linked chain of inherited blocks included in different templates if multiple extends.
Attributes inherited from Tag
#line_number, #nodelist, #options, #warnings
Instance Method Summary collapse
- #call_super(context) ⇒ Object
- #inherited_blocks ⇒ Object
-
#initialize(tag_name, markup, options) ⇒ InheritedBlock
constructor
A new instance of InheritedBlock.
- #parse(tokens) ⇒ Object
- #prepare_for_inheritance ⇒ Object
- #render(context) ⇒ Object
- #render_without_inheritance ⇒ Object
-
#self_or_first_descendant ⇒ Object
when we render an inherited block, we need the version of the very first descendant.
Methods inherited from Block
#blank?, #block_delimiter, #block_name, #nodelist, #unknown_tag, #warnings
Methods inherited from Tag
Methods included from ParserSwitching
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, ) super if markup =~ Syntax @name = $1.gsub(/["']/o, '').strip else raise(SyntaxError.new([:locale].t("errors.syntax.block")), [:line]) end prepare_for_inheritance end |
Instance Attribute Details
#descendant ⇒ Object
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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/liquid/tags/inherited_block.rb', line 12 def name @name end |
#parent ⇒ Object
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_blocks ⇒ Object
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 [: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_inheritance ⇒ Object
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_inheritance ⇒ Object
61 |
# File 'lib/liquid/tags/inherited_block.rb', line 61 alias_method :render_without_inheritance, :render |
#self_or_first_descendant ⇒ Object
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 |