Class: Liquid2::BlockTag

Inherits:
Tag
  • Object
show all
Defined in:
lib/liquid2/nodes/tags/extends.rb

Overview

The block tag.

Constant Summary collapse

END_BLOCK =

Instance Attribute Summary collapse

Attributes inherited from Node

#blank, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tag

#name

Methods inherited from Node

#expressions, #partial_scope, #render_with_disabled_tag_check, #template_scope

Constructor Details

#initialize(token, name, block, required:) ⇒ BlockTag

Returns a new instance of BlockTag.



177
178
179
180
181
182
183
# File 'lib/liquid2/nodes/tags/extends.rb', line 177

def initialize(token, name, block, required:)
  super(token)
  @block_name = name
  @block = block
  @required = required
  @blank = false
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



154
155
156
# File 'lib/liquid2/nodes/tags/extends.rb', line 154

def block
  @block
end

#block_nameObject (readonly)

Returns the value of attribute block_name.



154
155
156
# File 'lib/liquid2/nodes/tags/extends.rb', line 154

def block_name
  @block_name
end

#requiredObject (readonly)

Returns the value of attribute required.



154
155
156
# File 'lib/liquid2/nodes/tags/extends.rb', line 154

def required
  @required
end

Class Method Details

.parse(token, parser) ⇒ BlockTag

Parameters:

  • token ([Symbol, String?, Integer])
  • parser (Parser)

Returns:



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/liquid2/nodes/tags/extends.rb', line 161

def self.parse(token, parser)
  block_name = parser.parse_name
  required = if parser.current_kind == :token_required
               parser.next
               true
             else
               false
             end

  parser.carry_whitespace_control
  parser.eat(:token_tag_end)
  block = parser.parse_block(END_BLOCK)
  parser.eat_empty_tag("endblock")
  new(token, block_name, block, required: required)
end

Instance Method Details

#block_scopeObject



230
231
232
# File 'lib/liquid2/nodes/tags/extends.rb', line 230

def block_scope
  [Identifier.new([:token_word, "block", @token.last])]
end

#children(_static_context, include_partials: true) ⇒ Object



226
227
228
# File 'lib/liquid2/nodes/tags/extends.rb', line 226

def children(_static_context, include_partials: true)
  [@block]
end

#render(context, buffer) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/liquid2/nodes/tags/extends.rb', line 185

def render(context, buffer)
  # @type var stack: Array[[BlockTag, bool, Template, untyped?]]
  stack = context.tag_namespace[:extends][@block_name]

  if stack.empty?
    # This base block is being rendered directly.
    if @required
      raise RequiredBlockError.new("block #{@block_name.inspect} is required",
                                   @token)
    end

    context.extend({ "block" => BlockDrop.new(token, context, @block_name, nil) }) do
      @block.render(context, buffer)
    end

    return
  end

  block_tag, required, template, parent = stack.first

  if required
    raise RequiredBlockError.new("block #{@block_name.inspect} is required", @token,
                                 template_name: template.path || template.name)
  end

  namespace = { "block" => BlockDrop.new(token, context, @block_name, parent) }

  block_context = context.copy(namespace,
                               carry_loop_iterations: true,
                               block_scope: true,
                               template: template)

  begin
    block_tag.block.render(block_context, buffer)
  rescue LiquidError => e
    e.template_name = template.path || template.name
    e.source = template.source
    raise
  end
end