feat: add render_partial for HTMX fragment responses#355
Open
papilip wants to merge 1 commit intomartenframework:mainfrom
Open
feat: add render_partial for HTMX fragment responses#355papilip wants to merge 1 commit intomartenframework:mainfrom
papilip wants to merge 1 commit intomartenframework:mainfrom
Conversation
Add a render_partial method to Marten::Handlers::Base that renders a template without the layout, context producers, or before_render callbacks. This is useful for returning HTML fragments in HTMX responses.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a
render_partialmethod toMarten::Handlers::Basethat renders a template without the layout, context producers, orbefore_rendercallbacks.Motivation
HTMX is increasingly popular for building interactive web applications. A common pattern is returning HTML fragments from handlers to replace parts of the page:
Currently, developers must work around this by manually loading and rendering templates:
This is verbose, error-prone, and bypasses the handler's response infrastructure.
Changes
src/marten/handlers/base.cr: Newrender_partialmethod that creates a cleanContext(no producers), renders the template, and returns anHTTP::Responsespec/marten/handlers/base_spec.cr: 3 tests (hash context, named tuple context, custom status)Design decisions
before_rendercallbacks: These are designed for full-page rendersrequestin context: Keeps the partial isolated. If needed, the caller can add it explicitlyHash,NamedTuple,Context, andnilRelated
render_partialaddresses the server-side use caseTest plan