Add partial render support using "template#block" syntax (fixes gin-gonic/gin#3745)
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()
:warning: Please install the to ensure uploads and comments are reliably processed by Codecov.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 69.36%. Comparing base (32b2334) to head (296cfd9).
:warning: Report is 28 commits behind head on master.
:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.
:exclamation: 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%) |
:arrow_down: |
| ubuntu-latest | 69.36% <100.00%> (-30.64%) |
:arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
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.