Skip to content
This repository has been archived by the owner on Apr 2, 2023. It is now read-only.

Commit

Permalink
Added form and table base templates
Browse files Browse the repository at this point in the history
  • Loading branch information
uberswe committed Oct 25, 2020
1 parent b691268 commit e02f638
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 15 deletions.
6 changes: 4 additions & 2 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ func databaseSeed() {
}

// Create a page

content := `<p>This is a default page</p>`
content := `<p>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.</p>`
content += `<p>Beubo is open source and the project can be found on <a href="https://github.com/markustenghamn/beubo">Github</a>. If you find any problems or have an idea on how Beubo can be improved, please feel free to <a href="https://github.com/markustenghamn/beubo/issues">open an issue here</a>.</p>`
content += `<p>Feel free to <a href="https://github.com/markustenghamn/beubo/pulls">open a pull request</a> if you would like to contribute your own changes.</p>`
content += `<p>For more information on how to use, customize and extend Beubo please see the <a href="https://github.com/markustenghamn/beubo/wiki">wiki</a></p>`

page := structs.Page{
Model: gorm.Model{},
Expand Down
5 changes: 2 additions & 3 deletions pkg/routes/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}},
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/structs/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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())
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/structs/page/component.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 42 additions & 2 deletions pkg/structs/page/component/form.go
Original file line number Diff line number Diff line change
@@ -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()
}
51 changes: 48 additions & 3 deletions pkg/structs/page/component/table.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {

}
5 changes: 5 additions & 0 deletions pkg/structs/page/component/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
)

type Text struct {
Section string
Content template.HTML
Theme string
Template string
Class string
T *template.Template
}

func (t Text) GetSection() string {
return t.Section
}

func (t Text) Render() string {
tmpl := "component.text"
if t.Template != "" {
Expand Down
2 changes: 1 addition & 1 deletion themes/default

0 comments on commit e02f638

Please sign in to comment.