Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/liquid/tags/if.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def render_to_output_buffer(context, output)
block.evaluate(context),
)

if result
if result.respond_to?(:truthy?) ? result.truthy? : result
return block.attachment.render_to_output_buffer(context, output)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/liquid/tags/unless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def render_to_output_buffer(context, output)
first_block.evaluate(context),
)

unless result
unless result.respond_to?(:truthy?) ? result.truthy? : result
return first_block.attachment.render_to_output_buffer(context, output)
end

Expand All @@ -36,7 +36,7 @@ def render_to_output_buffer(context, output)
block.evaluate(context),
)

if result
if result.respond_to?(:truthy?) ? result.truthy? : result
return block.attachment.render_to_output_buffer(context, output)
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/unit/tags/if_tag_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ def test_if_nodelist
template = Liquid::Template.parse('{% if true %}IF{% else %}ELSE{% endif %}')
assert_equal(['IF', 'ELSE'], template.root.nodelist[0].nodelist.map(&:nodelist).flatten)
end

def test_support_truthy
falsey = Class.new(Liquid::Drop) { def truthy? = false }.new
truthy = Class.new(Liquid::Drop) { def truthy? = true }.new
template = '{% if obj %}IF{% else %}ELSE{% endif %}'
assert_template_result('ELSE', template, { 'obj' => falsey })
assert_template_result('IF', template, { 'obj' => truthy })
assert_template_result('IF', template, { 'obj' => "foo" }) # truthy? not defined
end
end
14 changes: 14 additions & 0 deletions test/unit/tags/unless_tag_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'test_helper'

class IfTagUnitTest < Minitest::Test
def test_support_truthy
falsey = Class.new(Liquid::Drop) { def truthy? = false }.new
truthy = Class.new(Liquid::Drop) { def truthy? = true }.new
template = '{% unless obj %}IF{% else %}ELSE{% endunless %}'
assert_template_result('ELSE', template, { 'obj' => truthy })
assert_template_result('IF', template, { 'obj' => falsey })
assert_template_result('ELSE', template, { 'obj' => "truthy? not defined" })
end
end