Sha256: e64e3414ae38ef68d0aa0f75de485019d4c749e95d5dcad34b78cdd31bd0a462

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Locomotive
  module Steam
    module Liquid
      module Tags
        class LinkTo < Hybrid

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

          include PathHelper

          def initialize(tag_name, markup, tokens, options)
            if markup =~ Syntax
              @handle = $1
              @_options = {}
              markup.scan(::Liquid::TagAttributes) do |key, value|
                @_options[key] = value
              end
            else
              raise ::Liquid::SyntaxError.new(options[:locale].t("errors.syntax.link_to"), options[:line])
            end

            super
          end

          def render(context)
            render_path(context) do |page, path|
              label = label_from_page(page)

              if @render_as_block
                context.scopes.last['target'] = page
                label = super.html_safe
              end

              %{<a href="#{path}">#{label}</a>}
            end
          end

          protected

          def label_from_page(page)
            ::Locomotive::Mounter.with_locale(@_options['locale']) do
              if page.templatized?
                page.content_entry._label
              else
                page.title
              end
            end
          end

        end

        ::Liquid::Template.register_tag('link_to', LinkTo)
      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/link_to.rb
locomotivecms_steam-0.1.1 lib/locomotive/steam/liquid/tags/link_to.rb
locomotivecms_steam-0.1.0 lib/locomotive/steam/liquid/tags/link_to.rb