Skip to content

Add method to render template to string instead of a file #896

Description

@bogdan

Sometimes rendering a template to file doesn't have enough flexibility and it is better to render template to string and then put content to file manually via inject_into_file etc.
As a bonus, this method can be easier to use to test templates.

Here is the draft from ChatGPT of what is needed:

    # Renders an ERB template from the source path and returns it as a string.
    #
    # ==== Parameters
    # source<String>:: the relative path to the source root.
    # context<Binding>:: binding context for the template (default: instance_eval binding).
    #
    # ==== Returns
    # String:: the rendered template content.
    #
    # ==== Example
    #   render_template("README")
    #
    def render_template(source, context: instance_eval("binding"))
      source = File.expand_path(find_in_source_paths(source.to_s))
      capturable_erb = CapturableERB.new(::File.binread(source), trim_mode: "-", eoutvar: "@output_buffer")
      capturable_erb.tap { |erb| erb.filename = source }.result(context)
    end

    # Gets an ERB template at the relative source, executes it, and makes a copy
    # at the relative destination. If the destination is not given it's assumed
    # to be equal to the source removing .tt from the filename.
    #
    # ==== Parameters
    # source<String>:: the relative path to the source root.
    # destination<String>:: the relative path to the destination root.
    # config<Hash>:: give :verbose => false to not log the status.
    #
    # ==== Examples
    #
    #   template "README", "doc/README"
    #
    #   template "doc/README"
    #
    def template(source, *args, &block)
      config = args.last.is_a?(Hash) ? args.pop : {}
      destination = args.first || source.sub(/#{TEMPLATE_EXTNAME}$/, "")
      content = render_template(source, context: config.delete(:context) || instance_eval("binding"))
      content = yield(content) if block
      create_file destination, nil, config { content }
    end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions