mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-26 13:45:13 -07:00
snippets
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
tag_unexpected_args: "Syntax Error in '%{tag}' - Valid syntax: %{tag}"
|
tag_unexpected_args: "Syntax Error in '%{tag}' - Valid syntax: %{tag}"
|
||||||
assign: "Syntax Error in 'assign' - Valid syntax: assign [var] = [source]"
|
assign: "Syntax Error in 'assign' - Valid syntax: assign [var] = [source]"
|
||||||
capture: "Syntax Error in 'capture' - Valid syntax: capture [var]"
|
capture: "Syntax Error in 'capture' - Valid syntax: capture [var]"
|
||||||
|
snippet: "Syntax Error in 'snippet' - Valid syntax: snippet [quoted string]"
|
||||||
case: "Syntax Error in 'case' - Valid syntax: case [condition]"
|
case: "Syntax Error in 'case' - Valid syntax: case [condition]"
|
||||||
case_invalid_when: "Syntax Error in tag 'case' - Valid when condition: {% when [condition] [or condition2...] %}"
|
case_invalid_when: "Syntax Error in tag 'case' - Valid when condition: {% when [condition] [or condition2...] %}"
|
||||||
case_invalid_else: "Syntax Error in tag 'case' - Valid else condition: {% else %} (no parameters) "
|
case_invalid_else: "Syntax Error in tag 'case' - Valid else condition: {% else %} (no parameters) "
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ require_relative "tags/comment"
|
|||||||
require_relative "tags/raw"
|
require_relative "tags/raw"
|
||||||
require_relative "tags/render"
|
require_relative "tags/render"
|
||||||
require_relative "tags/cycle"
|
require_relative "tags/cycle"
|
||||||
|
require_relative "tags/snippet"
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
module Tags
|
module Tags
|
||||||
@@ -42,6 +43,7 @@ module Liquid
|
|||||||
'if' => If,
|
'if' => If,
|
||||||
'echo' => Echo,
|
'echo' => Echo,
|
||||||
'tablerow' => TableRow,
|
'tablerow' => TableRow,
|
||||||
|
'snippet' => Snippet,
|
||||||
}.freeze
|
}.freeze
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -66,6 +66,14 @@ 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 '---------------'
|
||||||
|
puts template_name
|
||||||
|
puts '---------------'
|
||||||
|
if (inline_snippet = context.registers[:inline_snippet][template_name])
|
||||||
|
puts '!!!'
|
||||||
|
p(inline_snippet)
|
||||||
|
end
|
||||||
|
|
||||||
partial = PartialCache.load(
|
partial = PartialCache.load(
|
||||||
template_name,
|
template_name,
|
||||||
context: context,
|
context: context,
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Liquid
|
||||||
|
# @liquid_public_docs
|
||||||
|
# @liquid_type tag
|
||||||
|
# @liquid_category theme
|
||||||
|
# @liquid_name snippet
|
||||||
|
# @liquid_summary
|
||||||
|
# Creates a new variable with a string value.
|
||||||
|
# @liquid_description
|
||||||
|
# You can create complex strings with Liquid logic and variables.
|
||||||
|
# @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
|
||||||
|
|
||||||
|
def initialize(tag_name, markup, options)
|
||||||
|
super
|
||||||
|
if markup =~ SYNTAX
|
||||||
|
@to = Regexp.last_match(1)
|
||||||
|
else
|
||||||
|
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render(context)
|
||||||
|
context.registers[:inline_snippet] ||= {}
|
||||||
|
context.registers[@to] = @body
|
||||||
|
''
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class SnippetTest < Minitest::Test
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def xtest_valid_inline_snippet
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% snippet "input" %}
|
||||||
|
Hey
|
||||||
|
{% endsnippet %}
|
||||||
|
LIQUID
|
||||||
|
expected = ''
|
||||||
|
|
||||||
|
assert_template_result(expected, template)
|
||||||
|
end
|
||||||
|
|
||||||
|
def xtest_invalid_inline_snippet
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% snippet input %}
|
||||||
|
Hey
|
||||||
|
{% endsnippet %}
|
||||||
|
LIQUID
|
||||||
|
expected = "Syntax Error in 'snippet' - Valid syntax: snippet [quoted string]"
|
||||||
|
|
||||||
|
assert_match_syntax_error(expected, template)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_inline_snippet
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% snippet "input" %}
|
||||||
|
Hey
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{%- render "input" %}{% render "input" %}
|
||||||
|
LIQUID
|
||||||
|
expected = <<~OUTPUT.strip
|
||||||
|
HeyHey
|
||||||
|
OUTPUT
|
||||||
|
|
||||||
|
assert_template_result(expected, template, partials: {
|
||||||
|
'input' => 'Hey',
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user