An open API service indexing awesome lists of open source software.

https://github.com/dennwc/reago

Naive experiment with Go HTML components based on Go templates.
https://github.com/dennwc/reago

Last synced: 3 months ago
JSON representation

Naive experiment with Go HTML components based on Go templates.

Awesome Lists containing this project

README

        

# Go HTML component engine

This is just a simple/naive experiment for making HTML components based on the Go templates.

Components are defined in `./component/.gohtml` and will replace `content` in the main HTML of the page.

## Simple component

For example, component can be defined like this:

```html
{{define "mybutton"}}

{{ .Content }}

{{end}}
```

And used like this:

```html


Hello!

```

The resulting HTML will be rendered as:

```html



Hello!

```

## Accessing data

Components can also access external data, which is exposed as `.DB` in the template context by the library user.

For example, when using the library, data interface can be defined as:

```go
type ExampleRecord struct {
ID int
Name string
}

type ExampleDB struct {
}

func (d *ExampleDB) Table() []ExampleRecord {
return []ExampleRecord{
{ID: 1, Name: "Foo"},
{ID: 2, Name: "Bar"},
}
}
```

Then we can define a table component that shows this data:

```html
{{define "mytable"}}


{{range .DB.Table}}
{{.ID}}{{.Name}}
{{end}}


{{end}}
```

And component can be used on the main HTML as:

```html



```

We can potentially let the user pass parameters to this component. For this we first need to add parameter to the method:

```go
func (d *ExampleDB) Table(limit int) []ExampleRecord
```

And pass this parameter from component tag attributes:

```html
{{range .DB.Table .Attrs.limit}}
```

Usage on the main html:

```html



```

## License

MIT