Sha256: e53beffa86acbb5a6b97a9bef6811db4ba7ae58f82a536f8ee11052f27efb2d8

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module Liquid

  # Extends allows designer to use template inheritance
  #
  #   {% extends home %}
  #   {% block content }Hello world{% endblock %}
  #
  class Extends < Block
    Syntax = /(#{QuotedFragment}+)/

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

      @options = options

      @parent_template = parse_parent_template

      prepare_parsing

      super

      end_tag
    end

    def prepare_parsing
      @options.merge!(:blocks => self.find_blocks(@parent_template.root.nodelist))
    end

    def end_tag
      # replace the nodelist by the new one
      @nodelist = @parent_template.root.nodelist.clone

      @parent_template = nil # no need to keep it
    end

    def blank?
      false
    end

    protected

    def find_blocks(nodelist, blocks = {})
      if nodelist && nodelist.any?
        0.upto(nodelist.size - 1).each do |index|
          node = nodelist[index]

          if node.respond_to?(:call_super) # inherited block !
            new_node = node.class.clone_block(node)

            nodelist.insert(index, new_node)
            nodelist.delete_at(index + 1)

            blocks[node.name] = new_node
          end
          if node.respond_to?(:nodelist)
            self.find_blocks(node.nodelist, blocks) # FIXME: find nested blocks too
          end
        end
      end
      blocks
    end

    private

    def parse_parent_template
      source = Template.file_system.read_template_file(@template_name)
      Template.parse(source)
    end

    def assert_missing_delimitation!
    end
  end

  Template.register_tag('extends', Extends)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
locomotivecms-liquid-2.6.0 lib/liquid/tags/extends.rb