liquid 2.6.3 → 5.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/History.md +272 -26
- data/README.md +67 -3
- data/lib/liquid/block.rb +62 -94
- data/lib/liquid/block_body.rb +255 -0
- data/lib/liquid/condition.rb +96 -38
- data/lib/liquid/context.rb +172 -154
- data/lib/liquid/document.rb +57 -9
- data/lib/liquid/drop.rb +33 -14
- data/lib/liquid/errors.rb +56 -10
- data/lib/liquid/expression.rb +45 -0
- data/lib/liquid/extensions.rb +21 -7
- data/lib/liquid/file_system.rb +27 -14
- data/lib/liquid/forloop_drop.rb +92 -0
- data/lib/liquid/i18n.rb +41 -0
- data/lib/liquid/interrupts.rb +3 -2
- data/lib/liquid/lexer.rb +62 -0
- data/lib/liquid/locales/en.yml +29 -0
- data/lib/liquid/parse_context.rb +54 -0
- data/lib/liquid/parse_tree_visitor.rb +42 -0
- data/lib/liquid/parser.rb +102 -0
- data/lib/liquid/parser_switching.rb +45 -0
- data/lib/liquid/partial_cache.rb +24 -0
- data/lib/liquid/profiler/hooks.rb +35 -0
- data/lib/liquid/profiler.rb +139 -0
- data/lib/liquid/range_lookup.rb +47 -0
- data/lib/liquid/registers.rb +51 -0
- data/lib/liquid/resource_limits.rb +62 -0
- data/lib/liquid/standardfilters.rb +789 -118
- data/lib/liquid/strainer_factory.rb +41 -0
- data/lib/liquid/strainer_template.rb +62 -0
- data/lib/liquid/tablerowloop_drop.rb +121 -0
- data/lib/liquid/tag/disableable.rb +22 -0
- data/lib/liquid/tag/disabler.rb +21 -0
- data/lib/liquid/tag.rb +49 -10
- data/lib/liquid/tags/assign.rb +61 -19
- data/lib/liquid/tags/break.rb +14 -4
- data/lib/liquid/tags/capture.rb +29 -21
- data/lib/liquid/tags/case.rb +80 -31
- data/lib/liquid/tags/comment.rb +24 -2
- data/lib/liquid/tags/continue.rb +14 -13
- data/lib/liquid/tags/cycle.rb +50 -32
- data/lib/liquid/tags/decrement.rb +24 -26
- data/lib/liquid/tags/echo.rb +41 -0
- data/lib/liquid/tags/for.rb +164 -100
- data/lib/liquid/tags/if.rb +105 -44
- data/lib/liquid/tags/ifchanged.rb +10 -11
- data/lib/liquid/tags/include.rb +85 -65
- data/lib/liquid/tags/increment.rb +24 -22
- data/lib/liquid/tags/inline_comment.rb +43 -0
- data/lib/liquid/tags/raw.rb +50 -11
- data/lib/liquid/tags/render.rb +109 -0
- data/lib/liquid/tags/table_row.rb +88 -0
- data/lib/liquid/tags/unless.rb +37 -21
- data/lib/liquid/template.rb +124 -46
- data/lib/liquid/template_factory.rb +9 -0
- data/lib/liquid/tokenizer.rb +39 -0
- data/lib/liquid/usage.rb +8 -0
- data/lib/liquid/utils.rb +68 -5
- data/lib/liquid/variable.rb +128 -32
- data/lib/liquid/variable_lookup.rb +96 -0
- data/lib/liquid/version.rb +3 -1
- data/lib/liquid.rb +36 -13
- metadata +69 -77
- data/lib/extras/liquid_view.rb +0 -51
- data/lib/liquid/htmltags.rb +0 -73
- data/lib/liquid/module_ex.rb +0 -62
- data/lib/liquid/strainer.rb +0 -53
- data/test/liquid/assign_test.rb +0 -21
- data/test/liquid/block_test.rb +0 -58
- data/test/liquid/capture_test.rb +0 -40
- data/test/liquid/condition_test.rb +0 -127
- data/test/liquid/context_test.rb +0 -478
- data/test/liquid/drop_test.rb +0 -180
- data/test/liquid/error_handling_test.rb +0 -81
- data/test/liquid/file_system_test.rb +0 -29
- data/test/liquid/filter_test.rb +0 -125
- data/test/liquid/hash_ordering_test.rb +0 -25
- data/test/liquid/module_ex_test.rb +0 -87
- data/test/liquid/output_test.rb +0 -116
- data/test/liquid/parsing_quirks_test.rb +0 -52
- data/test/liquid/regexp_test.rb +0 -44
- data/test/liquid/security_test.rb +0 -64
- data/test/liquid/standard_filter_test.rb +0 -263
- data/test/liquid/strainer_test.rb +0 -52
- data/test/liquid/tags/break_tag_test.rb +0 -16
- data/test/liquid/tags/continue_tag_test.rb +0 -16
- data/test/liquid/tags/for_tag_test.rb +0 -297
- data/test/liquid/tags/html_tag_test.rb +0 -63
- data/test/liquid/tags/if_else_tag_test.rb +0 -166
- data/test/liquid/tags/include_tag_test.rb +0 -166
- data/test/liquid/tags/increment_tag_test.rb +0 -24
- data/test/liquid/tags/raw_tag_test.rb +0 -24
- data/test/liquid/tags/standard_tag_test.rb +0 -295
- data/test/liquid/tags/statements_test.rb +0 -134
- data/test/liquid/tags/unless_else_tag_test.rb +0 -26
- data/test/liquid/template_test.rb +0 -146
- data/test/liquid/variable_test.rb +0 -186
- data/test/test_helper.rb +0 -29
- /data/{MIT-LICENSE → LICENSE} +0 -0
data/lib/liquid/tags/if.rb
CHANGED
@@ -1,78 +1,139 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
3
|
+
module Liquid
|
4
|
+
# @liquid_public_docs
|
5
|
+
# @liquid_type tag
|
6
|
+
# @liquid_category conditional
|
7
|
+
# @liquid_name if
|
8
|
+
# @liquid_summary
|
9
|
+
# Renders an expression if a specific condition is `true`.
|
10
|
+
# @liquid_syntax
|
11
|
+
# {% if condition %}
|
12
|
+
# expression
|
9
13
|
# {% endif %}
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
+
# @liquid_syntax_keyword condition The condition to evaluate.
|
15
|
+
# @liquid_syntax_keyword expression The expression to render if the condition is met.
|
14
16
|
class If < Block
|
15
|
-
|
16
|
-
Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/o
|
17
|
+
Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/o
|
17
18
|
ExpressionsAndOperators = /(?:\b(?:\s?and\s?|\s?or\s?)\b|(?:\s*(?!\b(?:\s?and\s?|\s?or\s?)\b)(?:#{QuotedFragment}|\S+)\s*)+)/o
|
18
|
-
BOOLEAN_OPERATORS
|
19
|
+
BOOLEAN_OPERATORS = %w(and or).freeze
|
19
20
|
|
20
|
-
|
21
|
-
@blocks = []
|
21
|
+
attr_reader :blocks
|
22
22
|
|
23
|
+
def initialize(tag_name, markup, options)
|
24
|
+
super
|
25
|
+
@blocks = []
|
23
26
|
push_block('if', markup)
|
27
|
+
end
|
24
28
|
|
25
|
-
|
29
|
+
def nodelist
|
30
|
+
@blocks.map(&:attachment)
|
26
31
|
end
|
27
32
|
|
33
|
+
def parse(tokens)
|
34
|
+
while parse_body(@blocks.last.attachment, tokens)
|
35
|
+
end
|
36
|
+
@blocks.reverse_each do |block|
|
37
|
+
block.attachment.remove_blank_strings if blank?
|
38
|
+
block.attachment.freeze
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
ELSE_TAG_NAMES = ['elsif', 'else'].freeze
|
43
|
+
private_constant :ELSE_TAG_NAMES
|
44
|
+
|
28
45
|
def unknown_tag(tag, markup, tokens)
|
29
|
-
if
|
46
|
+
if ELSE_TAG_NAMES.include?(tag)
|
30
47
|
push_block(tag, markup)
|
31
48
|
else
|
32
49
|
super
|
33
50
|
end
|
34
51
|
end
|
35
52
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
53
|
+
def render_to_output_buffer(context, output)
|
54
|
+
@blocks.each do |block|
|
55
|
+
result = Liquid::Utils.to_liquid_value(
|
56
|
+
block.evaluate(context)
|
57
|
+
)
|
58
|
+
|
59
|
+
if result
|
60
|
+
return block.attachment.render_to_output_buffer(context, output)
|
42
61
|
end
|
43
|
-
''
|
44
62
|
end
|
63
|
+
|
64
|
+
output
|
45
65
|
end
|
46
66
|
|
47
67
|
private
|
48
68
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
69
|
+
def push_block(tag, markup)
|
70
|
+
block = if tag == 'else'
|
71
|
+
ElseCondition.new
|
72
|
+
else
|
73
|
+
parse_with_selected_parser(markup)
|
74
|
+
end
|
53
75
|
|
54
|
-
|
55
|
-
|
76
|
+
@blocks.push(block)
|
77
|
+
block.attach(new_body)
|
78
|
+
end
|
56
79
|
|
57
|
-
|
80
|
+
def parse_expression(markup)
|
81
|
+
Condition.parse_expression(parse_context, markup)
|
82
|
+
end
|
58
83
|
|
59
|
-
|
60
|
-
|
84
|
+
def lax_parse(markup)
|
85
|
+
expressions = markup.scan(ExpressionsAndOperators)
|
86
|
+
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop =~ Syntax
|
61
87
|
|
62
|
-
|
88
|
+
condition = Condition.new(parse_expression(Regexp.last_match(1)), Regexp.last_match(2), parse_expression(Regexp.last_match(3)))
|
63
89
|
|
64
|
-
|
65
|
-
|
66
|
-
new_condition.send(operator, condition)
|
67
|
-
condition = new_condition
|
68
|
-
end
|
90
|
+
until expressions.empty?
|
91
|
+
operator = expressions.pop.to_s.strip
|
69
92
|
|
70
|
-
|
71
|
-
|
93
|
+
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop.to_s =~ Syntax
|
94
|
+
|
95
|
+
new_condition = Condition.new(parse_expression(Regexp.last_match(1)), Regexp.last_match(2), parse_expression(Regexp.last_match(3)))
|
96
|
+
raise SyntaxError, options[:locale].t("errors.syntax.if") unless BOOLEAN_OPERATORS.include?(operator)
|
97
|
+
new_condition.send(operator, condition)
|
98
|
+
condition = new_condition
|
99
|
+
end
|
100
|
+
|
101
|
+
condition
|
102
|
+
end
|
103
|
+
|
104
|
+
def strict_parse(markup)
|
105
|
+
p = Parser.new(markup)
|
106
|
+
condition = parse_binary_comparisons(p)
|
107
|
+
p.consume(:end_of_string)
|
108
|
+
condition
|
109
|
+
end
|
72
110
|
|
73
|
-
|
74
|
-
|
111
|
+
def parse_binary_comparisons(p)
|
112
|
+
condition = parse_comparison(p)
|
113
|
+
first_condition = condition
|
114
|
+
while (op = (p.id?('and') || p.id?('or')))
|
115
|
+
child_condition = parse_comparison(p)
|
116
|
+
condition.send(op, child_condition)
|
117
|
+
condition = child_condition
|
75
118
|
end
|
119
|
+
first_condition
|
120
|
+
end
|
121
|
+
|
122
|
+
def parse_comparison(p)
|
123
|
+
a = parse_expression(p.expression)
|
124
|
+
if (op = p.consume?(:comparison))
|
125
|
+
b = parse_expression(p.expression)
|
126
|
+
Condition.new(a, op, b)
|
127
|
+
else
|
128
|
+
Condition.new(a)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
class ParseTreeVisitor < Liquid::ParseTreeVisitor
|
133
|
+
def children
|
134
|
+
@node.blocks
|
135
|
+
end
|
136
|
+
end
|
76
137
|
end
|
77
138
|
|
78
139
|
Template.register_tag('if', If)
|
@@ -1,18 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Liquid
|
2
4
|
class Ifchanged < Block
|
5
|
+
def render_to_output_buffer(context, output)
|
6
|
+
block_output = +''
|
7
|
+
super(context, block_output)
|
3
8
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
output = render_all(@nodelist, context)
|
8
|
-
|
9
|
-
if output != context.registers[:ifchanged]
|
10
|
-
context.registers[:ifchanged] = output
|
11
|
-
output
|
12
|
-
else
|
13
|
-
''
|
14
|
-
end
|
9
|
+
if block_output != context.registers[:ifchanged]
|
10
|
+
context.registers[:ifchanged] = block_output
|
11
|
+
output << block_output
|
15
12
|
end
|
13
|
+
|
14
|
+
output
|
16
15
|
end
|
17
16
|
end
|
18
17
|
|
data/lib/liquid/tags/include.rb
CHANGED
@@ -1,92 +1,112 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
3
|
+
module Liquid
|
4
|
+
# @liquid_public_docs
|
5
|
+
# @liquid_type tag
|
6
|
+
# @liquid_category theme
|
7
|
+
# @liquid_name include
|
8
|
+
# @liquid_summary
|
9
|
+
# Renders a [snippet](/themes/architecture#snippets).
|
10
|
+
# @liquid_description
|
11
|
+
# Inside the snippet, you can access and alter variables that are [created](/api/liquid/tags#variable-tags) outside of the
|
12
|
+
# snippet.
|
13
|
+
# @liquid_syntax
|
14
|
+
# {% include 'filename' %}
|
15
|
+
# @liquid_syntax_keyword filename The name of the snippet to render, without the `.liquid` extension.
|
16
|
+
# @liquid_deprecated
|
17
|
+
# Deprecated because the way that variables are handled reduces performance and makes code harder to both read and maintain.
|
16
18
|
#
|
19
|
+
# The `include` tag has been replaced by [`render`](/api/liquid/tags#render).
|
17
20
|
class Include < Tag
|
18
|
-
|
21
|
+
prepend Tag::Disableable
|
22
|
+
|
23
|
+
SYNTAX = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
24
|
+
Syntax = SYNTAX
|
19
25
|
|
20
|
-
|
21
|
-
|
26
|
+
attr_reader :template_name_expr, :variable_name_expr, :attributes
|
27
|
+
|
28
|
+
def initialize(tag_name, markup, options)
|
29
|
+
super
|
22
30
|
|
23
|
-
|
24
|
-
|
25
|
-
|
31
|
+
if markup =~ SYNTAX
|
32
|
+
|
33
|
+
template_name = Regexp.last_match(1)
|
34
|
+
variable_name = Regexp.last_match(3)
|
35
|
+
|
36
|
+
@alias_name = Regexp.last_match(5)
|
37
|
+
@variable_name_expr = variable_name ? parse_expression(variable_name) : nil
|
38
|
+
@template_name_expr = parse_expression(template_name)
|
39
|
+
@attributes = {}
|
26
40
|
|
27
41
|
markup.scan(TagAttributes) do |key, value|
|
28
|
-
@attributes[key] = value
|
42
|
+
@attributes[key] = parse_expression(value)
|
29
43
|
end
|
30
44
|
|
31
45
|
else
|
32
|
-
raise SyntaxError.
|
46
|
+
raise SyntaxError, options[:locale].t("errors.syntax.include")
|
33
47
|
end
|
34
|
-
|
35
|
-
super
|
36
48
|
end
|
37
49
|
|
38
|
-
def parse(
|
50
|
+
def parse(_tokens)
|
39
51
|
end
|
40
52
|
|
41
|
-
def
|
42
|
-
|
43
|
-
|
53
|
+
def render_to_output_buffer(context, output)
|
54
|
+
template_name = context.evaluate(@template_name_expr)
|
55
|
+
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
44
56
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
57
|
+
partial = PartialCache.load(
|
58
|
+
template_name,
|
59
|
+
context: context,
|
60
|
+
parse_context: parse_context
|
61
|
+
)
|
49
62
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
context[@template_name[1..-2]] = variable
|
57
|
-
partial.render(context)
|
58
|
-
end
|
63
|
+
context_variable_name = @alias_name || template_name.split('/').last
|
64
|
+
|
65
|
+
variable = if @variable_name_expr
|
66
|
+
context.evaluate(@variable_name_expr)
|
67
|
+
else
|
68
|
+
context.find_variable(template_name, raise_on_not_found: false)
|
59
69
|
end
|
60
|
-
end
|
61
70
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
template_name =
|
71
|
+
old_template_name = context.template_name
|
72
|
+
old_partial = context.partial
|
73
|
+
begin
|
74
|
+
context.template_name = template_name
|
75
|
+
context.partial = true
|
76
|
+
context.stack do
|
77
|
+
@attributes.each do |key, value|
|
78
|
+
context[key] = context.evaluate(value)
|
79
|
+
end
|
66
80
|
|
67
|
-
|
68
|
-
|
81
|
+
if variable.is_a?(Array)
|
82
|
+
variable.each do |var|
|
83
|
+
context[context_variable_name] = var
|
84
|
+
partial.render_to_output_buffer(context, output)
|
85
|
+
end
|
86
|
+
else
|
87
|
+
context[context_variable_name] = variable
|
88
|
+
partial.render_to_output_buffer(context, output)
|
89
|
+
end
|
69
90
|
end
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
context.registers[:cached_partials] = cached_partials
|
74
|
-
partial
|
91
|
+
ensure
|
92
|
+
context.template_name = old_template_name
|
93
|
+
context.partial = old_partial
|
75
94
|
end
|
76
95
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
96
|
+
output
|
97
|
+
end
|
98
|
+
|
99
|
+
alias_method :parse_context, :options
|
100
|
+
private :parse_context
|
101
|
+
|
102
|
+
class ParseTreeVisitor < Liquid::ParseTreeVisitor
|
103
|
+
def children
|
104
|
+
[
|
105
|
+
@node.template_name_expr,
|
106
|
+
@node.variable_name_expr,
|
107
|
+
] + @node.attributes.values
|
89
108
|
end
|
109
|
+
end
|
90
110
|
end
|
91
111
|
|
92
112
|
Template.register_tag('include', Include)
|
@@ -1,34 +1,36 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# increment is used in a place where one needs to insert a counter
|
4
|
-
# into a template, and needs the counter to survive across
|
5
|
-
# multiple instantiations of the template.
|
6
|
-
# (To achieve the survival, the application must keep the context)
|
7
|
-
#
|
8
|
-
# if the variable does not exist, it is created with value 0.
|
1
|
+
# frozen_string_literal: true
|
9
2
|
|
10
|
-
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
3
|
+
module Liquid
|
4
|
+
# @liquid_public_docs
|
5
|
+
# @liquid_type tag
|
6
|
+
# @liquid_category variable
|
7
|
+
# @liquid_name increment
|
8
|
+
# @liquid_summary
|
9
|
+
# Creates a new variable, with a default value of 0, that's increased by 1 with each subsequent call.
|
10
|
+
# @liquid_description
|
11
|
+
# Variables that are declared with `increment` are unique to the [layout](/themes/architecture/layouts), [template](/themes/architecture/templates),
|
12
|
+
# or [section](/themes/architecture/sections) file that they're created in. However, the variable is shared across
|
13
|
+
# [snippets](/themes/architecture#snippets) included in the file.
|
17
14
|
#
|
15
|
+
# Similarly, variables that are created with `increment` are independent from those created with [`assign`](/api/liquid/tags#assign)
|
16
|
+
# and [`capture`](/api/liquid/tags#capture). However, `increment` and [`decrement`](/api/liquid/tags#decrement) share
|
17
|
+
# variables.
|
18
|
+
# @liquid_syntax
|
19
|
+
# {% increment variable_name %}
|
20
|
+
# @liquid_syntax_keyword variable_name The name of the variable being incremented.
|
18
21
|
class Increment < Tag
|
19
|
-
def initialize(tag_name, markup,
|
20
|
-
@variable = markup.strip
|
21
|
-
|
22
|
+
def initialize(tag_name, markup, options)
|
22
23
|
super
|
24
|
+
@variable = markup.strip
|
23
25
|
end
|
24
26
|
|
25
|
-
def
|
27
|
+
def render_to_output_buffer(context, output)
|
26
28
|
value = context.environments.first[@variable] ||= 0
|
27
29
|
context.environments.first[@variable] = value + 1
|
28
|
-
value.to_s
|
29
|
-
end
|
30
30
|
|
31
|
-
|
31
|
+
output << value.to_s
|
32
|
+
output
|
33
|
+
end
|
32
34
|
end
|
33
35
|
|
34
36
|
Template.register_tag('increment', Increment)
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Liquid
|
4
|
+
# @liquid_public_docs
|
5
|
+
# @liquid_type tag
|
6
|
+
# @liquid_category syntax
|
7
|
+
# @liquid_name inline_comment
|
8
|
+
# @liquid_summary
|
9
|
+
# Prevents an expression from being rendered or output.
|
10
|
+
# @liquid_description
|
11
|
+
# Any text inside an `inline_comment` tag won't be rendered or output.
|
12
|
+
#
|
13
|
+
# You can create multi-line inline comments. However, each line must begin with a `#`.
|
14
|
+
# @liquid_syntax
|
15
|
+
# {% # content %}
|
16
|
+
# @liquid_syntax_keyword content The content of the comment.
|
17
|
+
class InlineComment < Tag
|
18
|
+
def initialize(tag_name, markup, options)
|
19
|
+
super
|
20
|
+
|
21
|
+
# Semantically, a comment should only ignore everything after it on the line.
|
22
|
+
# Currently, this implementation doesn't support mixing a comment with another tag
|
23
|
+
# but we need to reserve future support for this and prevent the introduction
|
24
|
+
# of inline comments from being backward incompatible change.
|
25
|
+
#
|
26
|
+
# As such, we're forcing users to put a # symbol on every line otherwise this
|
27
|
+
# tag will throw an error.
|
28
|
+
if markup.match?(/\n\s*[^#\s]/)
|
29
|
+
raise SyntaxError, options[:locale].t("errors.syntax.inline_comment_invalid")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def render_to_output_buffer(_context, output)
|
34
|
+
output
|
35
|
+
end
|
36
|
+
|
37
|
+
def blank?
|
38
|
+
true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
Template.register_tag('#', InlineComment)
|
43
|
+
end
|
data/lib/liquid/tags/raw.rb
CHANGED
@@ -1,19 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Liquid
|
4
|
+
# @liquid_public_docs
|
5
|
+
# @liquid_type tag
|
6
|
+
# @liquid_category syntax
|
7
|
+
# @liquid_name raw
|
8
|
+
# @liquid_summary
|
9
|
+
# Outputs any Liquid code as text instead of rendering it.
|
10
|
+
# @liquid_syntax
|
11
|
+
# {% raw %}
|
12
|
+
# expression
|
13
|
+
# {% endraw %}
|
14
|
+
# @liquid_syntax_keyword expression The expression to be output without being rendered.
|
2
15
|
class Raw < Block
|
3
|
-
|
16
|
+
Syntax = /\A\s*\z/
|
17
|
+
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
|
18
|
+
|
19
|
+
def initialize(tag_name, markup, parse_context)
|
20
|
+
super
|
21
|
+
|
22
|
+
ensure_valid_markup(tag_name, markup, parse_context)
|
23
|
+
end
|
4
24
|
|
5
25
|
def parse(tokens)
|
6
|
-
@
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
if block_delimiter == $2
|
12
|
-
end_tag
|
13
|
-
return
|
14
|
-
end
|
26
|
+
@body = +''
|
27
|
+
while (token = tokens.shift)
|
28
|
+
if token =~ FullTokenPossiblyInvalid && block_delimiter == Regexp.last_match(2)
|
29
|
+
@body << Regexp.last_match(1) if Regexp.last_match(1) != ""
|
30
|
+
return
|
15
31
|
end
|
16
|
-
@
|
32
|
+
@body << token unless token.empty?
|
33
|
+
end
|
34
|
+
|
35
|
+
raise_tag_never_closed(block_name)
|
36
|
+
end
|
37
|
+
|
38
|
+
def render_to_output_buffer(_context, output)
|
39
|
+
output << @body
|
40
|
+
output
|
41
|
+
end
|
42
|
+
|
43
|
+
def nodelist
|
44
|
+
[@body]
|
45
|
+
end
|
46
|
+
|
47
|
+
def blank?
|
48
|
+
@body.empty?
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def ensure_valid_markup(tag_name, markup, parse_context)
|
54
|
+
unless Syntax.match?(markup)
|
55
|
+
raise SyntaxError, parse_context.locale.t("errors.syntax.tag_unexpected_args", tag: tag_name)
|
17
56
|
end
|
18
57
|
end
|
19
58
|
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Liquid
|
4
|
+
# @liquid_public_docs
|
5
|
+
# @liquid_type tag
|
6
|
+
# @liquid_category theme
|
7
|
+
# @liquid_name render
|
8
|
+
# @liquid_summary
|
9
|
+
# Renders a [snippet](/themes/architecture#snippets) or [app block](/themes/architecture/sections/section-schema#render-app-blocks).
|
10
|
+
# @liquid_description
|
11
|
+
# Inside snippets and app blocks, you can't directly access variables that are [created](/api/liquid/tags#variable-tags) outside
|
12
|
+
# of the snippet or app block. However, you can [specify variables as parameters](/api/liquid/tags#render-passing-variables-to-snippets)
|
13
|
+
# to pass outside variables to snippets.
|
14
|
+
#
|
15
|
+
# While you can't directly access created variables, you can access global objects, as well as any objects that are
|
16
|
+
# directly accessible outside the snippet or app block. For example, a snippet or app block inside the [product template](/themes/architecture/templates/product)
|
17
|
+
# can access the [`product` object](/api/liquid/objects#product), and a snippet or app block inside a [section](/themes/architecture/sections)
|
18
|
+
# can access the [`section` object](/api/liquid/objects#section).
|
19
|
+
#
|
20
|
+
# Outside a snippet or app block, you can't access variables created inside the snippet or app block.
|
21
|
+
#
|
22
|
+
# > Note:
|
23
|
+
# > When you render a snippet using the `render` tag, you can't use the [`include` tag](/api/liquid/tags#include)
|
24
|
+
# > inside the snippet.
|
25
|
+
# @liquid_syntax
|
26
|
+
# {% render 'filename' %}
|
27
|
+
# @liquid_syntax_keyword filename The name of the snippet to render, without the `.liquid` extension.
|
28
|
+
class Render < Tag
|
29
|
+
FOR = 'for'
|
30
|
+
SYNTAX = /(#{QuotedString}+)(\s+(with|#{FOR})\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
31
|
+
|
32
|
+
disable_tags "include"
|
33
|
+
|
34
|
+
attr_reader :template_name_expr, :variable_name_expr, :attributes
|
35
|
+
|
36
|
+
def initialize(tag_name, markup, options)
|
37
|
+
super
|
38
|
+
|
39
|
+
raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
|
40
|
+
|
41
|
+
template_name = Regexp.last_match(1)
|
42
|
+
with_or_for = Regexp.last_match(3)
|
43
|
+
variable_name = Regexp.last_match(4)
|
44
|
+
|
45
|
+
@alias_name = Regexp.last_match(6)
|
46
|
+
@variable_name_expr = variable_name ? parse_expression(variable_name) : nil
|
47
|
+
@template_name_expr = parse_expression(template_name)
|
48
|
+
@for = (with_or_for == FOR)
|
49
|
+
|
50
|
+
@attributes = {}
|
51
|
+
markup.scan(TagAttributes) do |key, value|
|
52
|
+
@attributes[key] = parse_expression(value)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def render_to_output_buffer(context, output)
|
57
|
+
render_tag(context, output)
|
58
|
+
end
|
59
|
+
|
60
|
+
def render_tag(context, output)
|
61
|
+
# The expression should be a String literal, which parses to a String object
|
62
|
+
template_name = @template_name_expr
|
63
|
+
raise ::ArgumentError unless template_name.is_a?(String)
|
64
|
+
|
65
|
+
partial = PartialCache.load(
|
66
|
+
template_name,
|
67
|
+
context: context,
|
68
|
+
parse_context: parse_context
|
69
|
+
)
|
70
|
+
|
71
|
+
context_variable_name = @alias_name || template_name.split('/').last
|
72
|
+
|
73
|
+
render_partial_func = ->(var, forloop) {
|
74
|
+
inner_context = context.new_isolated_subcontext
|
75
|
+
inner_context.template_name = template_name
|
76
|
+
inner_context.partial = true
|
77
|
+
inner_context['forloop'] = forloop if forloop
|
78
|
+
|
79
|
+
@attributes.each do |key, value|
|
80
|
+
inner_context[key] = context.evaluate(value)
|
81
|
+
end
|
82
|
+
inner_context[context_variable_name] = var unless var.nil?
|
83
|
+
partial.render_to_output_buffer(inner_context, output)
|
84
|
+
forloop&.send(:increment!)
|
85
|
+
}
|
86
|
+
|
87
|
+
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
88
|
+
if @for && variable.respond_to?(:each) && variable.respond_to?(:count)
|
89
|
+
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
|
90
|
+
variable.each { |var| render_partial_func.call(var, forloop) }
|
91
|
+
else
|
92
|
+
render_partial_func.call(variable, nil)
|
93
|
+
end
|
94
|
+
|
95
|
+
output
|
96
|
+
end
|
97
|
+
|
98
|
+
class ParseTreeVisitor < Liquid::ParseTreeVisitor
|
99
|
+
def children
|
100
|
+
[
|
101
|
+
@node.template_name_expr,
|
102
|
+
@node.variable_name_expr,
|
103
|
+
] + @node.attributes.values
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
Template.register_tag('render', Render)
|
109
|
+
end
|