pystache icon indicating copy to clipboard operation
pystache copied to clipboard

Template Helpers

Open rluders opened this issue 10 years ago • 11 comments

Hi,

It's most a question and maybe a request. It's possible to create a helper with parameters? Some like this sample:

{{hello name='Ricardo'}}
def hello(name=None):
    return 'Hello, %s!', name

The expected result is: Hello, Ricardo!

rluders avatar Mar 28 '14 20:03 rluders

@rluders checkout the lambdas example: https://github.com/defunkt/pystache/blob/master/pystache/tests/examples/lambdas.mustache https://github.com/defunkt/pystache/blob/master/pystache/tests/examples/lambdas.py

Docs here: https://mustache.github.io/mustache.5.html

Your example would look like:

{{#hello}}Richardo{{/hello}}

The python would be the same, afaik.

Hope that helps!

BigBlueHat avatar May 02 '14 13:05 BigBlueHat

So, that's the example that I saw, but they only accept one parameter. They assume that everything between the template maker is a single parameter, when I need to pass multiple parameters.

rluders avatar May 02 '14 16:05 rluders

Not particularly elegant but I've always just done the parameter unpacking in the lambda itself e.g.

{{#hello}}Bob Bobbinson{{/hello}}

def hello(args):
    first_name, last_name = args.split(' ')
    return 'Hello, %s of the %s family' % (first_name, last_name)

kevincarrogan avatar May 02 '14 17:05 kevincarrogan

Yeah... Would be nice but I don't think it's been done in any of the mustache implementations that I've seen...nor in the spec. Probably that's due to putting "too much" data in the template.

Could you make lambdas or prefilled data to serve this purpose?

BigBlueHat avatar May 02 '14 17:05 BigBlueHat

@kevincarrogan's example gets what you need. I think. If you wanted key=value you could certainly parse that instead.

Not "idea" perhaps, but any change of this sort would have to happen upstream in the spec or via a pragma (first...then spec) to really get traction.

The best thing about Mustache is that it's (relatively) consistently updated across languages. Keeping that intact is important for its future.

BigBlueHat avatar May 05 '14 17:05 BigBlueHat

So, that's how I'm doing today. I pass the values like this:

{{#sum}}x=1,y=2{{/sum}}

But I don't think that's looks elegant. Look, I think the follow syntax is so much elegant and great to read:

{{sum x=1, y=2}}

And maybe this discussion don't fit here, 'cause this is more about mustache specs then about pystache implementation or something like that.

rluders avatar May 05 '14 18:05 rluders

:+1: for proposing this on the Mustache spec repo!

It would (in some ways) bring Mustache close to Web Component style stuff as seen in Ember.js: http://emberjs.com/guides/components/defining-a-component/

Which look like this:

{{blog-post title=title}}

http://emberjs.com/guides/components/passing-properties-to-a-component/

Look familiar? :smile:

Ping me on the issue you file, @rluders ! Would love to track it.

BigBlueHat avatar May 06 '14 15:05 BigBlueHat

Ember just use handlebars.js, which is itself a superset of mustache. There are ports of handlebars to python (pybars for example) that should work with your existing templates.

The fact that mustache forces you to deal with these sorts of complicated transformations before hitting the template layer is a massive plus for me. I'd argue that you would be better to work your data into a shape that better suits your templates with a presenter or something.

Theres plenty of room for both approaches. But I think this sort of sugar would run counter to the goals of mustache.

carlmw avatar May 06 '14 15:05 carlmw

Good call @carlmw. You're likely right. They feel "logicless" (as the logic happens elsewhere---sort of a partial + params), but...you may be right.

Likely it's best to move the conversation to the spec repo and get the wider implementers involved. Though...this may not be the first time this was asked. :-P Who knows. :)

BigBlueHat avatar May 06 '14 15:05 BigBlueHat

@carlmw nice! I read about pybars, but the only problem is the fact that the 'handlebar' implementation for 'helpers' are not supported on mustache specs. So if I try to use my mustache templates with any other parser I'll be in a bad situation.

I don't think as multiple parameters as a logic stuff, it's just a 'helper' to simplify some things that sometimes we have to do. I keep try to make all my logic on python server, but sometimes looks better if I could have some helpers on template site.

I'll move this conversation to mustache specs, I think its better to us put all this thoughts there.

rluders avatar May 06 '14 15:05 rluders

@carlmw : I agree with your approach for staying to pure mustache.

However, (adding you in @rluders ) pybars has an issue, it is LGPL.

I would be ok if someone branched / forked pystache to started adding handlebars and handlebars helpers support. If that were to happen though, I'd like to see a mode the strictly enforces the mustache spec, since that's far more simpler and ubiquitous than hbs.

tony avatar Aug 25 '14 21:08 tony