Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Conversation

chungers
Copy link
Contributor

This PR introduces an interface called template.Context that the context for the template can optionally implement. If a struct value is used as the template context and this struct also implements this interface, the template engine will expose template functions of the form func(ctx template.Context, arg string) string as a regular template function of the form func(arg string) string. It does this by dynamically creating a new function which omits the first argument.

This allows the template developer to expose functions that can carry state that are not visible to the users of the template and still keep the template function easy to use (no context parameter in the template function arguments).

In essence, this PR allows a template to be viewed as a complex, user-programmable function whose evaluation/application not only produces a string (the output of the template) but also a complex value where its state can be updated via template function calls as they appear in the template text. An example of this is in pkg/template/integration_test.go where a simple context struct exports template functions to print and mutate some state and keeping track of invocation count. At the end of the evaluation of the template, a string view is rendered and the state of the struct reflects the function invocations in the text template.

David Chung added 3 commits January 30, 2017 13:25
…te evaluation

Signed-off-by: David Chung <david.chung@docker.com>
Signed-off-by: David Chung <david.chung@docker.com>
@GordonTheTurtle
Copy link

Please sign your commits following these rules:
https://github.com/docker/docker/blob/master/CONTRIBUTING.md#sign-your-work
The easiest way to do this is to amend the last commit:

$ git clone -b "template-context" git@github.com:chungers/infrakit.git somewhere
$ cd somewhere
$ git rebase -i HEAD~842353812120
editor opens
change each 'pick' to 'edit'
save the file and quit
$ git commit --amend -s --no-edit
$ git rebase --continue # and repeat the amend for each commit
$ git push -f

Amending updates the existing PR. You DO NOT need to open a new one.

@chungers chungers added the v0.3 label Jan 30, 2017
@chungers chungers merged commit 17f7130 into docker-archive:master Jan 30, 2017
@chungers chungers deleted the template-context branch January 30, 2017 23:40
@chungers chungers modified the milestone: v0.3 Jan 31, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants