Class: Ruty::Tags::Block

Inherits:
Ruty::Tag show all
Defined in:
lib/ruty/tags/inheritance.rb

Overview

special tag that marks a part of a template for inheritance. If a template extends from a template with a block tag with the same name it will replace the block in the inherited template with the block with the same name in the current template.

Instance Method Summary collapse

Constructor Details

#initialize(parser, argstring) ⇒ Block

Returns a new instance of Block.


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruty/tags/inheritance.rb', line 16

def initialize parser, argstring
  if not argstring =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/
    parser.fail('Invalid syntax for block tag')
  end
  @name = argstring.to_sym
  @stack = [parser.parse_until { |name, a| name == :endblock }]

  blocks = (parser.storage[:blocks] ||= {})
  parser.fail("block '#{@name}' defined twice") if blocks.include?(@name)
  blocks[@name] = self
end

Instance Method Details

#add_layer(nodelist) ⇒ Object


28
29
30
# File 'lib/ruty/tags/inheritance.rb', line 28

def add_layer nodelist
  @stack << nodelist
end

#render_node(context, stream, index = -1) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruty/tags/inheritance.rb', line 32

def render_node context, stream, index=-1
  context.push
  context[:block] = Ruty::Datastructure::Deferred.new(
    :super =>     Proc.new {
      render_node(context, stream, index - 1) if index.abs <= @stack.size
    },
    :depth =>     Proc.new { index.abs },
    :name =>      Proc.new { @name }
  )
  @stack[index].render_node(context, stream)
  context.pop
  nil
end