Skip to content

Commit

Permalink
Merge pull request #1775 from Shopify/allow-nil-template-source
Browse files Browse the repository at this point in the history
allow non-string template source
  • Loading branch information
ggmichaelgo authored Jan 12, 2024
2 parents 3ac7e47 + 6a0fe3f commit cf76c0b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/liquid/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def initialize
# Returns self for easy chaining
def parse(source, options = {})
parse_context = configure_options(options)
source = source.to_s.to_str

unless source.valid_encoding?
raise SyntaxError, parse_context.locale.t("errors.syntax.invalid_template_encoding")
Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/tokenizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Tokenizer
attr_reader :line_number, :for_liquid_tag

def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false)
@source = source.to_s.to_str
@source = source
@line_number = line_number || (line_numbers ? 1 : nil)
@for_liquid_tag = for_liquid_tag
@offset = 0
Expand Down
6 changes: 6 additions & 0 deletions test/integration/template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,10 @@ def test_raises_error_with_invalid_utf8

assert_equal('Liquid syntax error: Invalid template encoding', e.message)
end

def test_allows_non_string_values_as_source
assert_equal('', Template.parse(nil).render)
assert_equal('1', Template.parse(1).render)
assert_equal('true', Template.parse(true).render)
end
end

0 comments on commit cf76c0b

Please sign in to comment.