GBTemplateHandler Class Reference
Inherits from | NSObject |
Declared in | GBTemplateHandler.h GBTemplateHandler.m |
Overview
Loads a template file, prepares it for output generation and renders the output on any given object.
The main responsibilities of this class are loading template from file to string, parsing and extractng all template sections and rendering the template for any given object. As the first two tasks are actually combined into a single method, public API is even simpler: just create one instance of the class for each different template by sending parseTemplateFromPath:error:
or parseTemplate:error:
which loads, parses and verifies the template. Then send renderObject:
to generate output from a concrete object.
Note that, as said above, there is no need to create a new GBTemplateHandler
instance for each object for which we want to render output. It's enough and much more efficient to create a single instance for each different type of template and use it to generate as many objects from that template as needed.
Tasks
Initialization & disposal
-
+ handler
Returns a new autoreleasedGBTemplateHandler
.
Parsing
-
– parseTemplateFromPath:error:
Parses template file from the given path and converts it into a ready-to-use template. -
– parseTemplate:error:
Parses the given template string and converts it into a ready-to-use template.
Rendering
-
– renderObject:
Renders the given object using current template data.
Instance Methods
parseTemplate:error:
Parses the given template string and converts it into a ready-to-use template.
- (BOOL)parseTemplate:(NSString *)string error:(NSError **)error
Parameters
- string
- The template string to parse.
- error
- If parsing fails, error message is returned here.
Return Value
Returns YES
if parsing was sucesful, NO
otherwise.
Discussion
The results are parsed into templateString
and templateSections
. After reading the template from the file, you can re-use the class for generating all objects that need to be templated from read template. That is, there's no need of re-parsing the same template for each generation.
Declared In
GBTemplateHandler.h
parseTemplateFromPath:error:
Parses template file from the given path and converts it into a ready-to-use template.
- (BOOL)parseTemplateFromPath:(NSString *)path error:(NSError **)error
Parameters
- path
- The name of the template file to parse.
- error
- If reading or parsing fails, error message is returned here.
Return Value
Returns YES
if parsing was sucesful, NO
otherwise.
Discussion
The results are parsed into templateString
and templateSections
. After reading the template from the file, you can re-use the class for generating all objects that need to be templated from read template. That is, there's no need of re-parsing the same template for each generation.
See Also
Declared In
GBTemplateHandler.h
renderObject:
Renders the given object using current template data.
- (NSString *)renderObject:(id)object
Parameters
- object
- The object containins data to be replaced by template placeholders.
Return Value
Returns generated output string.
Discussion
This is where template placeholders get replaced with actual values from the given object and as thus the main focus of the GBTemplateHandler
class. The object must contain all expected variables as defined by the template. Failing to provide required values will result in that part of the template being ignored, but may also result in unpredicted behavior, so it's better to make sure proper objects are passed to proper templates.
Important: Note that this message can only be sent after parsing template with one of the parsing methods! Sending the message to a class with no parsed data results in a warning and empty string being returned.
Declared In
GBTemplateHandler.h