Sha256: f73d3c01425a1c260ad019b8898dc40f01c55e09529b0c0b45a02de93898f992

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

module Locomotive
  module Steam
    module Liquid
      module Tags

        class Snippet < ::Liquid::Include

          def parse(tokens)
            name = evaluate_snippet_name

            ActiveSupport::Notifications.instrument('steam.parse.include', page: options[:page], name: name)

            # look for editable elements
            if options[:snippet_finder] && snippet = options[:snippet_finder].find(name)
              options[:parser]._parse(snippet, options.merge(snippet: name))
            end
          end

          def render(context)
            @template_name = evaluate_snippet_name(context)
            # @options doesn't include the page key if cache is on
            @options[:page] = context.registers[:page]

            begin
              super
            rescue Locomotive::Steam::ParsingRenderingError => e
              e.file = @template_name + ' [Snippet]'
              raise e
            end
          end

          private

          def read_template_from_file_system(context)
            service = context.registers[:services]
            snippet = service.snippet_finder.find(@template_name)

            raise SnippetNotFound.new("Snippet with slug '#{@template_name}' was not found") if snippet.nil?

            snippet.liquid_source
          end

          def evaluate_snippet_name(context = nil)
            context.try(:evaluate, @template_name) ||
            (!@template_name.is_a?(String) && @template_name.send(:state).first) ||
            @template_name
          end

        end

        ::Liquid::Template.register_tag('include'.freeze, Snippet)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
locomotivecms_steam-1.5.0.rc0 lib/locomotive/steam/liquid/tags/snippet.rb
locomotivecms_steam-1.5.0.beta3 lib/locomotive/steam/liquid/tags/snippet.rb
locomotivecms_steam-1.5.0.beta2 lib/locomotive/steam/liquid/tags/snippet.rb
locomotivecms_steam-1.5.0.beta1 lib/locomotive/steam/liquid/tags/snippet.rb