Releases: seanpdoyle/html_page
Releases · seanpdoyle/html_page
v0.1.0
This gem simplifies injecting content into an existing HTML document.
Given an HTML document as a string, HtmlPage::Renderer
can inject strings of
content into the <head>
and <body>
:
renderer = HtmlPage::Renderer.new(
content: "<html><head></head><body></body></html>",
head: "<title>Appended to the head!</title>",
body: "<h1>Appended to the body!</h1>",
)
renderer.render #=> "<html><head><title>Appended to the head!</title></head><body><h1>Appended to the body</h1></body></html>"
The gem also includes the HtmlPage::Capture
class.
Given a Rails helpers as a context
along with a block, the Capture
class
simplifies appending content captured from a Rails view into the HTML's <head>
and <body>
tags.
For example, given the render_html_with_modifications
defined as:
require "html_page/capture"
module MyRailsHelper
def render_html_with_modifications(html_page_as_string, &block)
capturer = HtmlPage::Capture.new(self, &block)
head, body = capturer.capture
renderer = HtmlPage::Renderer.new(
content: html_page_as_string,
head: head,
body: body,
)
render inline: renderer.render
end
end
The rendered HTML page will include content captured with from the view:
<%= render_html_with_modifications @html_page_as_string do %>
<title>
Without a block argument,
the contents of the block will be appended to the `head` element
</title>
<% end %>
<%= render_html_with_modifications @html_page_as_string do |head| %>
<% head.append do %>
<title>A single argument, yields the `head`</title>
<% end %>
<% end %>
<%= render_html_with_modifications @html_page_as_string do |head, body| %>
<% head.append do %>
<title>Both the `head` and `body` can be yielded</title>
<% end %>
<% end %>