0

I want to render a template using a base template and one of two partial templates, based on the context. Something like:

   Patial A                          Base                          Template
|────────────|                 |───────────────|               |───────────────|
|            |                 |               |               |               |
|  AAAAAAAA  |  componse with  | {{ .Target }} |   to yield    |   AAAAAAAA    | 
|            |                 |               |               |               |
|────────────|                 |───────────────|               |───────────────|
    
    
   Patial A                          Base                          Template
|────────────|                 |───────────────|               |───────────────|
|            |                 |               |               |               |
|  BBBBBBBB  |  componse with  | {{ .Target }} |   to yield    |   BBBBBBBB    | 
|            |                 |               |               |               |
|────────────|                 |───────────────|               |───────────────|

Implementation

I constructed an experiement using the following folder structure, templates, and logic

Folder Structure

templates/
│
├── main.go
└── view/
    ├── base.html
    ├── partailA.html
    ├── partailB.html

Templates

I used the html/template define and block and layout actions:

{{ define "base" .}}
<!-- This is the base template -->
<div>
   <!-- insert particl template here -->
   {{ block "partial" . }}{{ end }}
</div>
    
{{ end }}

These are the partial templates:

**{{ define "partial" }}**
<!-- This is a partical template -->
<div>
    <p>Partial Template A</p>
</div>
{{ end }}
{{ define "partial"}}
<!-- This is another template -->
<div>
    <p>Partial Template B</p>
</div>
{{ end }}

Logic

package main

import (
    "log"

    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/template/html/v2"
)
    
const (
    ADMIN         = "admin"
)

var LayoutDir string = "views/layouts"
var bootstrap *template.Template

func main() {
    var err error

    // Create a new engine
    engine := html.New("./views", ".html")

    // Pass the engine to the Views
    app := fiber.New(fiber.Config{
        Views: engine,
    })
    app.Static("/", "./public")

    app.Get("/patialA", func(c *fiber.Ctx) error {
        // Render index
        return c.Render("partialA", fiber.Map{}, "base")
    })

    app.Get("/patialB", func(c *fiber.Ctx) error {
        // Render index
        return c.Render("partialB", fiber.Map{}, "base")
    })
    log.Fatal(app.Listen(":3000"))
}

After trying all kinds of suggestions, including from ChatGPT, I gave up on the Go, html/template, and Fiber stack, switched to Go, html/template, and Gin, and made it work.

Regardless, I'm still curious about whether there is a way to make it work with Fiber.

1 Answer 1

2

The main correction (after correcting some typos including names like "view -> views") is using the tag `embed` in the template where you wish to insert other template at runtime and name partial templates uniquely:

views/base.html:

{{ define "base" }}
<!-- This is the base template -->
<div>
    <div>base block</div>
   <!-- insert partial template here -->
    {{embed}}
</div>

{{ end }}

views/partialA.html:

{{ define "partialA" }}
<!-- This is a partial template -->
<div>
    <p>Partial Template A</p>
</div>
{{ end }}

views/partialB.html:

{{ define "partialB"}}
<!-- This is another template -->
<div>
    <p>Partial Template B</p>
</div>
{{ end }}

main.go:

func main() {
    // var err error

    // Create a new engine
    engine := html.New("./views", ".html")

    // Pass the engine to the Views
    app := fiber.New(fiber.Config{
        Views: engine,
    })
    app.Static("/", "./public")

    app.Get("/patialA", func(c *fiber.Ctx) error {
        // Render index
        return c.Render("partialA", fiber.Map{}, "base")
    })

    app.Get("/patialB", func(c *fiber.Ctx) error {
        // Render index
        return c.Render("partialB", fiber.Map{}, "base")
    })
    log.Fatal(app.Listen(":3000"))
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I discovered the subtlety of naming the partial templates uniquelly in their {{ define }} tags while experimenting with Gin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.