You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
# Background Golang template is not friendly for large projects, and Golang template team is quite slow, related: * `https://github.com/golang/go/issues/54450` Without upstream support, we can also have our solution to make HTML template functions support context. It helps a lot, the above Golang template issue `#54450` explains a lot: 1. It makes `{{Locale.Tr}}` could be used in any template, without passing unclear `(dict "root" . )` anymore. 2. More and more functions need `context`, like `avatar`, etc, we do not need to do `(dict "Context" $.Context)` anymore. 3. Many request-related functions could be shared by parent&children templates, like "user setting" / "system setting" See the test `TestScopedTemplateSetFuncMap`, one template set, two `Execute` calls with different `CtxFunc`. # The Solution Instead of waiting for upstream, this PR re-uses the escaped HTML template trees, use `AddParseTree` to add related templates/trees to a new template instance, then the new template instance can have its own FuncMap , the function calls in the template trees will always use the new template's FuncMap. `template.New` / `template.AddParseTree` / `adding-FuncMap` are all quite fast, so the performance is not affected. The details: 1. Make a new `html/template/Template` for `all` templates 2. Add template code to the `all` template 3. Freeze the `all` template, reset its exec func map, it shouldn't execute any template. 4. When a router wants to render a template by its `name` 1. Find the `name` in `all` 2. Find all its related sub templates 3. Escape all related templates (just like what the html template package does) 4. Add the escaped parse-trees of related templates into a new (scoped) `text/template/Template` 5. Add context-related func map into the new (scoped) text template 6. Execute the new (scoped) text template 7. To improve performance, the escaped templates are cached to `template sets` # FAQ ## There is a `unsafe` call, is this PR unsafe? This PR is safe. Golang has strict language definition, it's safe to do so: https://pkg.go.dev/unsafe#Pointer (1) Conversion of a *T1 to Pointer to *T2 ## What if Golang template supports such feature in the future? The public structs/interfaces/functions introduced by this PR is quite simple, the code of `HTMLRender` is not changed too much. It's very easy to switch to the official mechanism if there would be one. ## Does this PR change the template execution behavior? No, see the tests (welcome to design more tests if it's necessary) --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Jason Song <i@wolfogre.com> Co-authored-by: Giteabot <teabot@gitea.io> |
1 year ago | |
---|---|---|
.. | ||
actions | 1 year ago | |
activitypub | 1 year ago | |
analyze | 2 years ago | |
assetfs | 1 year ago | |
auth | 1 year ago | |
avatar | 1 year ago | |
base | 1 year ago | |
cache | 1 year ago | |
charset | 1 year ago | |
container | 2 years ago | |
context | 1 year ago | |
csv | 1 year ago | |
doctor | 1 year ago | |
emoji | 2 years ago | |
eventsource | 2 years ago | |
generate | 2 years ago | |
git | 1 year ago | |
gitgraph | 1 year ago | |
graceful | 1 year ago | |
hcaptcha | 1 year ago | |
highlight | 1 year ago | |
hostmatcher | 2 years ago | |
html | 2 years ago | |
httpcache | 1 year ago | |
httplib | 1 year ago | |
indexer | 1 year ago | |
issue/template | 2 years ago | |
json | 2 years ago | |
label | 1 year ago | |
lfs | 1 year ago | |
log | 1 year ago | |
markup | 1 year ago | |
mcaptcha | 2 years ago | |
metrics | 1 year ago | |
migration | 1 year ago | |
mirror | 2 years ago | |
nosql | 1 year ago | |
notification | 1 year ago | |
options | 1 year ago | |
packages | 1 year ago | |
paginator | 2 years ago | |
pprof | 2 years ago | |
private | 1 year ago | |
process | 1 year ago | |
proxy | 1 year ago | |
proxyprotocol | 2 years ago | |
public | 1 year ago | |
queue | 1 year ago | |
recaptcha | 2 years ago | |
references | 1 year ago | |
regexplru | 2 years ago | |
repository | 1 year ago | |
secret | 1 year ago | |
session | 1 year ago | |
setting | 1 year ago | |
sitemap | 2 years ago | |
ssh | 2 years ago | |
storage | 1 year ago | |
structs | 1 year ago | |
svg | 1 year ago | |
sync | 2 years ago | |
system | 2 years ago | |
templates | 1 year ago | |
test | 1 year ago | |
timeutil | 1 year ago | |
translation | 1 year ago | |
turnstile | 1 year ago | |
typesniffer | 1 year ago | |
updatechecker | 2 years ago | |
upload | 2 years ago | |
uri | 2 years ago | |
user | 2 years ago | |
util | 1 year ago | |
validation | 1 year ago | |
web | 1 year ago | |
webhook | 2 years ago |