Error "incomplete or empty template" at execute a template

9,408 views
Skip to first unread message

Archos

unread,
Aug 5, 2012, 5:21:53 PM8/5/12
to golan...@googlegroups.com
Is right that the package "text/template" returns the next error?

template: :1: "" is an incomplete or empty template

I'm supposed that it is because the template file has not delimiters.

I've to use it so, because my program reads a file which was generated auto. where some times it will add the delimiters for that they can be substituted.

Rodrigo Moraes

unread,
Aug 5, 2012, 6:50:16 PM8/5/12
to golang-nuts
On Aug 5, 6:21 pm, Archos wrote:
> Is right that the package "text/template" returns the next error?
>
> template: :1: "" is an incomplete or empty template
>
> I'm supposed that it is because the template file has not delimiters.

That happens when no template was parsed, or an error occurred while
parsing, so that the template tree is nil. Check the error after
parsing, or paste some code to show what you are doing.

-- rodrigo

Archos

unread,
Aug 6, 2012, 5:39:03 AM8/6/12
to golan...@googlegroups.com

The template is correctly parsed since there is not errow with the next code:

    tmpl, err = template.New("").ParseFiles("modsql_1.sql")
    if err != nil {
        log.Fatal(err)
    }

The error comes when it is run:

    buf := new(bytes.Buffer)
    if err = tmpl.Execute(buf, data); err != nil {
        log.Fatal(err)
    }

Archos

unread,
Aug 6, 2012, 7:53:43 AM8/6/12
to golan...@googlegroups.com
The template "tmpl" is nill, like you said, because it already have been executed.

Archos

unread,
Aug 7, 2012, 4:57:41 AM8/7/12
to golan...@googlegroups.com
The error continues; I'd another errors that were masking this one.

$ cat templ.txt
CREATE TABLE types (
    id       bigint PRIMARY KEY,
    t_int    {{.PostgreInt}},
    t_float  double precision,
    t_string text,
    t_bool   boolean
);

// * * *
package main

import (
    "bytes"
    "fmt"
    "log"
    "text/template"
)

type sqlInt struct {
    MySQLInt   string
    PostgreInt string
}

func main() {
    tmpl, err := template.New("").ParseFiles("templ.txt")

    if err != nil {
        log.Fatal(err)
    }

    i := sqlInt{"INT", "integer"}

    buf := new(bytes.Buffer)
    if err = tmpl.Execute(buf, i); err != nil {
        log.Fatal(err)
    }

    fmt.Println(buf.String())
}
// * * *

El domingo, 5 de agosto de 2012 23:50:16 UTC+1, Rodrigo Moraes escribió:

Andrew Gerrand

unread,
Aug 7, 2012, 6:38:46 AM8/7/12
to Archos, golan...@googlegroups.com
A *template.Template is actually a group of named templates.

This line

> tmpl, err := template.New("").ParseFiles("templ.txt")

creates a *template.Template (tmpl) with two named templates: "" and
"templ.txt". The latter ("templ.txt") contains your actual template
data, while the former ("") is empty.

When you call tmpl.Execute, it tries to execute the first template,
"", which has no content. Hence the error.

You can fix this in one of two ways:

1. Call tmpl.ExecuteTemplate and name "templ.txt" explicitly:

> if err = tmpl.ExecuteTemplate(buf, "templ.txt", i); err != nil {
> log.Fatal(err)
> }

2. Use the package-level function template.ParseFiles to parse your template:

> tmpl, err := template.ParseFiles("templ.txt")

This returns a *template.Template with just one template, "templ.txt",
and so a call to tmpl.Execute will render just that template.

Andrew

Archos

unread,
Aug 7, 2012, 6:51:52 AM8/7/12
to golan...@googlegroups.com, Archos
Ops! My stupid mistake. Thanks!
Reply all
Reply to author
Forward
0 new messages