Sha256: 99ded26e70fec558caa4f19ae22303534d3a807ba1eec8dd112acd0e5ea8c06a

Contents?: true

Size: 1.99 KB

Versions: 25

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module PlatformosCheck
  module Tags
    class Render < Liquid::Tag
      SYNTAX = /
      (
        ## for {% render "snippet" %}
          #{Liquid::QuotedString}+ |
        ## for {% render block %}
        ## We require the variable # segment to be at the beginning of the
        ## string (with \A). This is to prevent code like {% render !foo! %}
        ## from parsing
        \A#{Liquid::VariableSegment}+
      )
      ## for {% render "snippet" with product as p %}
      ## or {% render "snippet" for products p %}
      (\s+(with|#{Liquid::Render::FOR})\s+(#{Liquid::QuotedFragment}+))?
        (\s+(?:as)\s+(#{Liquid::VariableSegment}+))?
        ## variables passed into the tag (e.g. {% render "snippet", var1: value1, var2: value2 %}
        ## are not matched by this regex and are handled by Liquid::Render.initialize
        /xo

      disable_tags "include"

      attr_reader :template_name_expr, :variable_name_expr, :attributes, :duplicated_attrs

      def initialize(tag_name, markup, options)
        super

        raise Liquid::SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX

        template_name = Regexp.last_match(1)
        with_or_for = Regexp.last_match(3)
        variable_name = Regexp.last_match(4)

        @alias_name = Regexp.last_match(6)
        @variable_name_expr = variable_name ? parse_expression(variable_name) : nil
        @template_name_expr = parse_expression(template_name)
        @for = (with_or_for == Liquid::Render::FOR)

        @attributes = {}
        @duplicated_attrs = []
        markup.scan(Liquid::TagAttributes) do |key, value|
          @duplicated_attrs << key if @attributes.key?(key)
          @attributes[key] = parse_expression(value)
        end
      end

      class ParseTreeVisitor < Liquid::ParseTreeVisitor
        def children
          [
            @node.template_name_expr,
            @node.variable_name_expr
          ] + @node.attributes.values
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
platformos-check-0.2.0 lib/platformos_check/tags/render.rb
platformos-check-0.1.0 lib/platformos_check/tags/render.rb
platformos-check-0.0.3 lib/platformos_check/tags/render.rb
platformos-check-0.0.2 lib/platformos_check/tags/render.rb
platformos-check-0.0.1 lib/platformos_check/tags/render.rb