Module: Charming::Presentation::Templates
- Defined in:
- lib/charming/presentation/templates.rb,
lib/charming/presentation/templates/erb_handler.rb
Overview
Templates resolves and renders view templates by name. Template handlers are registered for file extensions (e.g., ‘.tui.erb`) and the resolver searches `app/views/<name><ext>` under the application root, falling back through registered extensions when the first match is not found.
Defined Under Namespace
Classes: ErbHandler, ResolvedTemplate
Constant Summary collapse
- MissingTemplateError =
Raised when no template file matches the given name under the application root.
Class.new(Error)
Class Method Summary collapse
-
.handlers ⇒ Object
Hash of registered handlers keyed by extension.
-
.register(extension, handler) ⇒ Object
Registers a template handler for a file extension (e.g., “.tui.erb” => ErbHandler).
-
.resolve(name, root: nil) ⇒ Object
Resolves a template by name under ‘app/views` of root (defaults to the current working directory).
Class Method Details
.handlers ⇒ Object
Hash of registered handlers keyed by extension. Populated by ‘register`.
44 45 46 |
# File 'lib/charming/presentation/templates.rb', line 44 def handlers @handlers ||= {} end |
.register(extension, handler) ⇒ Object
Registers a template handler for a file extension (e.g., “.tui.erb” => ErbHandler). The handler responds to ‘.render(path, view)`.
24 25 26 |
# File 'lib/charming/presentation/templates.rb', line 24 def register(extension, handler) handlers[extension] = handler end |
.resolve(name, root: nil) ⇒ Object
Resolves a template by name under ‘app/views` of root (defaults to the current working directory). Raises MissingTemplateError when no matching file exists.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/charming/presentation/templates.rb', line 30 def resolve(name, root: nil) views_root = File.join(root || Dir.pwd, "app", "views") searched_paths = candidate_paths(views_root, name.to_s) searched_paths.each do |path| next unless File.file?(path) return ResolvedTemplate.new(path: path, handler: handler_for(path)) end raise MissingTemplateError, "Missing template #{name.inspect}. Searched: #{searched_paths.join(", ")}" end |