Skip to content

Commit 652b9c0

Browse files
committed
Add to_ruby convention for custom tag compilation
Tags can now implement to_ruby(code, compiler) to provide their own optimized Ruby code generation. This allows: 1. Third-party tags to participate in compilation 2. Shopify-specific tags to be lowered to Ruby 3. Better performance for frequently-used custom tags Priority order for tag compilation: 1. tag.to_ruby(code, compiler) - Custom implementation 2. Built-in compiler (IfCompiler, ForCompiler, etc.) 3. Yield to caller as external tag Also updated PROJECT.md with: - External calls yielding documentation - to_ruby convention documentation
1 parent 6c0d599 commit 652b9c0

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/liquid/compile/ruby_compiler.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,14 @@ def compile_string(str, code)
317317
end
318318

319319
def compile_tag(tag, code)
320-
compiler_class = find_tag_compiler(tag)
321-
if compiler_class
320+
# First, check if the tag implements to_ruby (custom compilation)
321+
if tag.respond_to?(:to_ruby)
322+
tag.to_ruby(code, self)
323+
# Then check for a built-in compiler class
324+
elsif (compiler_class = find_tag_compiler(tag))
322325
compiler_class.compile(tag, self, code)
323326
else
324-
# Unknown tag - delegate to the original tag's render method at runtime
327+
# Unknown tag - yield to caller at runtime
325328
compile_external_tag(tag, code)
326329
end
327330
end

0 commit comments

Comments
 (0)