FAQ
Reviewers: campoy,

Message:
Hello campoy@google.com (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go.talks


Description:
go.talks/present: remove presentation logic from data structures

Remove the Template field from Doc, and the Doc field from Section.
The new Render method on Doc wraps the Doc in an anonymous struct that
also contains the template, before passing it to the template iteslf.
The renderElem function wraps Section elements in a similar way.

Please review this at https://codereview.appspot.com/6863058/

Affected files:
M pkg/present/parse.go
M present/dir.go
M present/templates/action.tmpl


Index: pkg/present/parse.go
===================================================================
--- a/pkg/present/parse.go
+++ b/pkg/present/parse.go
@@ -24,12 +24,20 @@
funcs = template.FuncMap{}
)

-// Template returns an initialized template with the action functions in
its
-// FuncMap.
+// Template returns an empty template with the action functions in its
FuncMap.
func Template() *template.Template {
return template.New("").Funcs(funcs)
}

+// Render renders the doc to the given writer using the provided template.
+func (d *Doc) Render(w io.Writer, t *template.Template) error {
+ var data = struct {
+ *Doc
+ Template *template.Template
+ }{d, t}
+ return t.ExecuteTemplate(w, "root", data)
+}
+
type ParseFunc func(fileName string, lineNumber int, inputLine string)
(Elem, error)

// Register binds the named action, which does not being with a period, to
the
@@ -49,7 +57,6 @@
Time time.Time
Authors []Author
Sections []Section
- Template *template.Template
}

// Author represents the person who wrote and/or is presenting the
document.
@@ -76,7 +83,6 @@
Number []int
Title string
Elem []Elem
- Doc *Doc
}

func (s Section) Sections() (sections []Section) {
@@ -115,7 +121,14 @@
// renderElem implements the elem template function, used to render
// sub-templates.
func renderElem(t *template.Template, e Elem) (template.HTML, error) {
- return execTemplate(t, e.TemplateName(), e)
+ var data interface{} = e
+ if s, ok := e.(Section); ok {
+ data = struct {
+ Section
+ Template *template.Template
+ }{s, t}
+ }
+ return execTemplate(t, e.TemplateName(), data)
}

func init() {
@@ -268,7 +281,6 @@
section := Section{
Number: append(append([]int{}, number...), i),
Title: text[len(prefix)+1:],
- Doc: doc,
}
text, ok = lines.nextNonEmpty()
for ok && !lesserHeading(text, prefix) {
Index: present/dir.go
===================================================================
--- a/present/dir.go
+++ b/present/dir.go
@@ -63,7 +63,7 @@
// and executes the template, sending output to w.
func renderDoc(w io.Writer, base, docFile string) error {
// Read the input and build the doc structure.
- pres, err := parse(docFile, 0)
+ doc, err := parse(docFile, 0)
if err != nil {
return err
}
@@ -85,10 +85,8 @@
return err
}

- pres.Template = tmpl
-
// Execute the template.
- return tmpl.ExecuteTemplate(w, "root", pres)
+ return doc.Render(w, tmpl)
}

func parse(name string, mode present.ParseMode) (*present.Doc, error) {
Index: present/templates/action.tmpl
===================================================================
--- a/present/templates/action.tmpl
+++ b/present/templates/action.tmpl
@@ -5,7 +5,7 @@

{{define "section"}}
<h{{len .Number}} id="TOC_{{.FormattedNumber}}">{{.FormattedNumber}}
{{.Title}}</h{{len .Number}}>
- {{range .Elem}}{{elem $.Doc.Template .}}{{end}}
+ {{range .Elem}}{{elem $.Template .}}{{end}}
{{end}}

{{define "list"}}

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 5, '12 at 3:52a
activeDec 5, '12 at 4:09a
posts3
users2
websitegolang.org

2 users in discussion

Adg: 2 posts Dsymonds: 1 post

People

Translate

site design / logo © 2022 Grokbase