Skip to content

Latest commit

 

History

History
131 lines (73 loc) · 6.29 KB

README.md

File metadata and controls

131 lines (73 loc) · 6.29 KB

GRMustache

GRMustache is a production-ready implementation of Mustache templates for MacOS Cocoa and iOS.

August 6th, 2012: GRMustache 4.3.2 is out. Release notes

Breaking news on Twitter: http://twitter.com/GRMustache

How To

1. Download and add to your Xcode project

$ git clone https://github.com/groue/GRMustache.git
  • For MacOS development, add include/GRMustache.h and lib/libGRMustache4-MacOS.a to your project.
  • For iOS development, add include/GRMustache.h and lib/libGRMustache4-iOS.a to your project.

Alternatively, you may use CocoaPods: append dependency 'GRMustache' to your Podfile.

GRMustache targets MacOS down to 10.6 Snow Leopard, iOS down to version 3, and only depends on the Foundation framework.

2. Import "GRMustache.h" and start rendering templates

#import "GRMustache.h"

// Renders "Hello Arthur!"
NSString *rendering = [GRMustacheTemplate renderObject:[Person personWithName:@"Arthur"]
                                            fromString:@"Hello {{name}}!"
                                                 error:NULL];

// Renders from a resource
NSString *rendering = [GRMustacheTemplate renderObject:[Person personWithName:@"Arthur"]
                                          fromResource:@"Profile"  // loads `Profile.mustache`
                                                bundle:nil
                                                 error:NULL];

Documentation

Mustache syntax

Guides

Starting point:

Basic Mustache:

  • templates.md: how to load, parse, and render templates from various sources.
  • runtime.md: how to provide data to templates.

Advanced Mustache:

  • helpers.md: how to process the template canvas before it is rendered with Mustache "lambda sections".
  • filters.md: how to process data before it is rendered with "filters".
  • delegate.md: how to hook into template rendering.

Sample code:

  • sample_code: because some tasks are easier to do with some guidelines.

Reference

  • Reference: the GRMustache reference, automatically generated from inline documentation, for fun and profit, by appledoc.

FAQ

  • Q: How do I render array indexes?

    A: Check Guides/sample_code/indexes.md

  • Q: How do I format numbers and dates, or localize portions of templates, etc?

    A: Filters and Mustache lambda sections are your friends. Filters act on the data you provide to the template, and are typically a good match for formatting values. Lambda sections act directly on the template canvas, and can help you process a full portion of a template.

  • Q: Does GRMustache provide any layout facility?

    A: No. But there is a sample Xcode project that demonstrates how to do that.

  • Q: How do I render default values for missing keys?

    A: This can be done by providing your template a delegate: check Guides/delegate.md.

  • Q: I have a bunch of templates and partials that live in memory / a database / the cloud / wherever.

    A: Check Guides/template_repositories.md.

  • Q: What is this NSUndefinedKeyException stuff?

    A: When GRMustache has to try several objects until it finds the one that provides a {{key}}, several NSUndefinedKeyException are raised and caught. Let us double guess you: it's likely that you wish Xcode would stop breaking on those exceptions. This use case is covered in Guides/runtime/context_stack.md.

  • Q: Why does GRMustache need JRSwizzle?

    A: GRMustache will swizzle the implementation of valueForUndefinedKey: in the NSObject and NSManagedObject classes when you invoke [GRMustache preventNSUndefinedKeyExceptionAttack]. This use case is covered in Guides/runtime/context_stack.md. The dreadful swizzling happens in src/classes/GRMustacheNSUndefinedKeyExceptionGuard.m.

Contribution wish-list

I wish somebody would review my non-native English, and clean up the guides, if you ask.

Forking

Please fork. You'll learn useful information in Guides/forking.md.

License

Released under the MIT License

Copyright (c) 2012 Gwendal Roué

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.