Author: Doug Hellmann <doug@doughellmann.com>
templates: use consistent base template context Always construct the template context with site-wide settings and add any local data that is specific to the template being rendered. Signed-off-by: Doug Hellmann <doug@doughellmann.com>
include/header.html | 4 +- include/index.html | 4 -- include/repo-index.html | 2 pkg/smithy/smithy.go | 59 ++++++++++++++++++++++++++----------------
diff --git a/include/header.html b/include/header.html index 69a08fa46e93be2ea7d068c440f510ad0fdbe67c..70757c3c2382fcda4801938d979fbdb6d8825994 100644 --- a/include/header.html +++ b/include/header.html @@ -4,14 +4,14 @@ <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> - <title>Smithy</title> + <title>{{ .Site.Title }}</title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="{{ css }}" /> </head> <body> <nav class="navbar navbar-expand navbar-light bg-light fixed-top"> - <a class="navbar-brand" href="/">Smithy</a> + <a class="navbar-brand" href="/">{{ .Site.Title }}</a> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li class="nav-item"> diff --git a/include/index.html b/include/index.html index 2c73a8cb3f27815edd1b5bb9591c84a39036aa88..2de004625a19fa12343f670713d477f8541c36ae 100644 --- a/include/index.html +++ b/include/index.html @@ -1,8 +1,6 @@ {{ template "header" . }} -<h3>{{ .Title }}</h3> - -<p>{{ .Description }}</p> +<p>{{ .Site.Description }}</p> <h3>Projects</h3> diff --git a/include/repo-index.html b/include/repo-index.html index 253367bd9ac12722f4d51ac50dba5e6bee9eb459..089cd9169dd91d5ede3a7cd01e5054ee8b9921d1 100644 --- a/include/repo-index.html +++ b/include/repo-index.html @@ -29,7 +29,7 @@ {{ .Readme }} <hr> <pre> -$ git clone https://{{ .Host }}/git/{{ $repo }} +$ git clone https://{{ .Site.Host }}/git/{{ $repo }} </pre> </div> </div> diff --git a/pkg/smithy/smithy.go b/pkg/smithy/smithy.go index 8936cb3c338e24668a3f7d27def3fdfa63403a54..7356c4de1d3013d630e123f0c00a9402fb9d9714 100644 --- a/pkg/smithy/smithy.go +++ b/pkg/smithy/smithy.go @@ -215,22 +215,37 @@ return buf.String(), nil } func Http404(ctx *gin.Context) { - ctx.HTML(http.StatusNotFound, "404.html", gin.H{}) + smithyConfig := ctx.MustGet("config").(SmithyConfig) + ctx.HTML(http.StatusNotFound, "404.html", makeTemplateContext(smithyConfig, gin.H{})) } func Http500(ctx *gin.Context) { - ctx.HTML(http.StatusInternalServerError, "500.html", gin.H{}) + smithyConfig := ctx.MustGet("config").(SmithyConfig) + ctx.HTML(http.StatusInternalServerError, "500.html", + makeTemplateContext(smithyConfig, gin.H{})) +} + +func makeTemplateContext(config SmithyConfig, extra gin.H) gin.H { + results := gin.H{ + "Site": gin.H{ + "Title": config.Title, + "Description": config.Description, + "Host": config.Host, + }, + } + for k, v := range extra { + results[k] = v + } + return results } func IndexView(ctx *gin.Context, urlParts []string) { smithyConfig := ctx.MustGet("config").(SmithyConfig) repos := smithyConfig.GetRepositories() - ctx.HTML(http.StatusOK, "index.html", gin.H{ - "Repos": repos, - "Title": smithyConfig.Title, - "Description": smithyConfig.Description, - }) + ctx.HTML(http.StatusOK, "index.html", makeTemplateContext(smithyConfig, gin.H{ + "Repos": repos, + })) } func findMainBranch(ctx *gin.Context, repo *git.Repository) (string, *plumbing.Hash, error) { @@ -293,15 +308,13 @@ } } } - ctx.HTML(http.StatusOK, "repo-index.html", gin.H{ + ctx.HTML(http.StatusOK, "repo-index.html", makeTemplateContext(smithyConfig, gin.H{ "Name": repoName, "Branches": bs, "Tags": ts, "Readme": template.HTML(formattedReadme), "Repo": repo, - - "Host": smithyConfig.Host, - }) + })) } func RepoGitView(ctx *gin.Context, urlParts []string) { @@ -345,11 +358,11 @@ if err != nil { ts = []*plumbing.Reference{} } - ctx.HTML(http.StatusOK, "refs.html", gin.H{ + ctx.HTML(http.StatusOK, "refs.html", makeTemplateContext(smithyConfig, gin.H{ "Name": repoName, "Branches": bs, "Tags": ts, - }) + })) } func TreeView(ctx *gin.Context, urlParts []string) { @@ -421,12 +434,12 @@ // We're looking at the root of the project. Show a list of files. if treePath == "" { entries := ConvertTreeEntries(tree.Entries) - ctx.HTML(http.StatusOK, "tree.html", gin.H{ + ctx.HTML(http.StatusOK, "tree.html", makeTemplateContext(smithyConfig, gin.H{ "RepoName": repoName, "RefName": refNameString, "Files": entries, "Path": treePath, - }) + })) return } @@ -445,14 +458,14 @@ Http404(ctx) return } entries := ConvertTreeEntries(subTree.Entries) - ctx.HTML(http.StatusOK, "tree.html", gin.H{ + ctx.HTML(http.StatusOK, "tree.html", makeTemplateContext(smithyConfig, gin.H{ "RepoName": repoName, "ParentPath": parentPath, "RefName": refNameString, "SubTree": out.Name, "Path": treePath, "Files": entries, - }) + })) return } @@ -471,7 +484,7 @@ if err != nil { Http404(ctx) return } - ctx.HTML(http.StatusOK, "blob.html", gin.H{ + ctx.HTML(http.StatusOK, "blob.html", makeTemplateContext(smithyConfig, gin.H{ "RepoName": repoName, "RefName": refNameString, "File": out, @@ -479,7 +492,7 @@ "ParentPath": parentPath, "Path": treePath, "Contents": contents, "ContentsHighlighted": template.HTML(syntaxHighlighted), - }) + })) } func LogView(ctx *gin.Context, urlParts []string) { @@ -539,11 +552,11 @@ } commits = append(commits, c) } - ctx.HTML(http.StatusOK, "log.html", gin.H{ + ctx.HTML(http.StatusOK, "log.html", makeTemplateContext(smithyConfig, gin.H{ "Name": repoName, "RefName": refNameString, "Commits": commits, - }) + })) } func LogViewDefault(ctx *gin.Context, urlParts []string) { @@ -649,11 +662,11 @@ Http404(ctx) return } - ctx.HTML(http.StatusOK, "commit.html", gin.H{ + ctx.HTML(http.StatusOK, "commit.html", makeTemplateContext(smithyConfig, gin.H{ "Name": repoName, "Commit": commitObj, "Changes": template.HTML(formattedChanges), - }) + })) } func ListBranches(r *git.Repository) ([]*plumbing.Reference, error) {