Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register custom renderers #10

Open
DevinCodes opened this issue Jul 1, 2020 · 3 comments
Open

Register custom renderers #10

DevinCodes opened this issue Jul 1, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@DevinCodes
Copy link
Collaborator

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.
@DevinCodes DevinCodes added the enhancement New feature or request label Jul 1, 2020
@westonganger westonganger mentioned this issue Aug 30, 2020
4 tasks
@DevinCodes
Copy link
Collaborator Author

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

@westonganger
Copy link
Contributor

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

@DevinCodes
Copy link
Collaborator Author

Great idea, let's try this out 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants