Code Monkey home page Code Monkey logo

Comments (4)

mickelsonm avatar mickelsonm commented on September 6, 2024

@blachniet: Anything is possible. I think what you're looking for is here: http://golang.org/pkg/text/template/ or {{template "path/to/my/template" .}}

On your layout you can do something like:

<body>
    {{template "shared/header" .}}
    {{yield}}
    {{template "shared/footer" .}}
</body>

Make sure you put your layout and all other associated templates in your templates directory and that you remember that yield acts more like a placeholder for whatever template you call. For example if I had:

routing on main.go

m.Get("/", func(r render.Render) {
    r.HTML(200, "index")
})

index.tmpl (content)

<h1>Widgets and Gizmos!</h1>

index.tmpl (rendered with my layout)

My first page would get rendered/displayed as:

<body>
    <header>My content from shared/header.tmpl</header>
    <h1>Widgets and Gizmos!</h1>
    <footer>My content from shared/footer.tmpl</footer>
</body>

The main thing to watch out for is that your file paths are pointing to the right spots, otherwise you'll get errors. Also, keep in mind that the {{template}} way of doing things is built into the Go templating system, so you can build things to act like web components and re-use them in different parts of your web application. Hope this helps!

from render.

mattkanwisher avatar mattkanwisher commented on September 6, 2024

Yeah problem is different pages need to inject different javascript into the HEAD section of the page. In rails they allow named yields, so the inner template can inject in multiple spots into the layout

from render.

mickelsonm avatar mickelsonm commented on September 6, 2024

Sounds like a nifty feature in Rails. If I were trying to tackle this using Go templates/render package, I would probably set mine up something like this:

layout.tmpl

<html>
<head>
    {{range .JavaScriptFiles}}
    <script type="text/javascript" src="{{.}}"></script>
    {{end}}
</head>
<body>
    <header>
        <ul>
            <li><a href="/">Home</a></li>
            <li><a href="/about">About Us</a></li>
        </ul>
    </header>
    <section>
    {{yield}}
    </section>
    <footer>
        &copy; MyCompany Inc.
    </footer>
</body>
</html>

index.go

package main

import (
    "net/http"

    "github.com/go-martini/martini"
    "github.com/martini-contrib/render"
)

func main() {
    m := martini.Classic()

    m.Use(render.Renderer(render.Options{
        Directory:       "templates",
        Layout:          "layout",
        Extensions:      []string{".tmpl", ".html"},
        Delims:          render.Delims{"{{", "}}"},
        Charset:         "UTF-8",
        HTMLContentType: "text/html",
    }))

    m.Get("/", func(req *http.Request, r render.Render) {
        data := make(map[string]interface{})
        data["JavaScriptFiles"] = getJavaScriptFiles(req.URL.Path)
        r.HTML(200, "home", data)
    })

    m.Get("/about", func(req *http.Request, r render.Render) {
        data := make(map[string]interface{})
        data["JavaScriptFiles"] = getJavaScriptFiles(req.URL.Path)
        r.HTML(200, "about", data)
    })

    m.Run()
}

func getJavaScriptFiles(path string) []string {
    var scripts []string

    //common scripts that all pages have
    scripts = []string{
        "https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js",
    }

    //specific pages
    switch path {
    case "/about":
        scripts = append(scripts,
            "https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js",
        )
    }

    //application script
    scripts = append(scripts, "scripts/app.js")

    return scripts
}

Setting it up this way you have some control on what scripts go what page, in what order, etc. This works, but I would look into using something like RequireJS for javascript loading on specific pages, managing dependencies, modularizing the javascript, etc. It is also nice because your template would have a single javascript file. Lastly, it is good practice to put scripts at the bottom of the page in the body section of the page. Good luck and I hope this helps!

from render.

blachniet avatar blachniet commented on September 6, 2024

The only thing that I don't like about this approach is that the controller-esque code needs some view-specific knowledge. However, as you pointed out, this could be avoided by using RequireJS.

from render.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.