Introduce inline snippets prototype

This commit is contained in:
Guilherme Carreiro
2024-09-20 11:24:12 +02:00
parent ffe48869be
commit b397513f8b
4 changed files with 24 additions and 20 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ module Liquid
return cached if cached return cached if cached
file_system = context.registers[:file_system] 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 parse_context.partial = true
+2 -5
View File
@@ -66,12 +66,9 @@ module Liquid
template_name = @template_name_expr template_name = @template_name_expr
raise ::ArgumentError unless template_name.is_a?(String) raise ::ArgumentError unless template_name.is_a?(String)
puts '---------------' # Inline snippets take precedence over external snippets
puts template_name
puts '---------------'
if (inline_snippet = context.registers[:inline_snippet][template_name]) if (inline_snippet = context.registers[:inline_snippet][template_name])
puts '!!!' return output << inline_snippet.render(context)
p(inline_snippet)
end end
partial = PartialCache.load( partial = PartialCache.load(
+13 -5
View File
@@ -6,15 +6,13 @@ module Liquid
# @liquid_category theme # @liquid_category theme
# @liquid_name snippet # @liquid_name snippet
# @liquid_summary # @liquid_summary
# Creates a new variable with a string value. # Creates a new inline snippet using a string value as the identifier.
# @liquid_description # @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 # @liquid_syntax
# {% snippet "input" %} # {% snippet "input" %}
# value # value
# {% endsnippet %} # {% 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 class Snippet < Block
SYNTAX = /(#{QuotedString}+)/o SYNTAX = /(#{QuotedString}+)/o
@@ -29,8 +27,18 @@ module Liquid
def render(context) def render(context)
context.registers[:inline_snippet] ||= {} context.registers[:inline_snippet] ||= {}
context.registers[@to] = @body context.registers[:inline_snippet][snippet_id] = snippet_body
'' ''
end end
private
def snippet_id
@to[1, @to.size - 2]
end
def snippet_body
@body
end
end end
end end
+8 -9
View File
@@ -5,7 +5,7 @@ require 'test_helper'
class SnippetTest < Minitest::Test class SnippetTest < Minitest::Test
include Liquid include Liquid
def xtest_valid_inline_snippet def test_valid_inline_snippet
template = <<~LIQUID.strip template = <<~LIQUID.strip
{% snippet "input" %} {% snippet "input" %}
Hey Hey
@@ -16,7 +16,7 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template) assert_template_result(expected, template)
end end
def xtest_invalid_inline_snippet def test_invalid_inline_snippet
template = <<~LIQUID.strip template = <<~LIQUID.strip
{% snippet input %} {% snippet input %}
Hey Hey
@@ -30,17 +30,16 @@ class SnippetTest < Minitest::Test
def test_render_inline_snippet def test_render_inline_snippet
template = <<~LIQUID.strip template = <<~LIQUID.strip
{% snippet "input" %} {% snippet "input" %}
Hey Hey
{% endsnippet %} {% endsnippet %}
{%- render "input" %}{% render "input" %} {%- render "input" -%}
LIQUID LIQUID
expected = <<~OUTPUT.strip expected = <<~OUTPUT
HeyHey
Hey
OUTPUT OUTPUT
assert_template_result(expected, template, partials: { assert_template_result(expected, template)
'input' => 'Hey',
})
end end
end end