Sha256: 5a13e9c9a6ccd71dab7b3d934a347921545f29eed47ef343a7c512066262430c

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

module Liquid

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

    def initialize(tag_name, markup, options)
      super

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

      # variables needed by the inheritance mechanism during the parsing
      options[:inherited_blocks] ||= {
        nested:   [], # used to get the full name of the blocks if nested (stack mechanism)
        all:      {}  # keep track of the blocks by their full name
      }
    end

    def parse(tokens)
      super

      parent_template = parse_parent_template

      # replace the nodes of the current template by those from the parent
      # which itself may have have done the same operation if it includes
      # the extends tag.
      nodelist.replace(parent_template.root.nodelist)
    end

    def blank?
      false
    end

    protected

    def parse_body(body, tokens)
      body.parse(tokens, options) do |end_tag_name, end_tag_params|
        @blank &&= body.blank?

        # Note: extends does not require the "end tag".
        return false if end_tag_name.nil?
      end

      true
    end

    def parse_parent_template
      source = Template.file_system.read_template_file(@template_name, {})
      Template.parse(source, options)
    end

  end

  Template.register_tag('extends', Extends)
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
locomotivecms-liquid-4.0.0 lib/liquid/tags/extends.rb
locomotivecms-liquid-4.0.0.alpha2 lib/liquid/tags/extends.rb
locomotivecms-liquid-4.0.0.alpha1 lib/liquid/tags/extends.rb
locomotivecms-liquid-4.0.0.alpha lib/liquid/tags/extends.rb