mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
WIP
This commit is contained in:
@@ -27,14 +27,28 @@ module Liquid
|
|||||||
super
|
super
|
||||||
if markup =~ Syntax
|
if markup =~ Syntax
|
||||||
@to = Regexp.last_match(1)
|
@to = Regexp.last_match(1)
|
||||||
@from = Variable.new(Regexp.last_match(2), parse_context)
|
from_ = Regexp.last_match(2).strip
|
||||||
|
if /^\[.*\]$/.match?(from_)
|
||||||
|
from_ = from_[1..-2].split(",")
|
||||||
|
@from = from_.map { |s| Variable.new(s.strip, parse_context) }
|
||||||
|
else
|
||||||
|
@from = Variable.new(from_, parse_context)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
self.class.raise_syntax_error(parse_context)
|
self.class.raise_syntax_error(parse_context)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
val = @from.render(context)
|
val = nil
|
||||||
|
if @from.is_a?(Array)
|
||||||
|
val = []
|
||||||
|
@from.each do |var|
|
||||||
|
val << var.render(context)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
val = @from.render(context)
|
||||||
|
end
|
||||||
context.scopes.last[@to] = val
|
context.scopes.last[@to] = val
|
||||||
context.resource_limits.increment_assign_score(assign_score_of(val))
|
context.resource_limits.increment_assign_score(assign_score_of(val))
|
||||||
output
|
output
|
||||||
|
|||||||
@@ -53,6 +53,21 @@ class AssignTest < Minitest::Test
|
|||||||
assert_template_result('result', source, { 'a' => { 'b' => 'result' } })
|
assert_template_result('result', source, { 'a' => { 'b' => 'result' } })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_assign_array
|
||||||
|
source = "{% assign r = [1, 2, 3] %}{{ r | join: ';' }}"
|
||||||
|
assert_template_result('1;2;3', source)
|
||||||
|
|
||||||
|
source = <<~LIQUID
|
||||||
|
{%- assign a = "one" -%}
|
||||||
|
{%- assign b = "two" -%}
|
||||||
|
{%- assign c = "three" -%}
|
||||||
|
{%- assign l = [a, b, c] -%}
|
||||||
|
{{- l | join: ", and " -}}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
assert_template_result("one, and two, and three", source)
|
||||||
|
end
|
||||||
|
|
||||||
def test_assign_score_exceeding_resource_limit
|
def test_assign_score_exceeding_resource_limit
|
||||||
t = Template.parse("{% assign foo = 42 %}{% assign bar = 23 %}")
|
t = Template.parse("{% assign foo = 42 %}{% assign bar = 23 %}")
|
||||||
t.resource_limits.assign_score_limit = 1
|
t.resource_limits.assign_score_limit = 1
|
||||||
|
|||||||
Reference in New Issue
Block a user