Skip to content

Commit

Permalink
fix(django): concurrent map writes
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Oct 5, 2023
1 parent 421710b commit f94caa9
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions django/django.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,40 @@ func init() {
})
}

// runtimeInjectGlobals injects globals to pongo2 template runtime, because globals may be changed at runtime,
// runtimeGlobals return runtime globals to pongo2 template runtime, because globals may be changed at runtime,
// such as language, time, tracing.
// This function should be called getTemplate().
func runtimeInjectGlobals() {
// This function should be called and injected before rendering a template.
func runtimeGlobals() Context {
runtimeCtx := Context{
"app": Context{
"year": carbon.Now().Format("Y"),
},
"today": carbon.Now().Format("Y 年 n 月 j 日"),
}
CopyPongoContextRecursive(instance.Globals, runtimeCtx)
return runtimeCtx
}

// GetTemplate returns a template from cache.
// Note that this method do not inject and return runtime globals.
func GetTemplate(filename string) (*pongo2.Template, error) {
runtimeInjectGlobals()
return instance.FromCache(filename + ".django")
}

// GetTemplateR returns a template from cache and runtime ctx.
// Note that you should inject runtime globals before rendering a template.
func GetTemplateR(filename string) (*pongo2.Template, Context, error) {
tpl, err := GetTemplate(filename)
return tpl, runtimeGlobals(), err
}

// RenderTemplate renders a template with context.
// This function is a shortcut of GetTemplate().Execute().
func RenderTemplate(name string, ctx Context) (string, error) {
tpl, err := GetTemplate(name)
if err != nil {
return "", err
}
return tpl.Execute(ctx)
mergedCtx := runtimeGlobals()
CopyPongoContextRecursive(mergedCtx, ctx)
return tpl.Execute(mergedCtx)
}

0 comments on commit f94caa9

Please sign in to comment.