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

Query template for delimited value #47

Closed KosmicTask closed 11 years ago

KosmicTask commented 11 years ago

Is it possible using the existing API to query a template as to whether it contains a particular key value, say {{ my-header }}?

My usage case is: if a certain key is present in the template then the GUI presents options to configure the content for that key. If the key is absent then the GUI disables the options.

Obviously I can scan/regex the template string externally but it would need to take into account the current delimiter and be able to deal with filters.

groue commented 11 years ago

Hi Jonathan.

If you look for an identifier, such as my-header, then rendering a template with a KVC-recorder object (an object whose valueForKey: method returns itself for any given key, and logs the requested keys) should do the trick. Not very efficient, I agree, plus it would require extra work if you look for keys into sections triggered by false values.

A definitive general solution to your request would be to expose the template syntax tree in the public API. However your recent #45 et #46 issues will have me tweak both parser and compiler: I'm happy the AST headers are still private, because I'm free to break any internal guts today :-)

I can explain you how to use the current private headers, and look for the my-header key, if the first solution does not fit your needs.

KosmicTask commented 11 years ago

HI Gwendal

I can work with the first method.

As ever, thanks for the reply. Good luck with the surgery on GRMustache!

groue commented 11 years ago

Thanks ! I'm not a parser professional, and Mustache parsing has quirks that do not help, such as the {{=<% %>=}} "set delimiters tag" that allows to change Mustache delimiters in the middle of a template: {{name}} {{=<% %>=}} <% name %>. And the white space rules are bitches. So I'll turn a low-quality parser to another low-quality parser, I'm afraid :)