Sha256: e41f82ee7f418e0869aa42a8bd43e63c5e1f8031da3f447145333aa83a7da49e

Contents?: true

Size: 1.8 KB

Versions: 17

Compression:

Stored size: 1.8 KB

Contents

module Locomotive
  module Liquid
    module Tags

      class Snippet < ::Liquid::Include

        attr_accessor :slug
        attr_accessor :partial

        def initialize(tag_name, markup, tokens, context)
          super

          @slug = @template_name.gsub(/['"]/o, '')

          if @context[:snippets].present?
            (@context[:snippets] << @slug).uniq!
          else
            @context[:snippets] = [@slug]
          end

          if @context[:site].present?
            snippet = @context[:site].snippets.where(slug: @slug).first
            self.refresh(snippet) if snippet
          end
        end

        def render(context)
          return '' if @partial.nil?

          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)

            output
          end
        end

        def refresh(snippet, context = {})
          if snippet.destroyed?
            @snippet_id = nil
            @partial = nil
          else
            @snippet_id = snippet.id
            @partial = ::Liquid::Template.parse(snippet.template, context.merge(@context))
            @partial.root.context.clear
          end
        end

        def nodelist
          if @partial
            @partial.root.nodelist
          else
            []
          end
        end

      end

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

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
locomotive_cms-2.5.7 lib/locomotive/liquid/tags/snippet.rb
locomotivecms-3.0.0.pre.alpha.2 lib/locomotive/previous_liquid/tags/snippet.rb
locomotivecms-3.0.0.pre.alpha lib/locomotive/previous_liquid/tags/snippet.rb
locomotive_cms-2.5.6 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.6.rc2 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.6.rc1 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.5 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.4 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.3 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.2 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.1 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.0 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.0.rc3 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.0.rc2 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.5.0.rc1 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.4.1 lib/locomotive/liquid/tags/snippet.rb
locomotive_cms-2.4.0 lib/locomotive/liquid/tags/snippet.rb