gotemplate-enginegorazor

gorazor: Can't find layout


For go i want to use the gorazor template engine. I used code from gorazor repository and from go template benchmark and ran gorazor tpl tpl and got panic: Can't find layout: rzrexmpl/tpl/layout/base [Home]:

gorazor tpl tpl
gorazor processing dir: tpl -> tpl
panic: Can't find layout: rzrexmpl/tpl/layout/base [Home]

The project rzrexmpl looks like this

rzrexmpl
├─── main.go
├───models
|     └─── user.go
└───tpl
    ├───helper
    |     ├─── footer.gohtml
    |     ├─── header.gohtml
    |     └─── msg.gohtml
    ├───layout
    |       └─── base.gohtml
    └─── home.gohtml

The relevant parts are

package main

import (
    "fmt"
    "net/http"
    "rzrexmpl/models"
    // "rzrexmpl/tpl" // available after gorazor was run successfully
)

func main(){

    // ....
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

        fmt.Fprintf(w, "after running gorazor uncomment line below %v", user)
        // fmt.Fprintf(w, tpl.Home(1, user)) // tpl will be generated by gorazor
    })
}

And from /tpl/home.gohtml

@{
    import (
        "rzrexmpl/models"
        "rzrexmpl/tpl/helper"
        "rzrexmpl/tpl/layout"
    )
    layout := layout.Base
    var totalMessage int
    var u *models.User
}

@helper.Header()
@helper.Msg(u)

@section title {
    <title>@u.Name's homepage</title>
}

@section side {
    switch totalMessage {
    case 0:
          <p>@u.Name has no message</p>
    case 1:
          <p>@u.Name has 1 messages</p>
    default:
          <p>@u.Name has @totalMessage messages</p>
    }
}

The full source code is at rzrexmple.

It is unclear to me what i have to change either in the template files or in the imports to be able to generate the code from the gohtml files.


Solution

  • You need to use -prefix parameter while generating go code by gorazor in case you want to use layouts:

    gorazor -prefix rzrexmpl ./tpl ./tpl
    

    See Examples section in the official documentation