Comments (1)
Hi @Jimmy99
Good question. It's really up to you and what your page requires. Page.Data
can (obviously) be anything. In my experience, it usually makes sense to have a custom type for the given page that contains everything you want to provide to the templates. You populate that then assign that to Page.Data
. It's very flexible.
IE (very made up, simple example):
type shoppingCartPageData struct {
Products []ent.Product
PromoCode ent.PromoCode
Total float64
}
func (c *shoppingCart) Get(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = "cart"
page.Name = "Shopping cart"
// Fetch data from ent, etc....
page.Data = shoppingCartPageData{
Products: products,
PromoCode: promo,
Total: productsSumPrice(products),
}
return c.RenderPage(ctx, page)
}
And in your template:
{{- range .Data.Products}}
<div class="product">{{.Name}} (${{.Price}})</div>
{{- end}}
Total: ${{.Data.Total}}
Unless your page really will only ever require 1 thing, then you can just assign that directly to Page.Data
rather than having a wrapper type to contain everything.
There's a very basic example of passing an ent
entity (user) to the template, though it's not part of Page.Data
:
- https://github.com/mikestefanello/pagoda/blob/main/pkg/controller/page.go#L72
- https://github.com/mikestefanello/pagoda/blob/main/templates/pages/home.gohtml#L18
Let me know if that helps and if you run in to any problems.
from pagoda.
Related Issues (20)
- Workflow on making migrations with Ent and Atlas? HOT 3
- Syntax Highlighting for gohtml files HOT 1
- Error initializing ORM HOT 3
- Flash messages when using HTMX HOT 2
- Can't update Go and deps cleanly HOT 5
- htmx problem HOT 1
- Feature idea: Deployment examples HOT 10
- Switching to embed.FS broke hot-reload of templates HOT 1
- A few suggestions / questions from a first time user HOT 10
- Problem implementing redirect with querystring parameters HOT 2
- Request for Tailwind CSS Support HOT 3
- Create a space for discussions, support and future roadmap planning HOT 2
- Best approach for mocking container services HOT 6
- what is the correct use of page.Form HOT 1
- Thoughts on Example for Splitting Out "Business Logic Layer"? HOT 2
- Adding custom javascript to an individual page HOT 1
- Default to SQLite rather than Postgres and Redis HOT 2
- Switch from Bulma to Tabler? HOT 2
- Setup problem HOT 2
- PocketBase integration into the administration area HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pagoda.