diff --git a/database.go b/database.go index 160dc65..136030d 100644 --- a/database.go +++ b/database.go @@ -145,8 +145,10 @@ func databaseSeed() { } // Create a page - - content := `
This is a default page
` + content := `Welcome to Beubo! Beubo is a free, simple, and minimal CMS with unlimited extensibility using plugins. This is the default page and can be changed in the admin area for this site.
` + content += `Beubo is open source and the project can be found on Github. If you find any problems or have an idea on how Beubo can be improved, please feel free to open an issue here.
` + content += `Feel free to open a pull request if you would like to contribute your own changes.
` + content += `For more information on how to use, customize and extend Beubo please see the wiki
` page := structs.Page{ Model: gorm.Model{}, diff --git a/pkg/routes/base.go b/pkg/routes/base.go index 5794bcb..0f719c3 100644 --- a/pkg/routes/base.go +++ b/pkg/routes/base.go @@ -22,21 +22,20 @@ func (br *BeuboRouter) NotFoundHandler(w http.ResponseWriter, r *http.Request) { // PageHandler checks if a page exists for the give slug func (br *BeuboRouter) PageHandler(w http.ResponseWriter, r *http.Request) { - // TODO check if url matches a slug site := structs.FetchSiteByHost(br.DB, r.Host) if site.ID != 0 { page := structs.FetchPageBySiteIDAndSlug(br.DB, int(site.ID), r.URL.Path) - // TODO add ability to select a page template - // TODO how does the page know what content to render? if page.ID != 0 { pageData := structs.PageData{ Template: "page", Title: page.Title, + // TODO Components should be defined on the page edit page and defined in the db Components: []beuboPage.Component{component.Text{ Content: template.HTML(page.Content), Theme: "", Template: "", Class: "", + Section: "main", T: br.Renderer.T, }}, } diff --git a/pkg/structs/page.go b/pkg/structs/page.go index cdc2c7c..fe7bb03 100644 --- a/pkg/structs/page.go +++ b/pkg/structs/page.go @@ -5,7 +5,6 @@ import ( "github.com/jinzhu/gorm" "github.com/markustenghamn/beubo/pkg/structs/page" "html/template" - "log" ) // Page represents the content of a page, I wanted to go with the concept of having everything be a post even if it's a page, contact form or product @@ -131,9 +130,10 @@ func DeletePage(db *gorm.DB, id int) Page { func (pd PageData) Content(section string) template.HTML { result := "" - log.Println("Content called:", section) for _, component := range pd.Components { - result += component.Render() + if component.GetSection() == section { + result += component.Render() + } } return template.HTML(result) } @@ -142,7 +142,6 @@ func (pd PageData) Menu(section string) template.HTML { result := "" for _, menu := range pd.Menus { if menu.GetIdentifier() == section { - log.Println("Menu called:", section) return template.HTML(menu.Render()) } } diff --git a/pkg/structs/page/component.go b/pkg/structs/page/component.go index 98b6870..a4bd886 100644 --- a/pkg/structs/page/component.go +++ b/pkg/structs/page/component.go @@ -1,6 +1,7 @@ package page type Component interface { + GetSection() string // render returns a html template string with the content of the field Render() string } diff --git a/pkg/structs/page/component/form.go b/pkg/structs/page/component/form.go index ac5d5a8..8f7792a 100644 --- a/pkg/structs/page/component/form.go +++ b/pkg/structs/page/component/form.go @@ -1,7 +1,47 @@ package component -import "github.com/markustenghamn/beubo/pkg/structs/page" +import ( + "bytes" + "fmt" + "github.com/markustenghamn/beubo/pkg/structs/page" + "html/template" + "log" +) type Form struct { - Fields []page.Field + Section string + Fields []page.Field + Theme string + Template string + T *template.Template + Method string + Action string +} + +func (f Form) GetSection() string { + return f.Section +} + +func (f Form) Render() string { + tmpl := "component.form" + if f.Template != "" { + tmpl = f.Template + } + theme := "default" + if f.Theme != "" { + theme = f.Theme + } + path := fmt.Sprintf("%s.%s", theme, tmpl) + var foundTemplate *template.Template + if foundTemplate = f.T.Lookup(path); foundTemplate == nil { + log.Printf("Component file not found %s\n", path) + return "" + } + buf := &bytes.Buffer{} + err := foundTemplate.Execute(buf, f) + if err != nil { + log.Printf("Component file error executing template %s\n", path) + return "" + } + return buf.String() } diff --git a/pkg/structs/page/component/table.go b/pkg/structs/page/component/table.go index dc51a8b..aa0eee9 100644 --- a/pkg/structs/page/component/table.go +++ b/pkg/structs/page/component/table.go @@ -1,10 +1,26 @@ package component -import "github.com/markustenghamn/beubo/pkg/structs/page" +import ( + "bytes" + "fmt" + "github.com/markustenghamn/beubo/pkg/structs/page" + "html/template" + "log" +) type Table struct { - // Row defines - Row Row + Section string + Header []Column + Rows []Row + Theme string + Template string + PageNumber int // Current page + PageDisplayCount int // How many rows per page + T *template.Template +} + +func (t Table) GetSection() string { + return t.Section } type Row struct { @@ -14,4 +30,33 @@ type Row struct { type Column struct { Name string Field page.Field + Value string +} + +func (t Table) Render() string { + tmpl := "component.table" + if t.Template != "" { + tmpl = t.Template + } + theme := "default" + if t.Theme != "" { + theme = t.Theme + } + path := fmt.Sprintf("%s.%s", theme, tmpl) + var foundTemplate *template.Template + if foundTemplate = t.T.Lookup(path); foundTemplate == nil { + log.Printf("Component file not found %s\n", path) + return "" + } + buf := &bytes.Buffer{} + err := foundTemplate.Execute(buf, t) + if err != nil { + log.Printf("Component file error executing template %s\n", path) + return "" + } + return buf.String() +} + +func (c Column) RenderField(value string, field page.Field) { + } diff --git a/pkg/structs/page/component/text.go b/pkg/structs/page/component/text.go index e1ddf6d..61e0e72 100644 --- a/pkg/structs/page/component/text.go +++ b/pkg/structs/page/component/text.go @@ -8,6 +8,7 @@ import ( ) type Text struct { + Section string Content template.HTML Theme string Template string @@ -15,6 +16,10 @@ type Text struct { T *template.Template } +func (t Text) GetSection() string { + return t.Section +} + func (t Text) Render() string { tmpl := "component.text" if t.Template != "" { diff --git a/themes/default b/themes/default index 00a562d..324d97e 160000 --- a/themes/default +++ b/themes/default @@ -1 +1 @@ -Subproject commit 00a562d18b03c3c2d57e8bbd00e473c3ced8982f +Subproject commit 324d97e12f18b9066b240f805f88fc13969c61d5