Sha256: eea3ec16a6d1bc944119f2bcc0a146dd74ccf2d3d2f77d01164ed8dd7c901f0d

Contents?: true

Size: 1.81 KB

Versions: 15

Compression:

Stored size: 1.81 KB

Contents

module Locomotive
  module Wagon
    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::Wagon::Logger.info "  Rendered 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

15 entries across 15 versions & 1 rubygems

Version Path
locomotivecms_wagon-1.5.8 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.5.7 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.5.6 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.5.5 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.5.4 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.5.3 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.5.2 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.5.1 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.5.0 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.5.0.rc1 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.4.0 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.3.3 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.3.2 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.3.1 lib/locomotive/wagon/liquid/tags/snippet.rb
locomotivecms_wagon-1.3.0 lib/locomotive/wagon/liquid/tags/snippet.rb