Skip to content

Commit

Permalink
HTML Rendering documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Jan 25, 2024
1 parent 374d08b commit 6ac283b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions documentation/docs/tutorials/rendering/gomponents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Gomponents

Fuego supports rendering HTML templates with [Gomponents](https://github.com/maragudk/gomponents).

Just use the `fuego.Gomponent` type as a return type for your handler, and return the gomponent.
5 changes: 5 additions & 0 deletions documentation/docs/tutorials/rendering/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# HTML Rendering

Fuego is not only capable to handle XML and JSON, it can also render HTML.

It supports templating with [html/template](https://pkg.go.dev/html/template), [Templ](https://github.com/a-h/templ), and [Gomponents](https://github.com/maragudk/gomponents).
28 changes: 28 additions & 0 deletions documentation/docs/tutorials/rendering/std.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
sidebar_position: 1
---

# html/template

Fuego supports rendering HTML templates with the [html/template](https://pkg.go.dev/html/template) package.

Just use the `fuego.HTML` type as a return type for your handler, and return `c.Render()` with the template name and data.

```go
// highlight-next-line
func (rs Ressource) unitPreselected(c fuego.Ctx[any]) (fuego.HTML, error) {
id := c.QueryParam("IngredientID")

ingredient, err := rs.IngredientsQueries.GetIngredient(c.Context(), id)
if err != nil {
return "", err
}

// highlight-start
return c.Render("preselected-unit.partial.html", fuego.H{
"Units": types.UnitValues,
"SelectedUnit": ingredient.DefaultUnit,
})
// highlight-end
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ func (rs Ressource) adminIngredients(c fuego.Ctx[any]) (fuego.Templ, error) {
return admin.IngredientList(ingredients, searchParams), nil
}
```

Note that the `fuego.Templ` type is a simple alias for `fuego.CtxRenderer` : any type that implements the `Render(context.Context, io.Writer) error` method can be used as a return type for a handler.
12 changes: 9 additions & 3 deletions documentation/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const config: Config = {
// Please change this to your repo.
// Remove this to remove the "edit this page" links.
editUrl: "https://github.com/go-fuego/fuego",
sidebarCollapsed: false,
},
blog: {
showReadingTime: true,
Expand Down Expand Up @@ -66,7 +67,12 @@ const config: Config = {
type: "docSidebar",
sidebarId: "tutorialSidebar",
position: "left",
label: "Tutorial",
label: "Docs",
},
{
href: "https://pkg.go.dev/github.com/go-fuego/fuego",
position: "left",
label: "Reference",
},
{
href: "https://github.com/go-fuego/fuego",
Expand All @@ -82,8 +88,8 @@ const config: Config = {
title: "Docs",
items: [
{
label: "Tutorial",
to: "/docs/intro",
label: "Docs",
to: "/docs/",
},
],
},
Expand Down

0 comments on commit 6ac283b

Please sign in to comment.