1
0
mirror of https://github.com/golang/go synced 2024-09-28 18:14:29 -06:00
The Go programming language
Go to file
Mike Samuel 4c6454aecf exp/template/html: escape {{template}} calls and sets of templates
This adds support for {{template "callee"}} calls.
It recognizes that calls can appear in many contexts.

{{if .ImageURL}}
    <img src="{{.ImageURL}}" alt="{{template "description"}}">
{{else}}
    <p>{{template "description"}}</p>
{{end}}

calls a template in two different contexts, first in an HTML attribute
context, and second in an HTML text context.

Those two contexts aren't very different, but when linking text
to search terms, the escaping context can be materially different:

<a href="/search?q={{template "tags"}}">{{template "tags"}}</a>

This adds API:
EscapeSet(*template.Set, names ...string) os.Error

takes a set of templates and the names of those which might be called
in the default context as starting points.

It changes the escape* functions to be methods of an object which
maintains a conceptual mapping of
(template names*input context) -> output context.

The actual mapping uses as key a mangled name which combines the
template name with the input context.

The mangled name when the input context is the default context is the
same as the unmangled name.

When a template is called in multiple contexts, we clone the template.

{{define "tagLink"}}
  <a href="/search?q={{template "tags"}}">{{template "tags"}}</a>
{{end}}
{{define "tags"}}
  {{range .Tags}}{{.}},{{end}}
{{end}}

given []string{ "foo", "O'Reilly", "bar" } produces

  <a href="/search?q=foo,O%27Reilly,bar">foo,O&#39;Reilly,bar</a>

This involves rewriting the above to something like

{{define "tagLink"}}
  <a href="/search?q={{template "tags$1"}}">{{template "tags"}}</a>
{{end}}
{{define "tags"}}
  {{range .Tags}}{{. | html}},{{end}}
{{end}}
{{define "tags$1"}}
  {{range .Tags}}{{. | urlquery}},{{end}}
{{end}}

clone.go provides a mechanism for cloning template "tags" to produce
"tags$1".

changes to escape.go implement the new API and context propagation
around the call graph.

context.go includes minor changes to support name mangling and
context_test.go tests those.

js.go contains a bug-fix.

R=nigeltao, r
CC=golang-dev
https://golang.org/cl/4969072
2011-09-13 16:57:39 -07:00
doc doc: link to notable blog posts 2011-09-10 09:35:25 +10:00
include 5a, 5c, 6a, 6c, 8a, 8c: fix Windows file paths 2011-09-07 15:49:56 -04:00
lib godoc: fine tuning of template file 2011-09-08 18:27:26 -07:00
misc gofmt: indent multi-line signatures 2011-09-06 11:27:36 -07:00
src exp/template/html: escape {{template}} calls and sets of templates 2011-09-13 16:57:39 -07:00
test gc: clean up if grammar 2011-09-12 15:52:29 -04:00
.hgignore exp/norm: added trie lookup code and associated tests. 2011-08-10 15:34:12 +02:00
.hgtags tag release.r60 2011-09-08 12:54:35 +10:00
AUTHORS A+C: Paul Lalonde (individual CLA). 2011-09-08 16:31:13 +10:00
CONTRIBUTORS A+C: Paul Lalonde (individual CLA). 2011-09-08 16:31:13 +10:00
favicon.ico add a favicon plus a couple of hi-res versions of gordon 2009-10-26 10:13:07 -07:00
LICENSE LICENSE: separate, change PATENTS text 2010-12-06 16:31:59 -05:00
PATENTS LICENSE: separate, change PATENTS text 2010-12-06 16:31:59 -05:00
README build: update, streamline documentation for new $GOBIN 2010-08-24 20:00:50 -04:00
robots.txt godoc: serve robots.txt raw 2011-02-19 05:46:20 +11:00

This is the source code repository for the Go programming language.  

For documentation about how to install and use Go,
visit http://golang.org/ or load doc/install.html in your web browser.

After installing Go, you can view a nicely formatted
doc/install.html by running godoc --http=:6060
and then visiting http://localhost:6060/doc/install.html.

Unless otherwise noted, the Go source files are distributed
under the BSD-style license found in the LICENSE file.

--

Binary Distribution Notes

If you have just untarred a binary Go distribution, you need to set
the environment variable $GOROOT to the full path of the go
directory (the one containing this README).  You can omit the
variable if you unpack it into /usr/local/go, or if you rebuild
from sources by running all.bash (see doc/install.html).
You should also add the Go binary directory $GOROOT/bin
to your shell's path.

For example, if you extracted the tar file into $HOME/go, you might
put the following in your .profile:

    export GOROOT=$HOME/go
    export PATH=$PATH:$GOROOT/bin

See doc/install.html for more details.