Class: Liquid::Include
- Includes:
- Tag::Disableable
- Defined in:
- lib/liquid/tags/include.rb
Defined Under Namespace
Classes: ParseTreeVisitor
Constant Summary collapse
- FOR =
'for'- SYNTAX =
/(#{QuotedFragment}+)(\s+(with|#{FOR})\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o- Syntax =
SYNTAX
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#template_name_expr ⇒ Object
readonly
Returns the value of attribute template_name_expr.
-
#variable_name_expr ⇒ Object
readonly
Returns the value of attribute variable_name_expr.
Attributes inherited from Tag
#line_number, #nodelist, #tag_name
Instance Method Summary collapse
- #for_loop? ⇒ Boolean
-
#initialize(tag_name, markup, options) ⇒ Include
constructor
A new instance of Include.
- #lax_parse(markup) ⇒ Object
- #parse(_tokens) ⇒ Object
- #render_to_output_buffer(context, output) ⇒ Object
- #strict2_parse(markup) ⇒ Object
- #strict_parse(markup) ⇒ Object
Methods included from Tag::Disableable
Methods inherited from Tag
#blank?, disable_tags, #name, parse, #raw, #render
Methods included from ParserSwitching
#parse_with_selected_parser, #strict2_mode?, #strict_parse_with_error_mode_fallback
Constructor Details
#initialize(tag_name, markup, options) ⇒ Include
Returns a new instance of Include.
29 30 31 32 |
# File 'lib/liquid/tags/include.rb', line 29 def initialize(tag_name, markup, ) super parse_with_selected_parser(markup) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
27 28 29 |
# File 'lib/liquid/tags/include.rb', line 27 def attributes @attributes end |
#template_name_expr ⇒ Object (readonly)
Returns the value of attribute template_name_expr.
27 28 29 |
# File 'lib/liquid/tags/include.rb', line 27 def template_name_expr @template_name_expr end |
#variable_name_expr ⇒ Object (readonly)
Returns the value of attribute variable_name_expr.
27 28 29 |
# File 'lib/liquid/tags/include.rb', line 27 def variable_name_expr @variable_name_expr end |
Instance Method Details
#for_loop? ⇒ Boolean
88 89 90 |
# File 'lib/liquid/tags/include.rb', line 88 def for_loop? @is_for_loop end |
#lax_parse(markup) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/liquid/tags/include.rb', line 118 def lax_parse(markup) if 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) @is_for_loop = (with_or_for == FOR) @attributes = {} markup.scan(TagAttributes) do |key, value| @attributes[key] = parse_expression(value) end else raise SyntaxError, [:locale].t("errors.syntax.include") end end |
#parse(_tokens) ⇒ Object
34 35 |
# File 'lib/liquid/tags/include.rb', line 34 def parse(_tokens) end |
#render_to_output_buffer(context, output) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/liquid/tags/include.rb', line 37 def render_to_output_buffer(context, output) template_name = context.evaluate(@template_name_expr) raise ArgumentError, [:locale].t("errors.argument.include") unless template_name.is_a?(String) partial = PartialCache.load( template_name, context: context, parse_context: parse_context, ) context_variable_name = @alias_name || template_name.split('/').last variable = if @variable_name_expr context.evaluate(@variable_name_expr) else context.find_variable(template_name, raise_on_not_found: false) end old_template_name = context.template_name old_partial = context.partial begin context.template_name = partial.name context.partial = true context.stack do @attributes.each do |key, value| context[key] = context.evaluate(value) end if variable.is_a?(Array) variable.each do |var| context[context_variable_name] = var partial.render_to_output_buffer(context, output) end else context[context_variable_name] = variable partial.render_to_output_buffer(context, output) end end ensure context.template_name = old_template_name context.partial = old_partial end output end |
#strict2_parse(markup) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/liquid/tags/include.rb', line 92 def strict2_parse(markup) p = @parse_context.new_parser(markup) @template_name_expr = safe_parse_expression(p) with_or_for = p.id?("for") || p.id?("with") @variable_name_expr = safe_parse_expression(p) if with_or_for @alias_name = p.consume(:id) if p.id?("as") @is_for_loop = (with_or_for == FOR) p.consume?(:comma) @attributes = {} while p.look(:id) key = p.consume p.consume(:colon) @attributes[key] = safe_parse_expression(p) p.consume?(:comma) end p.consume(:end_of_string) end |
#strict_parse(markup) ⇒ Object
114 115 116 |
# File 'lib/liquid/tags/include.rb', line 114 def strict_parse(markup) lax_parse(markup) end |