Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
petaki committed Feb 26, 2021
2 parents dccd243 + 98db0b2 commit c2a3b4f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
go-version: 1.16

- name: Install dependencies
run: |
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ version := "" // Asset version
inertiaManager := inertia.New(url, rootTemplate, version)
```

Or create with `embed.FS` for root template:

```go
import "embed"

//go:embed template
var templateFS embed.FS

// ...

inertiaManager := inertia.NewWithFS(url, rootTemplate, version, templateFS)
```

### 2. Register the middleware

```go
Expand Down Expand Up @@ -119,6 +132,16 @@ r = r.WithContext(ctx)
</html>
```

## Example Apps

### Satellite

https://github.com/petaki/satellite

### Homettp

https://github.com/homettp/homettp

## Reporting Issues

If you are facing a problem with this package or found any bug, please open an issue on [GitHub](https://github.com/petaki/inertia-go/issues).
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/petaki/inertia-go

go 1.15
go 1.16
22 changes: 21 additions & 1 deletion inertia.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"html/template"
"io/fs"
"net/http"
"path/filepath"
"strings"
Expand All @@ -16,6 +17,7 @@ type Inertia struct {
version string
sharedProps map[string]interface{}
sharedFuncMap template.FuncMap
templateFS fs.FS
}

// New function.
Expand All @@ -30,6 +32,14 @@ func New(url, rootTemplate, version string) *Inertia {
return i
}

// NewWithFS function.
func NewWithFS(url, rootTemplate, version string, templateFS fs.FS) *Inertia {
i := New(url, rootTemplate, version)
i.templateFS = templateFS

return i
}

// Share function.
func (i *Inertia) Share(key string, value interface{}) {
i.sharedProps[key] = value
Expand Down Expand Up @@ -155,7 +165,7 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin

viewData["page"] = page

ts, err := template.New(filepath.Base(i.rootTemplate)).Funcs(i.sharedFuncMap).ParseFiles(i.rootTemplate)
ts, err := i.createRootTemplate()
if err != nil {
return err
}
Expand All @@ -167,3 +177,13 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin

return nil
}

func (i *Inertia) createRootTemplate() (*template.Template, error) {
ts := template.New(filepath.Base(i.rootTemplate)).Funcs(i.sharedFuncMap)

if i.templateFS != nil {
return ts.ParseFS(i.templateFS, i.rootTemplate)
}

return ts.ParseFiles(i.rootTemplate)
}

0 comments on commit c2a3b4f

Please sign in to comment.