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

Feature Request: Variable Defaults #64

Closed
spudly opened this issue Jan 15, 2013 · 9 comments
Closed

Feature Request: Variable Defaults #64

spudly opened this issue Jan 15, 2013 · 9 comments

Comments

@spudly
Copy link

spudly commented Jan 15, 2013

Right now if you tell mustache to output a variable, it defaults to empty string if the variable has no value. I'd like to be able to specify a default string to output in this case. For example:

Data:

{
  people: [
    {name: "Bob", fav_food: "Pizza"},
    {name: "Joe", fav_color: "Orange"}
  ]
}

With the current spec, I would have to write this:

{{#people}}
  <h1>{{name}}</h1>
  <dl>
    <dt>Favorite Color:</dt><dd>{{#fav_color}}{{fav_color}}{{/fav_color}}{{^fav_color}}None{{/fav_color}}</dd>
    <dt>Favorite Food:</dt><dd>{{#fav_food}}{{fav_food}}{{/fav_food}}{{^fav_food}}None{{/fav_food}}</dd>
  </dl>
{{/people}}

If some sort of default syntax were added, I could write this instead:

{{#people}}
  <h1>{{name}}</h1>
  <dl>
    <dt>Favorite Color:</dt><dd>{{fav_color || "None"}}</dd>
    <dt>Favorite Food:</dt><dd>{{fav_food || "None"}}</dd>
  </dl>
{{/people}}
@groue
Copy link

groue commented Jan 15, 2013

Sure. Check the template delegate guide, linked from the README. It covers exactly this use case.

@spudly
Copy link
Author

spudly commented Jan 15, 2013

Maybe I'm missing something but I wasn't able to find it. Are you referring to the mustache spec README.md?

@groue
Copy link

groue commented Jan 15, 2013

Nope. Sorry I'm on a mobile phone and copy pasting a link is not so easy : look for "tag delegate" in the GRMustache home page on github. Follow the link. There is the solution to your issue.

Tell me if it works as I expect.

@groue
Copy link

groue commented Jan 15, 2013

Oops, sorry, wrong repo. My mistake. Mustache has no such feature. Some implementations do, such as GRMustache. I advise you to check with your current Mustache implementation.

@spudly
Copy link
Author

spudly commented Jan 16, 2013

Unfortunately, the templates I write need to work under multiple implementations, so I can't rely on implementation-specific code.

@groue
Copy link

groue commented Jan 17, 2013

Yes. But you can open issues in each of these implementation repositories.

Before making the exact same suggestion, please open your eyes, and see that your suggestion:

  • implies parsing of literal values
  • implies, for consistency's sake, that {{ "Hello" }} would render "Hello"
  • arbitrarily, for no good reason whatsoever, limits default values to parsable literals (string, numbers, maybe boolean, but certainly not full objects or dates for example)

Some implementations provide with the feature you are looking for in a much more general way. Please, please, please, don't suggest JS/PHP implementations to implement your || syntax.

@spudly
Copy link
Author

spudly commented Jan 17, 2013

I understand your concerns. If mustache doesn't currently parse literal values this would indeed be a much larger undertaking than simply adding a new operator. The || syntax I suggested may not be the best way to implement this. I'm certainly open to doing it in other ways, perhaps as a callback function that could generate a default value. Or maybe if the spec had a filter syntax as is being discussed in #41 no changes would be necessary as you could use a filter to do it ({{foo | defaultToNone}}).

@groue
Copy link

groue commented Jan 17, 2013

Yes @spudly, I agree filters could do that as well.

In GRMustache, you have two ways to provide default values:

  • use the "tag delegate" pattern I was talking above. This is a mechanism that invokes a callback each time a value will get rendered. The callback has the opportunity to confirm or alter the value - including returning something instead of null.
  • define and use filters: {{ defaultToNone(foo) }}. GRMustache filters are functions that can return any value (not only text like pipe-filters), and can also fill sections, so that you can for instance render array indexes {{# withPosition(items) }} {{ position }}: {{ name }} {{ / }}.

This issue is typical of an use case that does not deserve a specific solution. It needs (#41), or has (GRMustache delegate and filters) more general mechanisms that include a solution for it.

@spudly
Copy link
Author

spudly commented Jan 17, 2013

Closing in favor of #41.

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

2 participants