Module: Perfect-Mustache
enum MustacheError : Error
This enum type represents the parsing and the runtime evaluation exceptions which may be generated.
case syntaxError(String)
The mustache template was malformed.
case evaluationError(String)
An exception occurred while evaluating the template.
protocol MustachePageHandler
A mustache handler, which should be passed to `mustacheRequest`, generates values to fill a mustache template.
Call `context.extendValues(with: values)` one or more times and then
`context.requestCompleted(withCollector collector)` to complete the request and output the resulting content to the client.
func mustacheRequest(request req: HTTPRequest, response: HTTPResponse, handler: MustachePageHandler, templatePath: String)
Convenience function to begin a mustache template request

```swift
routes.add(method: .get, uri: "/", handler: {
request, response in
mustacheRequest(request: request, response: response, handler: UploadHandler(), path: webRoot + "/index.mustache")
})
```
class MustacheEvaluationContext
This class represents an individual scope for mustache template values.
A mustache template handler will return a `MustacheEvaluationContext.MapType` object as a result from its `PageHandler.valuesForResponse` function.
var templatePath: String?
Complete path to the file being processed
var templateContent: String?
Mustache content for dynamic generation
var templateName: String?
Returns the name of the current template file.
func formulateResponse(withCollector collector: MustacheEvaluationOutputCollector) throws -> String
All the template values have been completed and resulting content should be
formulated and returned.
func getValue(named nam: String) -> MapType.Value?
Search for a value starting from the current context. If not found in the current context, the parent context will be searched, etc.
- parameter named: The name of the value to find
- returns: The value, if found, or nil
func extendValues(with wit: MapType)
Extends the current values with those from the parameter.
- parameter with: The new values to add
class MustacheWebEvaluationContext: MustacheEvaluationContext
This class represents an individual scope for mustache template values.
A mustache template handler will return a `MustacheWebEvaluationContext.MapType` object as a result from its `PageHandler.valuesForResponse` function.
var webResponse: HTTPResponse
Provides access to the current HTTPResponse object.
var webRequest: HTTPRequest
Provides access to the current HTTPRequest object.
func requestCompleted(withCollector collector: MustacheEvaluationOutputCollector) throws
All the template values have been completed and resulting content should be
formulated and returned to the client.
class MustacheEvaluationOutputCollector
An instance of this class will collect all output data generated by mustache tags during evaluation.
Call the `asString()` function to retreive the resulting data.
func init()
Empty public initializer.
func append(_ s: String, encoded: Bool = true) -> MustacheEvaluationOutputCollector
Append a new string value to the collected output.
- parameter s: The string value which will be appended.
- parameter encoded: If true, the string value will be HTML encoded as it is appended. Defaults to true.
func asString() -> String
Joins all the collected output into one string and returns this value.
class MustacheTag
An individual mustache tag or plain-text section.
func description() -> String
Reconstitutes the tag into its original source string form.
- returns: The resulting string, including the original delimiters and tag-type marker.
class MustachePartialTag : MustacheTag
A sub-class of MustacheTag which represents a mustache "partial" tag.
class MustachePragmaTag : MustacheTag
A sub-class of MustacheTag which represents a pragma tag.
Pragma tags are "meta" tags which influence template evaluation but likely do not output any data.
func parsePragma() -> Dictionary<String, String>
Parse the pragma. Pragmas should be in the format: A:B,C:D,E,F:G.
- returns: A Dictionary containing the pragma names and values.
class MustacheGroupTag : MustacheTag
A sub-class of MustacheTag which represents a group of child tags.
func override public func description() -> String
Returns a String containing the reconstituted tag, including all children.
class MustacheTemplate : MustacheGroupTag
This class represents a mustache template which has been parsed and is ready to evaluate.
It contains a series of "out of band" pragmas which can influence the evaluation, and a
series of children which constitute the body of the template itself.
func evaluatePragmas(context contxt: MustacheEvaluationContext, collector: MustacheEvaluationOutputCollector)
Evaluate any pragmas which were found in the template. These pragmas may alter the given `MustacheEvaluationContext` parameter.
- parameter context: The `MustacheEvaluationContext` object which will be used to further evaluate the template.
- parameter collector: The `MustacheEvaluationOutputCollector` object which will collect all output from the template evaluation.
func override public func evaluate(context contxt: MustacheEvaluationContext, collector: MustacheEvaluationOutputCollector)
Evaluate the template using the given context and output collector.
- parameter context: The `MustacheEvaluationContext` object which holds the values used for evaluating the template.
- parameter collector: The `MustacheEvaluationOutputCollector` object which will collect all output from the template evaluation. `MustacheEvaluationOutputCollector.asString()` can be called to retreive the resulting output data.
func override public func description() -> String
Returns a String containing the reconstituted template, including all children.
class MustacheParser
This object will parse templates written in the mustache markup language.
Calling `parse` with the path to a file will return the resulting parsed and ready to evaluate template.
func init()
Empty public initializer.
func parse(string strng: String) throws -> MustacheTemplate
Parses a string containing mustache markup and returns the `MustacheTemplate` object.
- throws: `MustacheError.SyntaxError`
- returns: A `MustacheTemplate` object which can be evaluated.