Skip to content

Commit

Permalink
snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilGenius13 committed Sep 19, 2024
1 parent b233b3d commit ffe4886
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/liquid/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
tag_unexpected_args: "Syntax Error in '%{tag}' - Valid syntax: %{tag}"
assign: "Syntax Error in 'assign' - Valid syntax: assign [var] = [source]"
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_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) "
Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/partial_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.load(template_name, context:, parse_context:)
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

Expand Down
2 changes: 2 additions & 0 deletions lib/liquid/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require_relative "tags/raw"
require_relative "tags/render"
require_relative "tags/cycle"
require_relative "tags/snippet"

module Liquid
module Tags
Expand All @@ -42,6 +43,7 @@ module Tags
'if' => If,
'echo' => Echo,
'tablerow' => TableRow,
'snippet' => Snippet,
}.freeze
end
end
8 changes: 8 additions & 0 deletions lib/liquid/tags/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ def render_tag(context, output)
template_name = @template_name_expr
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(
template_name,
context: context,
Expand Down
36 changes: 36 additions & 0 deletions lib/liquid/tags/snippet.rb
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
46 changes: 46 additions & 0 deletions test/integration/tags/snippet_test.rb
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

0 comments on commit ffe4886

Please sign in to comment.