Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix failing tests

  • Loading branch information...
commit 0b67eb85288c48fe495ca732dfcaffd99811f407 1 parent dff1359
@groue authored
Showing with 53 additions and 59 deletions.
  1. +53 −59 src/classes/GRMustacheSectionElement.m
View
112 src/classes/GRMustacheSectionElement.m
@@ -41,6 +41,11 @@ @interface GRMustacheSectionElement()
* @see +[GRMustacheSectionElement sectionElementWithExpression:templateRepository:templateString:innerRange:inverted:overridable:innerElements:]
*/
- (id)initWithExpression:(GRMustacheExpression *)expression templateRepository:(GRMustacheTemplateRepository *)templateRepository templateString:(NSString *)templateString innerRange:(NSRange)innerRange inverted:(BOOL)inverted overridable:(BOOL)overridable innerElements:(NSArray *)innerElements;
+
+/**
+ * Check object type, and render it as a section tag helper or a plain object.
+ */
+- (void)renderObject:(id)object inBuffer:(NSMutableString *)buffer withRuntime:(GRMustacheRuntime *)runtime;
@end
@@ -82,47 +87,27 @@ - (void)renderInBuffer:(NSMutableString *)buffer withRuntime:(GRMustacheRuntime
id value = [_expression evaluateInRuntime:runtime asFilterValue:NO];
[runtime delegateValue:value interpretation:GRMustacheSectionTagInterpretation forRenderingToken:_expression.token usingBlock:^(id value) {
- GRMustacheRuntime *sectionRuntime = runtime;
-
- // Values that conform to the GRMustacheTemplateDelegate protocol
- // enter the runtime.
-
- if ([value conformsToProtocol:@protocol(GRMustacheTemplateDelegate)]) {
- sectionRuntime = [runtime runtimeByAddingTemplateDelegate:(id<GRMustacheTemplateDelegate>)value];
- }
-
-
// Interpret value
if (value == nil)
{
// Missing value
if (_inverted || _overridable) {
- [self renderInnerElementsInBuffer:buffer withRuntime:sectionRuntime];
+ [self renderInnerElementsInBuffer:buffer withRuntime:runtime];
}
return;
}
- else if (
- value == [NSNull null] ||
- ([value isKindOfClass:[NSNumber class]] && [((NSNumber*)value) boolValue] == NO) ||
- ([value isKindOfClass:[NSString class]] && [((NSString*)value) length] == 0))
+ else if (value == [NSNull null] ||
+ ([value isKindOfClass:[NSNumber class]] && [((NSNumber*)value) boolValue] == NO) ||
+ ([value isKindOfClass:[NSString class]] && [((NSString*)value) length] == 0))
{
// False value
if (_inverted) {
- [self renderInnerElementsInBuffer:buffer withRuntime:sectionRuntime];
+ [self renderInnerElementsInBuffer:buffer withRuntime:runtime];
}
return;
}
- else if ([value isKindOfClass:[NSDictionary class]])
- {
- // True value
- if (!_inverted) {
- sectionRuntime = [sectionRuntime runtimeByAddingContextObject:value];
- [self renderInnerElementsInBuffer:buffer withRuntime:sectionRuntime];
- }
- return;
- }
- else if ([value conformsToProtocol:@protocol(NSFastEnumeration)])
+ else if (![value isKindOfClass:[NSDictionary class]] && [value conformsToProtocol:@protocol(NSFastEnumeration)])
{
// Enumerable
if (_inverted) {
@@ -132,50 +117,21 @@ - (void)renderInBuffer:(NSMutableString *)buffer withRuntime:(GRMustacheRuntime
break;
}
if (empty) {
- [self renderInnerElementsInBuffer:buffer withRuntime:sectionRuntime];
+ [self renderInnerElementsInBuffer:buffer withRuntime:runtime];
}
} else {
for (id item in value) {
- GRMustacheRuntime *itemRuntime = [sectionRuntime runtimeByAddingContextObject:item];
- [self renderInnerElementsInBuffer:buffer withRuntime:itemRuntime];
- }
- }
- return;
- }
- else if ([value conformsToProtocol:@protocol(GRMustacheSectionTagHelper)])
- {
- // Helper
- if (!_inverted) {
- GRMustacheRuntime *helperRuntime = [sectionRuntime runtimeByAddingContextObject:value];
- GRMustacheSectionTagRenderingContext *context = [GRMustacheSectionTagRenderingContext contextWithSectionElement:self runtime:helperRuntime];
- NSString *rendering = [(id<GRMustacheSectionTagHelper>)value renderForSectionTagInContext:context];
- if (rendering) {
- [buffer appendString:rendering];
- }
- }
- return;
- }
- else if ([value conformsToProtocol:@protocol(GRMustacheSectionHelper)])
- {
- // Helper
- if (!_inverted) {
- GRMustacheRuntime *helperRuntime = [sectionRuntime runtimeByAddingContextObject:value];
- GRMustacheSection *section = [GRMustacheSection sectionWithSectionElement:self runtime:helperRuntime];
- NSString *rendering = [(id<GRMustacheSectionHelper>)value renderSection:section];
- if (rendering) {
- [buffer appendString:rendering];
+ [self renderObject:item inBuffer:buffer withRuntime:runtime];
}
}
return;
}
else
{
- // True value
+ // Other values
if (!_inverted) {
- sectionRuntime = [sectionRuntime runtimeByAddingContextObject:value];
- [self renderInnerElementsInBuffer:buffer withRuntime:sectionRuntime];
+ [self renderObject:value inBuffer:buffer withRuntime:runtime];
}
- return;
}
}];
}
@@ -213,4 +169,42 @@ - (id)initWithExpression:(GRMustacheExpression *)expression templateRepository:(
return self;
}
+- (void)renderObject:(id)object inBuffer:(NSMutableString *)buffer withRuntime:(GRMustacheRuntime *)runtime
+{
+ // Objects that conform to the GRMustacheTemplateDelegate protocol enter the runtime.
+ if ([object conformsToProtocol:@protocol(GRMustacheTemplateDelegate)]) {
+ runtime = [runtime runtimeByAddingTemplateDelegate:(id<GRMustacheTemplateDelegate>)object];
+ }
+
+ // Objects enter context stack
+ runtime = [runtime runtimeByAddingContextObject:object];
+
+ if ([object conformsToProtocol:@protocol(GRMustacheSectionTagHelper)])
+ {
+ // Helper
+
+ GRMustacheSectionTagRenderingContext *context = [GRMustacheSectionTagRenderingContext contextWithSectionElement:self runtime:runtime];
+ NSString *rendering = [(id<GRMustacheSectionTagHelper>)object renderForSectionTagInContext:context];
+ if (rendering) {
+ [buffer appendString:rendering];
+ }
+ }
+ else if ([object conformsToProtocol:@protocol(GRMustacheSectionHelper)])
+ {
+ // Deprecated Helper
+
+ GRMustacheSection *section = [GRMustacheSection sectionWithSectionElement:self runtime:runtime];
+ NSString *rendering = [(id<GRMustacheSectionHelper>)object renderSection:section];
+ if (rendering) {
+ [buffer appendString:rendering];
+ }
+ }
+ else
+ {
+ // True object
+
+ [self renderInnerElementsInBuffer:buffer withRuntime:runtime];
+ }
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.