Inertia::Renderer
Renderer interface for Inertia.js server-side HTML template generation.
Use this module to provide the initial HTML shell that will be sent to the browser for non-Inertia requests (first page loads).
The renderer is responsible for generating the complete HTML document, including the initial payload.
render will be called by the usage of Inertia through your application,
passing the inertia instance, the current HTTP context and locals,
necessary to retrieve the page data.
See https://inertiajs.com/server-side-setup#root-template
Example:
class MyRenderer
include Inertia::Renderer
def render(inertia : Inertia, context : HTTP::Server::Context, locals)
# Extract the page data (already JSON-encoded)
page_data = HTML.escape(locals[:page])
# Set content type and write HTML response
context.response.content_type = "text/html"
context.response << <<-HTML
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<script src="/assets/app.js"></script>
</head>
<body>
<div id="app" data-page='#{page_data}'></div>
</body>
</html>
HTML
end
end
renderer = MyRenderer.new
inertia = Inertia.new(version: "1.0", renderer: renderer)
Important notes:
locals[:page]already contains the JSON-encoded page data (props).- Always make sure to include the
data-pagein the generated HTML. - Last but not least, ensure all the parameters are properly escaped to prevent XSS attacks.