Skip to content

Commit

Permalink
增加模板目录的配置
Browse files Browse the repository at this point in the history
增加渲染模板页面的方法
  • Loading branch information
qichengzx committed Jan 25, 2018
1 parent ab39fdc commit 86cb87d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public/
6 changes: 5 additions & 1 deletion _config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ category_dir = "categories"

# Pagination
per_page = 10
pagination_dir = "page"
pagination_dir = "page"

# Theme
theme_dir = "themes"
theme = "unnamed"
3 changes: 3 additions & 0 deletions config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type Config struct {

PerPage int `toml:"per_page"`
PaginationDir string `toml:"pagination_dir"`

ThemeDir string `toml:"theme_dir"`
Theme string `toml:"theme"`
}

func NewProvider(f string) *Config {
Expand Down
16 changes: 16 additions & 0 deletions themes/unnamed/layout/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{ define "layout" }}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>
{{ if eq .CurrentPage "index" }}
{{ .Cfg.Title }}
{{ end }}
</title>
</head>
<body>
Hello World.
</body>
</html>
{{ end }}
19 changes: 18 additions & 1 deletion xlib/x.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package xlib

import (
"bytes"
"html/template"
"math"
"path/filepath"
"x/config"
Expand Down Expand Up @@ -42,5 +44,20 @@ func (s *Site) Build() {
}

clearDir(s.Cfg.PublicDir)
makeFile([]byte("hello"), filepath.Join(s.Cfg.PublicDir, indexPage))

bt := s.renderPage()
makeFile(bt, filepath.Join(s.Cfg.PublicDir, indexPage))
}

func (s *Site) renderPage() []byte {
var doc bytes.Buffer

var t = filepath.Join(s.Cfg.ThemeDir, s.Cfg.Theme, "/layout/*.html")
tmpl, err := template.ParseGlob(t)
if err != nil {
panic(err)
}
tmpl.ExecuteTemplate(&doc, "layout", s)

return []byte(doc.String())
}

0 comments on commit 86cb87d

Please sign in to comment.