-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b233b3d
commit ffe4886
Showing
6 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |