mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Introduce inline snippets prototype
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,7 +5,7 @@ require 'test_helper'
|
||||
class SnippetTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def xtest_valid_inline_snippet
|
||||
def test_valid_inline_snippet
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet "input" %}
|
||||
Hey
|
||||
@@ -16,7 +16,7 @@ class SnippetTest < Minitest::Test
|
||||
assert_template_result(expected, template)
|
||||
end
|
||||
|
||||
def xtest_invalid_inline_snippet
|
||||
def test_invalid_inline_snippet
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet input %}
|
||||
Hey
|
||||
@@ -30,17 +30,16 @@ class SnippetTest < Minitest::Test
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user