Skip to content

Instantly share code, notes, and snippets.

@jonathaningram
Created August 28, 2015 07:04

Revisions

  1. jonathaningram created this gist Aug 28, 2015.
    10 changes: 10 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    Question: could the child/overlay/aboutPage get the default content from the master with something like `{{parent}}` or `{{master}}`?

    E.g.

    ```golang
    const (
    layout = `<title>{{block "metaTitle" .}}golang.org{{end}}</title><body>{{block "content"}}{{end}}</body>`
    aboutPage = `{{define "metaTitle"}}Read all about Go - {{parent}}{{end}} {{define "content"}}About page content{{end}}`
    )
    ```
    21 changes: 21 additions & 0 deletions block.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    const (
    layout = `<title>{{block "metaTitle" .}}Welcome to golang.org{{end}}</title><body>{{block "content"}}{{end}}</body>`
    aboutPage = `{{define "metaTitle"}}Read all about Go on golang.org{{end}} {{define "content"}}About page content{{end}}`
    )
    layoutTmpl, err := template.New("layout").Parse(layout)
    if err != nil {
    log.Fatal(err)
    }
    aboutPageTmpl, err := layoutTmpl.Overlay(aboutPage)
    if err != nil {
    log.Fatal(err)
    }
    if err := layoutTmpl.Execute(os.Stdout, nil); err != nil {
    log.Fatal(err)
    }
    if err := aboutPageTmpl.Execute(os.Stdout, nil); err != nil {
    log.Fatal(err)
    }
    // Output:
    // <title>Welcome to golang.org</title><body></body>
    // <title>Read all about Go on golang.org</title><body>About page content</body>