From 652b9c0897f9d3fcd2e39200fc24f4dcd0396deb Mon Sep 17 00:00:00 2001 From: Tobi Lutke Date: Wed, 31 Dec 2025 12:33:14 -0400 Subject: [PATCH] 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 --- lib/liquid/compile/ruby_compiler.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/liquid/compile/ruby_compiler.rb b/lib/liquid/compile/ruby_compiler.rb index 98fd3db6..d279dbba 100644 --- a/lib/liquid/compile/ruby_compiler.rb +++ b/lib/liquid/compile/ruby_compiler.rb @@ -317,11 +317,14 @@ module Liquid end def compile_tag(tag, code) - compiler_class = find_tag_compiler(tag) - if compiler_class + # First, check if the tag implements to_ruby (custom compilation) + 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) 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) end end