Skip to content

Instantly share code, notes, and snippets.

@marcuxyz
Last active November 26, 2020 15:07
Show Gist options
  • Save marcuxyz/a849fd53fae125454df2541fa1d4f964 to your computer and use it in GitHub Desktop.
Save marcuxyz/a849fd53fae125454df2541fa1d4f964 to your computer and use it in GitHub Desktop.
Yield and Render in ERB Without Ruby on Rails
require 'erb'

class Template
    def render(template_name="base")
        ERB.new(File.read("#{template_name}.erb")).result(binding)
    end
end

result = Template.new.render do
    ERB.new(File.read("index.erb")).result
end

puts result

base.erb

<main>
    <%= render "footer" %>
    <%=yield %>
</main>

index.erb

<h1>Index Page</h1>

footer.erb

<h1>Footer here</h1>

code

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