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:
Tobi Lutke
2025-12-31 12:33:14 -04:00
parent 6c0d599c89
commit 652b9c0897
+6 -3
View File
@@ -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