Sha256: 064ad3f49babc0ec0dab44b338d9d39b51beefde566dbad5211e568e99dabade

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

module Liquid

  # Blocks are used with the Extends tag to define
  # the content of blocks. Nested blocks are allowed.
  #
  #   {% extends home %}
  #   {% block content }Hello world{% endblock %}
  #
  class InheritedBlock < Block
    Syntax = /(#{QuotedFragment}+)/

    attr_accessor :parent
    attr_reader :name

    def initialize(tag_name, markup, tokens, context)
      if markup =~ Syntax
        @name = $1
      else
        raise SyntaxError.new("Error in tag 'block' - Valid syntax: block [name]")
      end

      (context[:block_stack] ||= []).push(self)
      context[:current_block] = self

      super if tokens
    end

    def render(context)
      context.stack do
        context['block'] = InheritedBlockDrop.new(self)
        render_all(@nodelist, context)
      end
    end

    def end_tag
      self.register_current_block

      @context[:block_stack].pop
      @context[:current_block] = @context[:block_stack].last
    end

    def call_super(context)
      if parent
        parent.render(context)
      else
        ''
      end
    end

    def self.clone_block(block)
      new_block = self.new(block.send(:instance_variable_get, :"@tag_name"), block.name, nil, {})
      new_block.parent = block.parent
      new_block.nodelist = block.nodelist
      new_block
    end

    protected

    def register_current_block
      @context[:blocks] ||= {}

      block = @context[:blocks][@name]

      if block
        # copy the existing block in order to make it a parent of the parsed block
        new_block = self.class.clone_block(block)

        # replace the up-to-date version of the block in the parent template
        block.parent = new_block
        block.nodelist = @nodelist
      end
    end

  end

  class InheritedBlockDrop < Drop

    def initialize(block)
      @block = block
    end

    def name
      @block.name
    end

    def super
      @block.call_super(@context)
    end

  end

  Template.register_tag('block', InheritedBlock)
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
locomotive_liquid-2.4.1 lib/liquid/tags/inherited_block.rb
locomotive_liquid-2.2.3 lib/liquid/tags/inherited_block.rb
locomotive_liquid-2.2.2 lib/liquid/tags/inherited_block.rb
locomotive_liquid-2.1.3 lib/liquid/tags/inherited_block.rb