diff --git a/lib/liquid/tags/table_row.rb b/lib/liquid/tags/table_row.rb
index 5371f70a..87f3e096 100644
--- a/lib/liquid/tags/table_row.rb
+++ b/lib/liquid/tags/table_row.rb
@@ -51,7 +51,7 @@ module Liquid
collection = Utils.slice_collection(collection, from, to)
length = collection.length
- cols = @attributes['cols'].nil? ? length : context.evaluate(@attributes['cols']).to_i
+ cols = @attributes.key?('cols') ? context.evaluate(@attributes['cols']).to_i : length
output << "
\n"
context.stack do
diff --git a/test/integration/tags/table_row_test.rb b/test/integration/tags/table_row_test.rb
index 1f6edda4..8556ed2d 100644
--- a/test/integration/tags/table_row_test.rb
+++ b/test/integration/tags/table_row_test.rb
@@ -66,6 +66,20 @@ class TableRowTest < Minitest::Test
{ 'characters' => '' })
end
+ def test_cols_nil_constant_same_as_evaluated_nil_expression
+ expect = "
\n" \
+ "| false | " \
+ "false | " \
+ "
\n"
+
+ assert_template_result(expect,
+ "{% tablerow i in (1..2) cols:nil %}{{ tablerowloop.col_last }}{% endtablerow %}")
+
+ assert_template_result(expect,
+ "{% tablerow i in (1..2) cols:var %}{{ tablerowloop.col_last }}{% endtablerow %}",
+ { "var" => nil })
+ end
+
def test_tablerow_loop_drop_attributes
template = <<~LIQUID.chomp
{% tablerow i in (1...2) %}