Skip to content

Conversation

@YamiOdymel
Copy link
Contributor

@YamiOdymel YamiOdymel commented Aug 29, 2025

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 multitemplate relies on Gin’s c.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#block syntax 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#world and passes world to Gin’s render.HTML.Name, basically reusing what we already have.

c.HTML(http.StatusOK, "hello#world", gin.H{
	"fruit": "apple",
})

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()

Copilot AI review requested due to automatic review settings August 29, 2025 06:46
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 parseTemplateName function to parse "template#block" syntax
  • Updated both Render.Instance and DynamicRender.Instance methods 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 {
Copy link

Copilot AI Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 '#'.

Suggested change
if idx := strings.Index(name, "#"); idx > 0 {
if idx := strings.Index(name, "#"); idx >= 0 {

Copilot uses AI. Check for mistakes.
@codecov-commenter
Copy link

codecov-commenter commented Aug 29, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.36%. Comparing base (32b2334) to head (296cfd9).
⚠️ Report is 28 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

❗ There is a different number of reports uploaded between BASE (32b2334) and HEAD (296cfd9). Click for more details.

HEAD has 12 uploads less than BASE
Flag BASE (32b2334) HEAD (296cfd9)
ubuntu-latest 7 3
go-1.14 1 0
go-1.15 1 0
go-1.16 1 0
go- 1 0
go-1.17 1 0
go-1.18 1 0
go-1.19 1 0
go-1.20 1 0
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     
Flag Coverage Δ
go- ?
go-1.14 ?
go-1.15 ?
go-1.16 ?
go-1.17 ?
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.23 69.36% <100.00%> (?)
go-1.24 69.36% <100.00%> (?)
go-1.25 69.36% <100.00%> (?)
macos-latest 69.36% <100.00%> (-30.64%) ⬇️
ubuntu-latest 69.36% <100.00%> (-30.64%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AxelRHD
Copy link

AxelRHD commented Sep 18, 2025

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants