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
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
+2 -5
View File
@@ -66,12 +66,9 @@ module Liquid
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(
+13 -5
View File
@@ -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
@@ -29,8 +27,18 @@ module Liquid
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