How do I initialize extemplate (a template engine in golang) so that I can use with Echo minimalist framework

90 views
Skip to first unread message

Ehioje Henry Erabor

unread,
Jan 8, 2020, 9:59:30 AM1/8/20
to golang-nuts
I am having issues using extemplate library in my echo framework project. Library :https://github.com/dannyvankooten/extemplate

My Code is below:

Enter code here...package main

import (
"fmt"
"io"
"net/http"

)

// Template ...
type Template struct {
templates *extemplate.Extemplate
}

// Render ...
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
err := t.templates.ExecuteTemplate(w, name, data)
if err != nil {
fmt.Println("error", err)
return err
}

return nil
}

// Page ...
type Page struct {
Name string
Content string
}

// Hello ...
func Hello(c echo.Context) error {
return c.Render(http.StatusOK, "hello.html", "Guys")
}

// About ...
func About(c echo.Context) error {

data := Page{
Name: "About", Content: `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Nisi scelerisque eu ultrices vitae auctor eu augue ut lectus. Quam pellentesque nec nam aliquam sem et tortor consequat.
Pharetra vel turpis nunc eget lorem dolor. Vitae turpis massa sed elementum tempus egestas. Turpis egestas pretium aenean pharetra magna ac placerat.
Neque ornare aenean euismod elementum nisi quis eleifend quam. In fermentum et sollicitudin ac orci. Ut porttitor leo a diam sollicitudin tempor id eu nisl.
Sed viverra tellus in hac habitasse platea dictumst vestibulum rhoncus. Lorem ipsum dolor sit amet. A diam sollicitudin tempor id eu. Sit amet facilisis magna etiam.
Praesent tristique magna sit amet purus gravida.`,
}
return c.Render(http.StatusOK, "about.html", data)
}

func main() {

xt := &Template{}
err := xt.templates.ParseDir("templates/", []string{".html"})
if err != nil {
fmt.Println("ErrorExt: ", err)
}

e := echo.New()
e.Renderer = xt
e.GET("/hello", Hello)
e.GET("/about", About)

e.Logger.Fatal(e.Start(":4000"))

}


My folder structure where the template resides is below:

--main--
--templates--
   --about.html--
   --home.html--
--go.mod--
--go.sum--

I am using go modules for the project.
Why do I keep on getting these errors below:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x776c85]
goroutine 1 [running]:
github.com/dannyvankooten/extemplate.(*Extemplate).ParseDir(0x0, 0x8cb153, 0xa, 0xc0000ebf20, 0x1, 0x1, 0x32, 0x963ae0)
/home/henry/work/pkg/mod/github.com/dannyvankooten/extem...@v0.0.0-20180818082729-efbdf6eacd7e/template.go:110 +0x125
main.main()
/home/henry/goproject/extemplate/main.go:56 +0xa7
exit status 2




Chris Burkert

unread,
Jan 8, 2020, 11:24:42 AM1/8/20
to Ehioje Henry Erabor, golang-nuts
xt := &Template{} leaves the templates field nil.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0b664dbf-4ca4-41e5-aa55-b514101b81c6%40googlegroups.com.

Ehioje Henry Erabor

unread,
Jan 10, 2020, 9:23:32 AM1/10/20
to Chris Burkert, golang-nuts
Thanks Chris Burkert and Uzondu.

xt := &Template{} was indeed nil and was not initialized. 

I created a function that when called enabled me to initialize the extemplate before returning an instance of the used struct and its field (The field value of the struct is what is actually initialized).

func NewTemplate(path string) *Template {

  t := new(Template)
  t.templates = extemplate.New()
 ........
}
Reply all
Reply to author
Forward
0 new messages