Skip to content

Commit

Permalink
Introduce inline snippets prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
karreiro committed Sep 20, 2024
1 parent ffe4886 commit b397513
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/liquid/partial_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.load(template_name, context:, parse_context:)
return cached if cached

file_system = context.registers[:file_system]
source = file_system.read_template_file(template_name)
source = file_system.read_template_file(template_name)

parse_context.partial = true

Expand Down
7 changes: 2 additions & 5 deletions lib/liquid/tags/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,9 @@ def render_tag(context, output)
template_name = @template_name_expr
raise ::ArgumentError unless template_name.is_a?(String)

puts '---------------'
puts template_name
puts '---------------'
# Inline snippets take precedence over external snippets
if (inline_snippet = context.registers[:inline_snippet][template_name])
puts '!!!'
p(inline_snippet)
return output << inline_snippet.render(context)
end

partial = PartialCache.load(
Expand Down
18 changes: 13 additions & 5 deletions lib/liquid/tags/snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ module Liquid
# @liquid_category theme
# @liquid_name snippet
# @liquid_summary
# Creates a new variable with a string value.
# Creates a new inline snippet using a string value as the identifier.
# @liquid_description
# You can create complex strings with Liquid logic and variables.
# You can create inline snippets to make your Liquid code more modular.
# @liquid_syntax
# {% snippet "input" %}
# value
# {% endsnippet %}
# @liquid_syntax_keyword variable The name of the variable being created.
# @liquid_syntax_keyword value The value you want to assign to the variable.
class Snippet < Block
SYNTAX = /(#{QuotedString}+)/o

Expand All @@ -29,8 +27,18 @@ def initialize(tag_name, markup, options)

def render(context)
context.registers[:inline_snippet] ||= {}
context.registers[@to] = @body
context.registers[:inline_snippet][snippet_id] = snippet_body
''
end

private

def snippet_id
@to[1, @to.size - 2]
end

def snippet_body
@body
end
end
end
17 changes: 8 additions & 9 deletions test/integration/tags/snippet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class SnippetTest < Minitest::Test
include Liquid

def xtest_valid_inline_snippet
def test_valid_inline_snippet
template = <<~LIQUID.strip
{% snippet "input" %}
Hey
Expand All @@ -16,7 +16,7 @@ def xtest_valid_inline_snippet
assert_template_result(expected, template)
end

def xtest_invalid_inline_snippet
def test_invalid_inline_snippet
template = <<~LIQUID.strip
{% snippet input %}
Hey
Expand All @@ -30,17 +30,16 @@ def xtest_invalid_inline_snippet
def test_render_inline_snippet
template = <<~LIQUID.strip
{% snippet "input" %}
Hey
Hey
{% endsnippet %}
{%- render "input" %}{% render "input" %}
{%- render "input" -%}
LIQUID
expected = <<~OUTPUT.strip
HeyHey
expected = <<~OUTPUT
Hey
OUTPUT

assert_template_result(expected, template, partials: {
'input' => 'Hey',
})
assert_template_result(expected, template)
end
end

0 comments on commit b397513

Please sign in to comment.