Add partial render support using "template#block" syntax (fixes gin-gonic/gin#3745)#77
Add partial render support using "template#block" syntax (fixes gin-gonic/gin#3745)#77YamiOdymel wants to merge 1 commit intogin-contrib:masterfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds partial render support to the multitemplate package using the "template#block" syntax, enabling applications to render specific template blocks instead of complete templates. This functionality is particularly useful for HTMX applications that need to return partial HTML fragments for dynamic page updates.
- Added
parseTemplateNamefunction to parse "template#block" syntax - Updated both
Render.InstanceandDynamicRender.Instancemethods to support partial rendering - Added comprehensive test coverage and documentation examples
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| multitemplate.go | Core implementation of partial rendering logic with template name parsing |
| dynamic.go | Extended dynamic renderer to support partial rendering functionality |
| multitemplate_test.go | Added test case to verify partial rendering works correctly |
| tests/partial/base.html | Test template file demonstrating block syntax usage |
| example/partial/ | Complete example showing partial rendering in practice |
| README.md | Documentation for the new partial rendering feature |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| // parseTemplateName parses a template name that may contain a partial reference | ||
| // in the format "template#partial" and returns the template name and partial name | ||
| func parseTemplateName(name string) (templateName, partialName string) { | ||
| if idx := strings.Index(name, "#"); idx > 0 { |
There was a problem hiding this comment.
The condition idx > 0 excludes template names that start with '#'. This should be idx >= 0 to handle cases where the partial name starts immediately with '#', or idx > 0 if you specifically want to require a template name before the '#'.
| if idx := strings.Index(name, "#"); idx > 0 { | |
| if idx := strings.Index(name, "#"); idx >= 0 { |
|
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## master #77 +/- ##
============================================
- Coverage 100.00% 69.36% -30.64%
============================================
Files 2 2
Lines 112 235 +123
============================================
+ Hits 112 163 +51
- Misses 0 71 +71
- Partials 0 1 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Is this something which is planned to be implemented in the near future? I will start an new project soon and it would save me a lot of extra code and files for the html-templating. Thanks. |
Description
As HTMX becomes more popular, a page should be able to decide whether to return a full response or a partial one that updates only part of the view.
Current Problem
The current
multitemplaterelies on Gin’sc.HTML(code int, name string, obj any)signature, which only allows rendering a whole "template set", not a specific template/block within "the set".In htmx ~ Template Fragments, a
template#blocksyntax is mentioned. This seems to be a fairly standard convention in template systems.Implementation
The change is very simple: it just splits template name from
hello#worldand passesworldto Gin’s render.HTML.Name, basically reusing what we already have.This makes it possible to render partials which is defined as
{{ block "world" . }}and{{ define "world" }}within a template set.related: [gin-gonic/gin] add partial rendering to context.HTML() or add context.HTMLBlock()