$ show rtomayko/tilt
Tilt is a Ruby library that provides a generic interface to a multitude of template engines. It abstracts away the differences between engines like ERB, Haml, Markdown variants, and many others, offering a unified API for template loading, compilation, and rendering. The architecture is built around a central Tilt module, a Mapping class that associates file extensions with template implementations, and a base Template class that individual engines subclass. The library supports lazy loading of template engines, template caching, and pipeline composition. It is widely used in the Ruby ecosystem, particularly in web frameworks like Sinatra and Rack. The repository under rtomayko/tilt is no longer maintained, as the project has moved to jeremyevans/tilt, but this version represents the stable 2.x series. The code is well-structured and demonstrates good software engineering practices, though some template implementations are deprecated or rely on older gems.
The LLM identified 3 files worth highlighting for reuse:
A generic class that maps file extensions to template implementation classes. Supports eager and lazy registration, unregistration, lookup by file name, and listing extensions for a template class. The lazy loading mechanism tries to require the underlying library only when needed, and falls back through a list of candidates. This component is self-contained and could be extracted as a standalone registry for any file-extension-to-class mapping scenario.
The base class for all template implementations. It handles file loading, encoding detection, template compilation, caching of compiled methods, and rendering with scope and locals. It provides a precompiled_template method that subclasses override to generate Ruby source code, and then compiles that source into an UnboundMethod for efficient rendering. The class also supports a compiled_path option for writing compiled templates to files. This is a well-crafted abstract base that could be reused for any template engine that compiles to Ruby.
A superclass for pipeline templates that compose multiple template processors in sequence. The output of each template is fed as input to the next. The pipeline is defined by a constant TEMPLATES array of [template_class, options] pairs. This pattern is useful for combining processors like ERB and SCSS into a single template. The class is clean and minimal, and the pipeline concept could be extracted for any chain-of-responsibility transformation.