Sha256: d2aa9716d0f144a209e3a05baf8ce47b06d0f86ce0667b3a0eb7dee3f82d5869

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

module Locomotive
  module Steam
    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 characters 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_instance'] || 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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
locomotivecms_steam-0.1.2.pre.beta lib/locomotive/steam/liquid/tags/seo.rb
locomotivecms_steam-0.1.1 lib/locomotive/steam/liquid/tags/seo.rb
locomotivecms_steam-0.1.0 lib/locomotive/steam/liquid/tags/seo.rb