Sha256: c8f5850e3ac98b6580013a4d747c3ceebb1d9676a0f175a635fecf2b144ed5d7

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module Locomotive
  module Liquid
    module Tags
      module Editable
        class Content < ::Liquid::Tag

          Syntax = /(#{::Liquid::Expression}+)?/

          def initialize(tag_name, markup, tokens, context)
            if markup =~ Syntax
              @slug = $1
              @options = { :inherit => false }
              markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
            else
              raise ::Liquid::SyntaxError.new("Syntax Error in 'content' - Valid syntax: slug")
            end

            super
          end

          def render(context)
            page = context.registers[:page]
            element = find_element(page)

            if element.nil? && @options[:inherit] != false
              while page.parent.present? && element.nil?
                page = page.parent
                element = find_element(page)
              end
            end

            if element.present?
              return element.content
            else
              raise ::Liquid::SyntaxError.new("Error in 'content' - Can't find editable element called `#{@slug}`")
            end
          end

          def find_element(page)
            page.editable_elements.where(:slug => @slug).first
          end

        end

        ::Liquid::Template.register_tag('content', Content)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotive_cms-2.0.0.rc2 lib/locomotive/liquid/tags/editable/content.rb
locomotive_cms-2.0.0.rc1 lib/locomotive/liquid/tags/editable/content.rb