mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
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
This commit is contained in:
@@ -317,11 +317,14 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def compile_tag(tag, code)
|
def compile_tag(tag, code)
|
||||||
compiler_class = find_tag_compiler(tag)
|
# First, check if the tag implements to_ruby (custom compilation)
|
||||||
if compiler_class
|
if tag.respond_to?(:to_ruby)
|
||||||
|
tag.to_ruby(code, self)
|
||||||
|
# Then check for a built-in compiler class
|
||||||
|
elsif (compiler_class = find_tag_compiler(tag))
|
||||||
compiler_class.compile(tag, self, code)
|
compiler_class.compile(tag, self, code)
|
||||||
else
|
else
|
||||||
# Unknown tag - delegate to the original tag's render method at runtime
|
# Unknown tag - yield to caller at runtime
|
||||||
compile_external_tag(tag, code)
|
compile_external_tag(tag, code)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user