Skip to content

text/template: Allow func to take region as final argument #4727

@robfig

Description

@robfig
I would like to extend Go templates to allow funcs to accept a region as an argument. 
This is useful any time you want to process or transform a section of text. 

For example, I would like to write a func that performs syntax highlighting
(server-side, while all existing solutions are in javascript):

{{/* A sample template */}}
...
A description of my ExampleFunc function and an example of its usage:
{{prettify "go"}}
func ExampleFunc() {
  ...
}
{{end}}

For that, I would like to use a template func with a signature like:

"prettify": func(lang, body string) template.HTML

The value of the parameter is the region, already executed.  This is necessary to allow
usage of template functionality within the region:

{{prettify "go"}}
  {{range .examples}}
    func Example1() {
      {{.}}
    }
  {{end}}
{{end}}

The func may be called using the current convention, e.g. {{prettify "go"
"func x()"}}, but the template system allows any func with a final argument of
type "string" to use the region form.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.Thinking

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions