mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Change behavior of capture tag to use existing variables if they already have been initialized in an outer scope.
This commit is contained in:
committed by
Tobias Lütke
parent
e26f509277
commit
13f98de7f3
@@ -26,7 +26,7 @@ module Liquid
|
||||
|
||||
def render(context)
|
||||
output = super
|
||||
context[@to] = output.join
|
||||
context.scopes.last[@to.to_s] = output.join
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
class CaptureTest < Test::Unit::TestCase
|
||||
include Liquid
|
||||
|
||||
def test_captures_block_content_in_variable
|
||||
assert_template_result("test string", "{% capture 'var' %}test string{% endcapture %}{{var}}", {})
|
||||
end
|
||||
|
||||
def test_capture_to_variable_from_outer_scope_if_existing
|
||||
template_source = <<-END_TEMPLATE
|
||||
{% assign var = '' %}
|
||||
{% if true %}
|
||||
{% capture var %}first-block-string{% endcapture %}
|
||||
{% endif %}
|
||||
{% if true %}
|
||||
{% capture var %}test-string{% endcapture %}
|
||||
{% endif %}
|
||||
{{var}}
|
||||
END_TEMPLATE
|
||||
template = Template.parse(template_source)
|
||||
rendered = template.render
|
||||
assert_equal "test-string", rendered.gsub(/\s/, '')
|
||||
end
|
||||
|
||||
def test_assigning_from_capture
|
||||
template_source = <<-END_TEMPLATE
|
||||
{% assign first = '' %}
|
||||
{% assign second = '' %}
|
||||
{% for number in (1..3) %}
|
||||
{% capture first %}{{number}}{% endcapture %}
|
||||
{% assign second = first %}
|
||||
{% endfor %}
|
||||
{{ first }}-{{ second }}
|
||||
END_TEMPLATE
|
||||
template = Template.parse(template_source)
|
||||
rendered = template.render
|
||||
assert_equal "3-3", rendered.gsub(/\s/, '')
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user