mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 10:52:48 -07:00
Create SnippetDrop and set in scope
This commit is contained in:
committed by
Guilherme Carreiro
parent
1eca707c4a
commit
12bbbc4537
@@ -67,6 +67,7 @@ require 'liquid/i18n'
|
|||||||
require 'liquid/drop'
|
require 'liquid/drop'
|
||||||
require 'liquid/tablerowloop_drop'
|
require 'liquid/tablerowloop_drop'
|
||||||
require 'liquid/forloop_drop'
|
require 'liquid/forloop_drop'
|
||||||
|
require 'liquid/snippet_drop'
|
||||||
require 'liquid/extensions'
|
require 'liquid/extensions'
|
||||||
require 'liquid/errors'
|
require 'liquid/errors'
|
||||||
require 'liquid/interrupts'
|
require 'liquid/interrupts'
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Liquid
|
||||||
|
class SnippetDrop < Drop
|
||||||
|
attr_reader :body
|
||||||
|
|
||||||
|
def initialize(body)
|
||||||
|
super()
|
||||||
|
@body = body
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
'SnippetDrop'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -42,6 +42,10 @@ module Liquid
|
|||||||
@is_for_loop
|
@is_for_loop
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inherit_context?
|
||||||
|
@inherit_context
|
||||||
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
render_tag(context, output)
|
render_tag(context, output)
|
||||||
end
|
end
|
||||||
@@ -51,14 +55,15 @@ 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)
|
||||||
|
|
||||||
# Inline snippets take precedence over external snippets
|
if context[template_name].is_a?(Liquid::SnippetDrop)
|
||||||
if (inline_snippet = context.registers[:inline_snippet][template_name])
|
snippet_drop = context[template_name]
|
||||||
inner_context = context.new_isolated_subcontext
|
inner_context = context.new_isolated_subcontext
|
||||||
snippet_body = inline_snippet[:body]
|
|
||||||
|
|
||||||
context.scopes.each do |scope|
|
if inherit_context?
|
||||||
scope.each do |key, value|
|
context.scopes.each do |scope|
|
||||||
inner_context[key] = value
|
scope.each do |key, value|
|
||||||
|
inner_context[key] = value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -66,7 +71,7 @@ module Liquid
|
|||||||
inner_context[key] = context.evaluate(value)
|
inner_context[key] = context.evaluate(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
return output << snippet_body.render(inner_context)
|
return output << snippet_drop.body.render(inner_context)
|
||||||
end
|
end
|
||||||
|
|
||||||
partial = PartialCache.load(
|
partial = PartialCache.load(
|
||||||
|
|||||||
@@ -25,12 +25,14 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render_to_output_buffer(context, output)
|
||||||
context.registers[:inline_snippet] ||= {}
|
snippet_drop = SnippetDrop.new(@body)
|
||||||
context.registers[:inline_snippet][@to] = {
|
context.scopes.last[@to] = snippet_drop
|
||||||
body: @body,
|
output
|
||||||
}
|
end
|
||||||
''
|
|
||||||
|
def blank?
|
||||||
|
true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -229,21 +229,18 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_parent_context_variable
|
def test_render_inline_snippet_without_outside_context
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% assign color_scheme = 'dark' %}
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
{% snippet header %}
|
{% snippet header %}
|
||||||
{% doc %}
|
|
||||||
@param {string} message - Message.
|
|
||||||
{% enddoc %}
|
|
||||||
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
<div class="header header--{{ color_scheme }}">
|
||||||
{{ message }}
|
{{ message }}
|
||||||
</div>
|
</div>
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
{%- render "header", message: "Welcome to my site" -%}
|
|
||||||
|
{% render "header", message: 'Welcome!' %}
|
||||||
LIQUID
|
LIQUID
|
||||||
expected = <<~OUTPUT
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
@@ -251,49 +248,44 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="header header--">
|
||||||
|
Welcome!
|
||||||
|
</div>
|
||||||
|
OUTPUT
|
||||||
|
|
||||||
|
assert_template_result(expected, template)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_inline_snippet_with_outside_context
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
|
{% snippet header %}
|
||||||
|
<div class="header header--{{ color_scheme }}">
|
||||||
|
{{ message }}
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
|
||||||
|
{% render "header", ..., message: 'Welcome!' %}
|
||||||
|
LIQUID
|
||||||
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--dark">
|
<div class="header header--dark">
|
||||||
Welcome to my site
|
Welcome!
|
||||||
</div>
|
</div>
|
||||||
OUTPUT
|
OUTPUT
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_deeply_nested_snippets
|
def test_inline_snippet_local_scope_takes_precedence
|
||||||
template = <<~LIQUID.strip
|
|
||||||
{% assign color_scheme = 'first-color' %}
|
|
||||||
{% snippet first %}
|
|
||||||
{% assign color_scheme = 'second-color' %}
|
|
||||||
{% snippet second %}
|
|
||||||
{% assign color_scheme = 'third-color' %}
|
|
||||||
{% snippet third %}
|
|
||||||
<div class="header header--{{ color_scheme }}">
|
|
||||||
This is a header
|
|
||||||
</div>
|
|
||||||
{% endsnippet %}
|
|
||||||
{%- render "third" -%}
|
|
||||||
{% endsnippet %}
|
|
||||||
{%- render "second" -%}
|
|
||||||
{% endsnippet %}
|
|
||||||
|
|
||||||
{%- render "first" -%}
|
|
||||||
LIQUID
|
|
||||||
expected = <<~OUTPUT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--third-color">
|
|
||||||
This is a header
|
|
||||||
</div>
|
|
||||||
OUTPUT
|
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_render_snippet_with_variables_in_both_scopes
|
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% assign color_scheme = 'dark' %}
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
@@ -306,7 +298,9 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
{{ color_scheme }}
|
{{ color_scheme }}
|
||||||
|
|
||||||
{%- render "header", message: 'Welcome to my site' -%}
|
{% render "header", ..., message: 'Welcome!' %}
|
||||||
|
|
||||||
|
{{ color_scheme }}
|
||||||
LIQUID
|
LIQUID
|
||||||
expected = <<~OUTPUT
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
@@ -315,39 +309,126 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
dark
|
dark
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class="header header--light">
|
<div class="header header--light">
|
||||||
Welcome to my site
|
Welcome!
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
dark
|
||||||
|
OUTPUT
|
||||||
|
|
||||||
|
assert_template_result(expected, template)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_captured_snippet
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
|
{% snippet header %}
|
||||||
|
<div class="header header--{{ color_scheme }}">
|
||||||
|
{{ message }}
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% capture up_header %}
|
||||||
|
{% render "header", ..., message: 'Welcome!' %}
|
||||||
|
{% endcapture %}
|
||||||
|
|
||||||
|
{{ up_header | upcase }}
|
||||||
|
|
||||||
|
{{ header | upcase }}
|
||||||
|
|
||||||
|
{{ header }}
|
||||||
|
LIQUID
|
||||||
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<DIV CLASS="HEADER HEADER--DARK">
|
||||||
|
WELCOME!
|
||||||
|
</DIV>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
SNIPPETDROP
|
||||||
|
|
||||||
|
SnippetDrop
|
||||||
|
OUTPUT
|
||||||
|
|
||||||
|
assert_template_result(expected, template)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_snippets_as_arguments
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
|
{% snippet header %}
|
||||||
|
<div class="header header--{{ color_scheme }}">
|
||||||
|
{{ message }}
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% snippet main %}
|
||||||
|
{% assign color_scheme = 'auto' %}
|
||||||
|
|
||||||
|
<div class="main main--{{ color_scheme }}">
|
||||||
|
{% render "header", ..., message: 'Welcome!' %}
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% render "main", header: header %}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="main main--auto">
|
||||||
|
|
||||||
|
<div class="header header--auto">
|
||||||
|
Welcome!
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
OUTPUT
|
OUTPUT
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
# def test_render_snippets_as_arguments
|
# def test_render_inline_snippet_inside_loop
|
||||||
# template = <<~LIQUID.strip
|
# template = <<~LIQUID.strip
|
||||||
# {% assign color_scheme = 'dark' %}
|
# {% assign color_scheme = 'dark' %}
|
||||||
|
# {% assign array = '1,2,3' | split: ',' %}
|
||||||
|
|
||||||
# {% snippet main_header %}
|
# {% for i in array %}
|
||||||
# {% assign color_scheme = 'auto' %}
|
|
||||||
|
|
||||||
# <div class="main main--{{ color_scheme }}">
|
|
||||||
# {%- render "header", message: 'Welcome to my site' -%}
|
|
||||||
# </div>
|
|
||||||
# {% endsnippet %}
|
|
||||||
|
|
||||||
# {% snippet header %}
|
# {% snippet header %}
|
||||||
# <div class="header header--{{ color_scheme }}">
|
# <div class="header header--{{ color_scheme }}">
|
||||||
# {{ message }}
|
# {{ message }} {{ i }}
|
||||||
# </div>
|
# </div>
|
||||||
# {% endsnippet %}
|
# {% endsnippet %}
|
||||||
|
# {% endfor %}
|
||||||
|
|
||||||
# {%- render "main_header", header: header -%}
|
# {% render "header", ..., message: '👉' %}
|
||||||
# LIQUID
|
# LIQUID
|
||||||
# expected = <<~OUTPUT
|
# expected = <<~OUTPUT
|
||||||
# <div class="main main--auto">
|
|
||||||
# <div class="header header--auto">
|
# <div class="header header--dark">
|
||||||
# Welcome to my site
|
# 👉 3
|
||||||
# </div>
|
|
||||||
# </div>
|
# </div>
|
||||||
# OUTPUT
|
# OUTPUT
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user