This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial theme component implementation #31
- Loading branch information
Showing
20 changed files
with
381 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,37 @@ | ||
package page | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
"log" | ||
) | ||
|
||
// Component is anything that can be rendered on a page. A text field is a component but so is the form the text field is a part of. | ||
type Component interface { | ||
GetSection() string | ||
// render returns a html template string with the content of the field | ||
Render() string | ||
GetTemplateName() string | ||
GetTheme() string | ||
GetTemplate() *template.Template | ||
} | ||
|
||
func RenderCompnent(c Component) string { | ||
path := fmt.Sprintf("%s.%s", c.GetTheme(), c.GetTemplateName()) | ||
var foundTemplate *template.Template | ||
if c.GetTemplate() == nil { | ||
return "" | ||
} | ||
if foundTemplate = c.GetTemplate().Lookup(path); foundTemplate == nil { | ||
log.Printf("Component file not found %s\n", path) | ||
return "" | ||
} | ||
buf := &bytes.Buffer{} | ||
err := foundTemplate.Execute(buf, c) | ||
if err != nil { | ||
log.Printf("Component file error executing template %s\n", path) | ||
return "" | ||
} | ||
return buf.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package component | ||
|
||
import ( | ||
"github.com/markustenghamn/beubo/pkg/structs/page" | ||
"html/template" | ||
) | ||
|
||
type Button struct { | ||
Theme string | ||
Template string | ||
Class string | ||
Content string | ||
T *template.Template | ||
} | ||
|
||
func (b Button) GetSection() string { | ||
return "" | ||
} | ||
|
||
func (b Button) GetTemplateName() string { | ||
return returnTIfNotEmpty(b.Template, "component.button") | ||
} | ||
|
||
func (b Button) GetTheme() string { | ||
return returnTIfNotEmpty(b.Theme, "default") | ||
} | ||
|
||
func (b Button) GetTemplate() *template.Template { | ||
return b.T | ||
} | ||
|
||
func (b Button) Render() string { | ||
return page.RenderCompnent(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package component | ||
|
||
import ( | ||
"github.com/markustenghamn/beubo/pkg/structs/page" | ||
"html/template" | ||
) | ||
|
||
type CheckBoxField struct { | ||
Theme string | ||
Template string | ||
Content string | ||
Class string | ||
Name string | ||
Identifier string | ||
Value string | ||
Checked bool | ||
T *template.Template | ||
} | ||
|
||
func (cb CheckBoxField) GetSection() string { | ||
return "" | ||
} | ||
|
||
func (cb CheckBoxField) GetTemplateName() string { | ||
return returnTIfNotEmpty(cb.Template, "component.checkboxfield") | ||
} | ||
|
||
func (cb CheckBoxField) GetTheme() string { | ||
return returnTIfNotEmpty(cb.Template, "default") | ||
} | ||
|
||
func (cb CheckBoxField) GetTemplate() *template.Template { | ||
return cb.T | ||
} | ||
|
||
func (cb CheckBoxField) Render() string { | ||
return page.RenderCompnent(cb) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package component | ||
|
||
func returnTIfNotEmpty(t string, d string) string { | ||
if t != "" { | ||
return t | ||
} | ||
return d | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package component | ||
|
||
import ( | ||
"github.com/markustenghamn/beubo/pkg/structs/page" | ||
"html/template" | ||
) | ||
|
||
type HiddenField struct { | ||
Theme string | ||
Template string | ||
Identifier string | ||
Name string | ||
Value string | ||
T *template.Template | ||
} | ||
|
||
func (hf HiddenField) GetSection() string { | ||
return "" | ||
} | ||
|
||
func (hf HiddenField) GetTemplateName() string { | ||
return returnTIfNotEmpty(hf.Template, "component.hiddenfield") | ||
} | ||
|
||
func (hf HiddenField) GetTheme() string { | ||
return returnTIfNotEmpty(hf.Template, "default") | ||
} | ||
|
||
func (hf HiddenField) GetTemplate() *template.Template { | ||
return hf.T | ||
} | ||
|
||
func (hf HiddenField) Render() string { | ||
return page.RenderCompnent(hf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package component | ||
|
||
import ( | ||
"github.com/markustenghamn/beubo/pkg/structs/page" | ||
"html/template" | ||
) | ||
|
||
type RadioField struct { | ||
Theme string | ||
Template string | ||
Class string | ||
Identifier string | ||
Name string | ||
Value string | ||
Content string | ||
Checked bool | ||
T *template.Template | ||
} | ||
|
||
func (rf RadioField) GetSection() string { | ||
return "" | ||
} | ||
|
||
func (rf RadioField) GetTemplateName() string { | ||
return returnTIfNotEmpty(rf.Template, "component.radiofield") | ||
} | ||
|
||
func (rf RadioField) GetTheme() string { | ||
return returnTIfNotEmpty(rf.Template, "default") | ||
} | ||
|
||
func (rf RadioField) GetTemplate() *template.Template { | ||
return rf.T | ||
} | ||
|
||
func (rf RadioField) Render() string { | ||
return page.RenderCompnent(rf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package component | ||
|
||
import ( | ||
"github.com/markustenghamn/beubo/pkg/structs/page" | ||
"html/template" | ||
) | ||
|
||
type SelectField struct { | ||
Theme string | ||
Template string | ||
Identifier string | ||
Class string | ||
Name string | ||
Options []SelectFieldOption | ||
T *template.Template | ||
} | ||
|
||
type SelectFieldOption struct { | ||
Value string | ||
Content string | ||
} | ||
|
||
func (sf SelectField) GetSection() string { | ||
return "" | ||
} | ||
|
||
func (sf SelectField) GetTemplateName() string { | ||
return returnTIfNotEmpty(sf.Template, "component.selectfield") | ||
} | ||
|
||
func (sf SelectField) GetTheme() string { | ||
return returnTIfNotEmpty(sf.Template, "default") | ||
} | ||
|
||
func (sf SelectField) GetTemplate() *template.Template { | ||
return sf.T | ||
} | ||
|
||
func (sf SelectField) Render() string { | ||
return page.RenderCompnent(sf) | ||
} |
Oops, something went wrong.