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

Template Helpers #160

Open
rluders opened this issue Mar 28, 2014 · 11 comments
Open

Template Helpers #160

rluders opened this issue Mar 28, 2014 · 11 comments

Comments

@rluders
Copy link

rluders commented Mar 28, 2014

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!

@BigBlueHat
Copy link

@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!

@rluders
Copy link
Author

rluders commented May 2, 2014

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.

@kevincarrogan
Copy link
Collaborator

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)

@BigBlueHat
Copy link

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
Copy link

@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.

@rluders
Copy link
Author

rluders commented May 5, 2014

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.

@BigBlueHat
Copy link

👍 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? 😄

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

@carlmw
Copy link
Collaborator

carlmw commented May 6, 2014

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.

@BigBlueHat
Copy link

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. :)

@rluders
Copy link
Author

rluders commented May 6, 2014

@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.

@tony
Copy link

tony commented Aug 25, 2014

@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.

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

5 participants