Calling a template with a pipeline parameter is like calling a function with only one parameter

243 views
Skip to first unread message

dele...@gmail.com

unread,
Aug 9, 2013, 1:32:06 PM8/9/13
to golan...@googlegroups.com
Pipelines are nice, but sometimes the way to pass the right data to the right template looks awkward to me.
Let's say you have a site for Gophers about Gophers. It has a home page main template, and a utility template to print a list of Gophers.

http://play.golang.org/p/Jivy_WPh16

Now if I want to add some tiny bit of context in the subtemplate, well I can't because there is only one possible "dot" pipeline! What can I do?
Obviously I can copy-paste the subtemplate code into the main template (no way I do that).
Or I can juggle with some kind of global variables with accessors (no way I do that).
Or I can create a new specific struct for each template parameter list (not great).
The best i've found so far (and I don't really like it) is muxing and demuxing parameters with a "generic" pair container :

http://play.golang.org/p/ri3wMAubPX

type PipelineDecorator struct {
    // The actual pipeline
    Data interface{}
    // Some helper data passed as "second pipeline"
    Deco interface{}
}

func decorate(data interface{}, deco interface{}) *PipelineDecorator {
    return &PipelineDecorator{
        Data: data,
        Deco: deco,
    }
}

This is no theoretical problem, I actually use this trick a lot for building my website, and I wonder if there exist some more idiomatic way to achieve the same.
I would surely be happy if some future backward-compatible change in the template syntax allows passing multiple pipeline parameters, just like functions do.

Any thoughts ?
Best regards

Val

unread,
Aug 17, 2013, 7:25:58 AM8/17/13
to golan...@googlegroups.com
For futures readers of this unanswered topic : it was then discussed on Stack Overflow  http://stackoverflow.com/questions/18276173/calling-a-template-with-several-pipeline-parameters
Reply all
Reply to author
Forward
0 new messages