Sha256: a9fd7e4cde212c7ca001b55f28b9446c992a1922d857cf132f36064308f976bb

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

module Locomotive
  module Steam
    module Liquid
      module Tags

        class Snippet < ::Liquid::Include

          def render(context)
            name    = @template_name.gsub(/[\"\']/, '')
            snippet = context.registers[:mounting_point].snippets[name]

            raise ::Liquid::StandardError.new("Unknown snippet \"#{name}\"") if snippet.nil?

            partial   = self.parse_template(snippet)

            variable  = context[@variable_name || @template_name[1..-2]]

            context.stack do
              @attributes.each do |key, value|
                context[key] = context[value]
              end

              output = (if variable.is_a?(Array)
                variable.collect do |variable|
                  context[@template_name[1..-2]] = variable
                  partial.render(context)
                end
              else
                context[@template_name[1..-2]] = variable
                partial.render(context)
              end)

              Locomotive::Common::Logger.info "  Steamed snippet #{name}"

              output
            end
          end

          protected

          def parse_template(snippet)
            begin
              ::Liquid::Template.parse(snippet.source)
            rescue ::Liquid::Error => e
              # do it again on the raw source instead so that the error line matches
              # the source file.
              begin
                ::Liquid::Template.parse(snippet.template.raw_source)
              rescue ::Liquid::Error => e
                e.backtrace.unshift "#{snippet.template.filepath}:#{e.line + 1}:in `#{snippet.name}'"
                e.line = self.line - 1
                raise e
              end
            end
          end

        end

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

Version data entries

2 entries across 2 versions & 1 rubygems

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