groue / GRMustache

Flexible and production-ready Mustache templates for MacOS Cocoa and iOS
http://mustache.github.com/
MIT License
1.44k stars 190 forks source link

Render if condition is true #81

Closed NightFox7 closed 10 years ago

NightFox7 commented 10 years ago

How can I render something only if if a certain variable verifies a certain condition For example

{{id==5}}
  id is equal to five.
{{/}}
groue commented 10 years ago

Hi Aymen,

Mustache (as defined by http://mustache.github.io/mustache.5.html - go read it if your haven't yet) has no support for literals. There is no way to embed strings, numbers, booleans, etc. in templates.

Mustache also has no support for operators. There is no such thing as ==, <, etc.

As I stated at http://stackoverflow.com/questions/25213143/grmustache-pass-constant-values-in-filter:

There is no support for string literals in the library.

You may consider those alternate solutions:

  • naming your constants: {{# isEqual(item, ThatValue) }}
  • declaring template-targeted properties: {{# item.isThatValue }}
  • using a template engine which supports string literals, such as handlebars-objc.

You may also enjoy the ViewModel Guide. It describes various techniques to feed templates.

NightFox7 commented 10 years ago

Thanks @groue for your quick reply. I think I will go with the IsEqual filter

groue commented 10 years ago

Great! Happy Mustache!