Comments (4)
@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.
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.
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>
© 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.
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)
- How to change content header? HOT 1
- FunctionMaps within a Controller function HOT 11
- Relative URL in template HOT 3
- json encoding remove fields HOT 2
- Transfer "HTMLContentType" from Options to HTMLOptions
- Is it possible to have multiple layouts? HOT 5
- Not use a template for specific view HOT 1
- Value not found for type render.Render HOT 4
- How to show multi variables in tmpl file? HOT 2
- Define sections for template parsing. HOT 2
- yield'ing some specific part
- Calling function from template
- 404 error on static directory HOT 1
- Default options inconsistent with those presented in the README
- Possible Memory Leak
- Missing return at end of function HOT 2
- need an other "yield"
- where is the template syntax document ? HOT 1
- Layout reloading every redirection HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from render.