Search code examples
gogo-gormgo-fibergo-html-template

Golang Fiber Render - Sending data to multiple Layouts


I'm trying to switch from PHP to GO, but I'm stuck at one point, I ask for your help.

I linked navbar.html, which is where I will draw my menus and logo, into index.html.

but I can't pull the data that I assigned for the index into the navbar from navbar.html.

I tried to add navbar in render but got error. Do I need to create a separate function for each page? Or can I do it with a single function?

main.go


func (r *Repository) BilgiGetir(c *fiber.Ctx) error {
      ....
      .....
      ......
      ............


    return c.Render("index", fiber.Map{
        "Title":    titlebilgi.Bilgi1,
        "Bilgi":    bilgi,
        "IlanList": ilanlist,
    })

}

index.html (image ) index.html

{{ define "index" }}

<html>
     <title>{{.Title}}</title>
    {{ template "head" }}
<body class="cover-pull-top header-transparent">
<div class="page-wrapper">
    {{ template "navbar" }}      **   <------------------------------**
    <div class="main-wrapper">
        <div class="main">
            <div class="main-inner">
             
                <div class="content">

navbar.html (image )navbar.html

 {{ define "navbar" }}


 <div class="header-wrapper">
    <a class="header-logo" href="">
    <img src="emlakgo-logo-w_2.png" alt= '{{.Title}}' title='{{.Title}}'>   ** <-------------   **          
    </a><!-- /.header-logo -->

I want to pull a title but it doesn't come. I think I need to define a navbar in the render, but I can't add more than one layout in the render, I always get an error. how can i solve this problem.


Solution

  • You can pass data from one template to another by specifying the pipeline after the template's name, i.e.

    {{template "name" pipeline}}
    

    The template with the specified name is executed
    with dot set to the value of the pipeline.

    For example: {{ template "navbar" . }} or {{ template "navbar" .Title }} (with the latter you'd then reference the title inside navbar as just . instead of .Title)