Register custom renderers
Description
Frontman supports HAML, ERB and Markdown out of the box. However, it could be nice to extend on this and give users the possibility to implement and add custom renderers, so that they can use even more templating languages.
Next steps
- Write an RFC for the addition of custom renderers.
Proposed syntax
A user could add custom renderers (or community-created renderers) from their config.rb, with the following approach:
require 'frontman-community-renderer' # Renderer from e.g. external package
require './lib/custom_renderer' # custom renderer in own project
Frontman::Config.set :custom_renderers, { comm: CommunityRenderer.instance, cust: CustomRenderer.instance }
In lib/frontman/renderers/renderer_resolver.rb, we can then merge the default renderers, and the custom renderers, making them all available to use, like this:
def get_renderer(extension)
renderers = {
'erb': Frontman::ErbRenderer.instance,
'md': Frontman::MarkdownRenderer.instance,
'haml': Frontman::HamlRenderer.instance,
}.merge(Frontman::Config.get(:custom_renderers, fallback: {}))
renderers[extension.to_sym]
end
How about we abstract away .instance part
While writing this out I also noticed, theres likely a chance to improve performance via caching of the renderers hash
Frontman::Config.set :custom_renderers, { comm: CommunityRenderer, cust: CustomRenderer }
def get_renderer(extension)
@renderers ||= begin
renderers = {
'erb': Frontman::ErbRenderer.instance,
'md': Frontman::MarkdownRenderer.instance,
'haml': Frontman::HamlRenderer.instance,
}.merge(Frontman::Config.get(:custom_renderers, fallback: {}))
custom_renderers = Frontman::Config.get(:custom_renderers, fallback: {})
custom_renderers.each do |k,v|
custom_renderers[k] = v.instance
end
renderers
end
@renderers[extension.to_sym]
end
Great idea, let's try this out 😄