Turning off / skipping the html escaping on just one variable

107 views
Skip to first unread message

RoboTamer

unread,
Sep 7, 2012, 3:21:18 AM9/7/12
to golan...@googlegroups.com

I did find the escape package and I found some older posts that no longer apply.
I pass a struct to the template that looks something like following, and I would like to just pass by the escaping for the Menu item, since I am creating html links with a function

type View struct {
    F.File
    Time uint
    Menu string
    Func template.FuncMap
}

func renderTemplate(w http.ResponseWriter, tmpl string, V *View) {
    t := template.New("gogit.html")
    t.Funcs(V.Func)
    t.SKIP_ESCAPE(V.Menu) // How do I do this?
}





Jesse McNelis

unread,
Sep 7, 2012, 3:26:18 AM9/7/12
to RoboTamer, golan...@googlegroups.com
On Fri, Sep 7, 2012 at 5:21 PM, RoboTamer <grue...@gmail.com> wrote:
type View struct {
F.File
Time uint
Menu template.HTML
Func template.FuncMap
}

"HTML encapsulates a known safe HTML document fragment. It should not
be used for HTML from a third-party, or HTML with unclosed tags or
comments. The outputs of a sound HTML sanitizer and a template escaped
by this package are fine for use with HTML."
http://golang.org/pkg/html/template/#HTML

For more information about how the html/template package works, read
the documentation.
http://golang.org/pkg/html/template/#overview




--
=====================
http://jessta.id.au

RoboTamer

unread,
Sep 7, 2012, 4:28:59 AM9/7/12
to golan...@googlegroups.com, RoboTamer, jes...@jessta.id.au

First thanks for the quick reply, but I still can't figure this out.
When I do the type casting somewhere something goes wrong, and I think it has something to do with pointers.
The error complains about line 70, and I have included line 70 and all it's associates down below. 

Type casting to HTML:
    u := &uri.Uri{Host: "ctoken.com", Appl: "wiki", Func: "v", Path: "", File: "about.html"}
    V.Menu = template.HTML(u.Link("cToken"))

The Error:
2012/09/07 01:09:57 http: panic serving 127.0.0.1:36228: runtime error: assignment to entry in nil map
/usr/local/go/src/pkg/net/http/server.go:576 (0x448416)
/tmp/bindist927634324/go/src/pkg/runtime/proc.c:1443 (0x410688)
/tmp/bindist927634324/go/src/pkg/runtime/runtime.c:128 (0x411154)
/tmp/bindist927634324/go/src/pkg/runtime/hashmap.c:952 (0x406d72)
/home/tamer/code/go/src/gotamer/uri/uri.go:70 (0x41ea1d)
(*Uri).Set: ss.Segs[c] = Segment{&key, &value}

Error pointer code:
type Uri struct {
    Host string           // localhost:8080
    Appl string           // Wiki, Blog
    Func string           // v,a,u,s view,add,update,save
    Path string           // Directory / Category
    File string           // The file name with html extention
    Segs map[uint]Segment // segments
    Frag string           // #fragment
}
type Segment struct {
    key   *string
    value *string
}






func (ss *Uri) Set(key string, value string) {
    //ResSegments.mu.Lock()
    //defer ResSegments.mu.Unlock()
    c := uint(len(ss.Segs))
    c++
    ss.Segs[c] = Segment{&key, &value}  // ### Line 70
}

Christoph Hack

unread,
Sep 7, 2012, 5:01:33 AM9/7/12
to golan...@googlegroups.com, RoboTamer, jes...@jessta.id.au
On Friday, September 7, 2012 10:28:59 AM UTC+2, RoboTamer wrote:
2012/09/07 01:09:57 http: panic serving 127.0.0.1:36228: runtime error: assignment to entry in nil map

You need to create the hash map before you can use it. Something like (note the last line):

u := &uri.Uri{
    Host: "ctoken.com",
    Appl: "wiki",
    Func: "v",
    Path: "",
    File: "about.html",
    Segs: make(map[uint]Segment),
}

-christoph

RoboTamer

unread,
Sep 7, 2012, 5:50:26 AM9/7/12
to golan...@googlegroups.com, RoboTamer, jes...@jessta.id.au

That's it, thank you
Reply all
Reply to author
Forward
0 new messages