renderbee

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

README

renderBee

Package for creating HTML renderable components in Go. Inspired by renderSnake and seaside.

Basics

  • Encapsulate a Go template and its data into a component, called a fragment
  • Use a Map or Composite to encapsulate a Go template and multiple fragments
  • Render fragments and containers on a HtmlCanvas (io.Writer)

Read the full documentation and try the examples or explore it on [sourcegraph] (https://sourcegraph.com/github.com/emicklei/renderbee).

Build Status

(c) 2013, http://ernestmicklei.com. Apache v2 License

Documentation

Overview

Package renderbee provides a micro-framework for creating HTML components based on the standard html/templates.

Summary

	Encapsulate a Go template and its data into a Fragment
 	Use a FragmentMap or CompositeFragment to encapsulate a Go template and multiple components
 	Render fragments on a HtmlCanvas (io.Writer)
	Template may refer to other Fragments.

Example

<html>
 <head>
  {{.Render "Head"}
 </head>
 <body>
  <div id="content">
   {{.Render "Header"}}
   {{.Render "BodyContent"}}
   {{.Render "Footer"}}
  </div>
 </body>
</html>

Use this template with fragment components to render a HTML page.

page := renderBee.NewFragmentMap(Page_Template)
page.Add("Head", headFragment)
page.Add("Header", headerFragment)
page.Add("BodyContent", boydFragmentMap)
page.Add("Footer", footerFragment)

canvas := NewHtmlCanvas(resp.ResponseWriter)
canvas.Render(page)

(c) 2013, http://ernestmicklei.com. Apache V2 License

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTML

func HTML(r Renderable) template.HTML

HTML returns a template.HTML from rendering a component r.

Types

type CompositeFragment

type CompositeFragment struct {
	Fragments []*Fragment
	Template  *template.Template
}

CompositeFragment encapsulates a slice of Fragment and a template and can be rendered on a HtmlCanvas.

Example
citiesTemplate := template.Must(template.New("Page").Parse(`
<div class="cities">
{{range .}}{{.Render}}
{{end}}</div>`))
cityTemplate := template.Must(template.New("City").Parse(
	`<h1>Welcome to {{.}}</h1>`))
fragment := NewCompositeFragment(citiesTemplate)
fragment.Add(NewFragment("New York", cityTemplate))
fragment.Add(NewFragment("Amsterdam", cityTemplate))
canvas := NewHtmlCanvas(os.Stdout)
canvas.Render(fragment)
Output:

<div class="cities">
<h1>Welcome to New York</h1>
<h1>Welcome to Amsterdam</h1>
</div>

func NewCompositeFragment

func NewCompositeFragment(t *template.Template) *CompositeFragment

NewCompositeFragment creates an empty CompositeFragment with a template.

func (*CompositeFragment) Add

func (c *CompositeFragment) Add(frags ...*Fragment)

Add a Fragement to the slice of Fragments.

func (CompositeFragment) Render

func (c CompositeFragment) Render() template.HTML

Return some HTML(string) as a result of the rendering the receiver on a temporary HtmlCanvas.

func (CompositeFragment) RenderOn

func (c CompositeFragment) RenderOn(hc *HtmlCanvas)

RenderOn takes the template and collection of Fragments to render on a HtmlCanvas. part of the Renderable interface

type Fragment

type Fragment struct {
	Template *template.Template
	// contains filtered or unexported fields
}

Fragment encapsulates data and a template and can be rendered on a HtmlCanvas.

Example
person := struct {
	Name  string
	Email string
}{
	"John",
	"john.doe@here.com",
}
personTemplate := template.Must(template.New("Person_Template").Parse(`
<div class="person">
	<a href="{{.Email}}">{{.Name}}</a>
</div>`))
fragment := NewFragment(person, personTemplate)
canvas := NewHtmlCanvas(os.Stdout)
canvas.Render(fragment)
Output:

<div class="person">
	<a href="john.doe@here.com">John</a>
</div>

func NewFragment

func NewFragment(data interface{}, t *template.Template) *Fragment

NewFragment creates a Fragment that can be rendered on a HtmlCanvas.

func (Fragment) Render

func (c Fragment) Render() template.HTML

Return some HTML(string) as a result of the rendering the receiver on a temporary HtmlCanvas.

func (Fragment) RenderOn

func (c Fragment) RenderOn(hc *HtmlCanvas)

RenderOn takes the template and data of the fragment to render on a HtmlCanvas. part of the Renderable interface

type FragmentMap

type FragmentMap struct {
	Components map[string]Renderable
	Template   *template.Template
}

FragmentMap encapsulates a Template and a set of named Renderable components. A FragmentMap can be rendered on a HtmlCanvas.

Example
pageTemplate := template.Must(template.New("Page").Parse(`
<html>
<body>
{{.Render "Header"}}
{{.Render "Footer"}}
</body>
</html>`))
headerTemplate := template.Must(template.New("Header").Parse(
	`<h1>Hello {{.}}</h1>`))
footerTemplate := template.Must(template.New("Footer").Parse(
	`<h1>&copy; 2013. Created by {{.}}</h1>`))
page := NewFragmentMap(pageTemplate)
page.Put("Header", NewFragment("John", headerTemplate))
page.Put("Footer", NewFragment("Jane", footerTemplate))
canvas := NewHtmlCanvas(os.Stdout)
canvas.Render(page)
Output:

<html>
<body>
<h1>Hello John</h1>
<h1>&copy; 2013. Created by Jane</h1>
</body>
</html>

func NewFragmentMap

func NewFragmentMap(template *template.Template) *FragmentMap

NewFragmentMap creates and returns a Renderable FragmentMap with a html Template.

func (*FragmentMap) Put

func (c *FragmentMap) Put(name string, r Renderable)

Put adds a Renderable component with its associated name Refer to this component in a template by writing: {{.Render "<name>" }}

func (FragmentMap) RenderOn

func (c FragmentMap) RenderOn(hc *HtmlCanvas)

RenderOn executes the template of the FragmentMap using the collection of named Renderable components. Required method from the Renderable interface.

type HtmlCanvas

type HtmlCanvas struct {
	io.Writer
}

HtmlCanvas is a io.Writer for writing Renderable components.

func NewHtmlCanvas

func NewHtmlCanvas(writer io.Writer) *HtmlCanvas

Return a new HtmlCanvas for writing Html on an io.Writer

func (*HtmlCanvas) Render

func (hc *HtmlCanvas) Render(r Renderable) *HtmlCanvas

Render dispatches to RenderOn on the Renderable component and returns itself.

type Javascript

type Javascript struct {
	Href string
}

Javascript is a simple Renderable components that encapsulates the HTML to reference a Javascript file by its hyperlink reference.

Example
canvas := NewHtmlCanvas(os.Stdout)
canvas.Render(Javascript{"http://mysite.com/static/main.js"})
Output:

<script type="text/javascript" src="http://mysite.com/static/main.js"></script>

func (Javascript) RenderOn

func (j Javascript) RenderOn(hc *HtmlCanvas)

RenderOn writes the HTML snippet for an external Javascript reference on a HtmlCanvas.

type Renderable

type Renderable interface {
	RenderOn(hc *HtmlCanvas)
}

Renderables can render themselves on a HtmlCanvas.

type Stylesheet

type Stylesheet struct {
	Href string
}

Stylesheet is a simple Renderable components that encapsulates the HTML to reference a Stylesheet file by its hyperlink reference.

Example
canvas := NewHtmlCanvas(os.Stdout)
canvas.Render(Stylesheet{"http://mysite.com/static/main.css"})
Output:

<link type="text/css" rel="stylesheet" href="http://mysite.com/static/main.css"/>

func (Stylesheet) RenderOn

func (s Stylesheet) RenderOn(hc *HtmlCanvas)

RenderOn writes the HTML snippet for an external Stylesheet reference on a HtmlCanvas.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL