How to debug a template?


#1

Hello,
I am trying to customise homepage or other templates. are there any methods people use to help debug the code? are we simply just rendering the variables in the html outputs and look at them?

Many thanks
regards


#2

This is helpful:


#3

There’s no general purpose inspector that would allow you to walk up and down the environment stack as Hugo renders the template. So far, there hasn’t been a demand for something like that because Hugo supports “live reloads” of pages.

That feature, which enables your browser to keep in sync with changes to content and templates, makes it easy to modify templates and see the results in almost real time. I know that I’m abusing the term, but it makes using Hugo similar to using a language that has a Read-Eval-Print loop. Debugging consists of interacting with the template via the browser. You save a change and inspect the output in your browser. Hugo’s exposed the Go template’s printf function, so you can dump the variable to the browser when something isn’t working.

I admit that debugging via printf isn’t perfect, but Hugo is so fast that it’s very viable.


#4

Thanks for this tip. It’s great. To add to this, if you have Notepad++, download the JSTool plugin, and copy/paste the content into a new file, then plugins > JSTool > JSFormat. You’ll get nice readable data:

 & hugolib.Node {
    RSSLink : "http://localhost:1313/index.xml",
    Site : ( * hugolib.SiteInfo)(0xc08245c060),
    Data : map[string]interface {}
    {
        "Pages" : hugolib.Pages {
            ( * hugolib.Page)(0xc08202bc00),
            ( * hugolib.Page)(0xc08202b400),
            ( * hugolib.Page)(0xc0823acc00),
            ( * hugolib.Page)(0xc082167000),
            ( * hugolib.Page)(0xc082166400),
            ( * hugolib.Page)(0xc0821e6000)
        }
    },
    Title : "Test",
    Description : "",
    Keywords : []string(nil),
    Params : map[string]interface {}

    (nil),
    Date : time.Time {
        sec : 0,
        nsec : 0,
        loc : ( * time.Location)(nil)
    },
    Sitemap : hugolib.Sitemap {
        ChangeFreq : "",
        Priority : 0
    },
    UrlPath : hugolib.UrlPath {
        Url : "/",
        Permalink : "http://localhost:1313/",
        Slug : "",
        Section : ""
    },
    paginator : ( * hugolib.pager)(nil),
    paginatorInit : sync.Once {
        m : sync.Mutex {
            state : 0,
            sema : 0x0
        },
        done : 0x0
    },
    scratch : ( * hugolib.Scratch)(nil)
}