Sha256: 9557a799306796f5c95c26dfb67c11da1839ac39fdb06a1bcb983ea68f0b062d

Contents?: true

Size: 1.82 KB

Versions: 11

Compression:

Stored size: 1.82 KB

Contents

module Locomotive
  module Liquid
    module Tags
      module SEO

        class Base < ::Liquid::Tag

          def render(context)
            %{
              #{self.render_title(context)}
              #{self.render_metadata(context)}
            }
          end

          protected

          def render_title(context)
            title = self.value_for(:seo_title, context)
            title = context.registers[:site].name if title.blank?

            %{
              <title>#{title}</title>
            }
          end

          def render_metadata(context)
            %{
              <meta name="description" content="#{self.value_for(:meta_description, context)}" />
              <meta name="keywords" content="#{self.value_for(:meta_keywords, context)}" />
            }
          end

          # Removes whitespace and quote charactets from the input
          def sanitized_string(string)
            string ? string.strip.gsub(/"/, '') : ''
          end

          def value_for(attribute, context)
            object = self.metadata_object(context)
            value = object.try(attribute.to_sym).blank? ? context.registers[:site].send(attribute.to_sym) : object.send(attribute.to_sym)
            self.sanitized_string(value)
          end

          def metadata_object(context)
            context['content_entry'] || context['page']
          end
        end

        class Title < Base

          def render(context)
            self.render_title(context)
          end

        end

        class Metadata < Base

          def render(context)
            self.render_metadata(context)
          end

        end

      end

      ::Liquid::Template.register_tag('seo', SEO::Base)
      ::Liquid::Template.register_tag('seo_title', SEO::Title)
      ::Liquid::Template.register_tag('seo_metadata', SEO::Metadata)
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
locomotive_cms-2.0.0.rc12 lib/locomotive/liquid/tags/seo.rb
locomotive_cms-2.0.0.rc11 lib/locomotive/liquid/tags/seo.rb
locomotive_cms-2.0.0.rc10 lib/locomotive/liquid/tags/seo.rb
locomotive_cms-2.0.0.rc9 lib/locomotive/liquid/tags/seo.rb
locomotive_cms-2.0.0.rc8 lib/locomotive/liquid/tags/seo.rb
locomotive_cms-2.0.0.rc7 lib/locomotive/liquid/tags/seo.rb
locomotive_cms-2.0.0.rc6 lib/locomotive/liquid/tags/seo.rb
locomotive_cms-2.0.0.rc5 lib/locomotive/liquid/tags/seo.rb
locomotive_cms-2.0.0.rc4 lib/locomotive/liquid/tags/seo.rb
locomotive_cms-2.0.0.rc2 lib/locomotive/liquid/tags/seo.rb
locomotive_cms-2.0.0.rc1 lib/locomotive/liquid/tags/seo.rb