Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unexpected "|" in template clause #290

Closed
estahn opened this issue Apr 18, 2018 · 8 comments
Closed

unexpected "|" in template clause #290

estahn opened this issue Apr 18, 2018 · 8 comments
Assignees

Comments

@estahn
Copy link

estahn commented Apr 18, 2018

Template:

{{- template "base" -}}
{{ define "jobs" }}
  {{ template "job-checkout_code" | indent 4 }}
{{ end }}

Error message:
Error: template: -:39: unexpected "|" in template clause

@hairyhenderson
Copy link
Owner

Thanks for logging this, @estahn!

That doesn't seem to be quite the right syntax, though there's still a bug. Using the built-in js function, this fails in the same way:

$ gomplate -i '{{define "foo"}}hello world{{end}}{{ template "foo" | js }}'   
Error: template: <arg>:1: unexpected "|" in template clause

But this succeeds:

$ gomplate -i '{{define "foo"}}hello world{{end}}{{ template "foo" . | js }}'
hello world

This should succeed, but doesn't:

$ gomplate -i '{{define "foo"}}hello world{{end}}{{ template "foo" . | strings.ToUpper }}'
Error: template: <arg>:1:56: executing "<arg>" at <strings>: wrong type for value; expected string; got *main.Context

So, clearly there's something missing 😉

@hairyhenderson hairyhenderson self-assigned this Apr 19, 2018
@hairyhenderson
Copy link
Owner

Ok, this is a pretty widely-scoped bug. Essentially, I have a number of functions that accept only strings, which should really be able to handle anything, converting to string as necessary.

Interestingly, at first glance strings.Indent isn't only accepting string, and indeed it gives a different error:

$ gomplate -i '{{define "foo"}}hello world{{end}}{{ template "foo" . | strings.Indent }}' 
2018/04/18 20:39:19 Indent: invalid arguments

This is because it takes ...interface{} as its arguments. It immediately fails if the last element in that array is a non-string, though.

All this to say - the fix for strings.Indent/indent will be slightly different than the fix for every other function that expects a string.

I'm on it - just need to step out for a quick jog and I'll try and get a fix drafted tonight or tomorrow 👍

@hairyhenderson
Copy link
Owner

Ok, further discovery - my reading of the template docs was way off... When rendering templates of the form {{ template "T1" pipeline }}, I thought the T1 template would be piped through pipeline, but that's not at all true - instead the value set by pipeline is passed to T1 as the context (.):

$ gomplate -i '{{ define "T1" }}hello {{ . }}{{end}}{{template "T1" "world" | strings.ToUpper }}'
hello WORLD
$ gomplate -i '{{ define "T1" }}hello {{ .hello }}{{end}}{{template "T1" data.JSON `{"hello":"world"}` }}'
hello world

So, based on all this, this isn't a bug in gomplate, but a simple syntax issue.

I'd be nice to be able to handle many different kinds of input to the functions in the strings namespace, but that's orthogonal to this issue.

I'm going to close this now - feel free to re-open if you disagree 🙂

@estahn
Copy link
Author

estahn commented Apr 19, 2018

I feel that helps sort of with #291 , but don't see how it will help with the indentation of the output of {{ template ...}}.

@hairyhenderson
Copy link
Owner

don't see how it will help with the indentation of the output of {{ template ...}}.

I'm not sure this is possible with template unfortunately. You'd have to alter it where it's defined, which may or may not be awkward:

$ gomplate -i '{{ define "T1" }}{{ strings.Indent .width "." "hello world" }}{{ end -}}
{{ template "T1" data.YAML "width: 3" }}
{{ template "T1" data.YAML "width: 4" }}'
...hello world
....hello world

Hope that helps!

@estahn
Copy link
Author

estahn commented Apr 19, 2018

@hairyhenderson Thanks for the support and quick turnaround. 👍

@hairyhenderson
Copy link
Owner

🙇

@RevantBabu
Copy link

The other approach I think (especially while including template files) is to use tmpl.Exec (that returns a string which can be piped to strings.Indent)

{{ tmpl.Exec "t/input.tmpl" . | strings.Indent 4 }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants