{"id":20037981,"url":"https://github.com/perfectlysoft/perfect-mustache","last_synced_at":"2025-05-05T06:31:53.406Z","repository":{"id":63920169,"uuid":"62745768","full_name":"PerfectlySoft/Perfect-Mustache","owner":"PerfectlySoft","description":"Mustache template support for Perfect.","archived":false,"fork":false,"pushed_at":"2020-05-19T18:01:25.000Z","size":50,"stargazers_count":24,"open_issues_count":0,"forks_count":13,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-30T17:17:22.075Z","etag":null,"topics":["mustache","mustache-templates","perfect","server-side-swift","swift"],"latest_commit_sha":null,"homepage":"https://www.perfect.org","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PerfectlySoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-06T18:54:10.000Z","updated_at":"2022-10-28T08:55:17.000Z","dependencies_parsed_at":"2022-11-29T09:50:40.656Z","dependency_job_id":null,"html_url":"https://github.com/PerfectlySoft/Perfect-Mustache","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Mustache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Mustache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Mustache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Mustache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PerfectlySoft","download_url":"https://codeload.github.com/PerfectlySoft/Perfect-Mustache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224429203,"owners_count":17309667,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mustache","mustache-templates","perfect","server-side-swift","swift"],"created_at":"2024-11-13T10:24:30.758Z","updated_at":"2024-11-13T10:24:31.489Z","avatar_url":"https://github.com/PerfectlySoft.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Perfect-Mustache\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"http://perfect.org/get-involved.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.org/assets/github/perfect_github_2_0_0.jpg\" alt=\"Get Involed with Perfect!\" width=\"854\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/PerfectlySoft/Perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_1_Star.jpg\" alt=\"Star Perfect On Github\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://stackoverflow.com/questions/tagged/perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/perfect_gh_button_2_SO.jpg\" alt=\"Stack Overflow\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"https://twitter.com/perfectlysoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_3_twit.jpg\" alt=\"Follow Perfect on Twitter\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_4_slack.jpg\" alt=\"Join the Perfect Slack\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Swift-4.1-orange.svg?style=flat\" alt=\"Swift 4.1\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-lightgray.svg?style=flat\" alt=\"Platforms OS X | Linux\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.org/licensing.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat\" alt=\"License Apache\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://twitter.com/PerfectlySoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Twitter-@PerfectlySoft-blue.svg?style=flat\" alt=\"PerfectlySoft Twitter\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.ly/badge.svg\" alt=\"Slack Status\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\nMustache template support for Perfect.\n\nThis package is designed to work along with [Perfect](https://github.com/PerfectlySoft/Perfect). It provides Mustache template support for your server.\n\n## Quick Start\n\nTo start, add this project as a dependency in your Package.swift file.\n\n```swift\n.package(url: \"https://github.com/PerfectlySoft/Perfect-Mustache.git\", from: \"3.0.0\")\n```\n\nThen, add \"PerfectMustache\" to the list of dependencies for the target that will use PerfectMustache.\n\nExample target configuration:\n\n```swift\ntargets: [.target(name: \"PerfectTemplate\", dependencies: [\"PerfectHTTPServer\", \"PerfectMustache\"])]\n```\n\n\nBasic usage:\n\n```swift\nlet map: [String:Any] = [\"fullName\":fullName, \"uri\":uri, \"authToken\":authToken]\nlet ctx = MustacheEvaluationContext(templatePath: emailTemplate, map: map)\nlet result = try ctx.formulateResponse(withCollector: MustacheEvaluationOutputCollector())\n```\n\nThe following snippet illustrates how to use mustache templates in your URL handler. In this example, the template named \"test.html\" would be located in your server's web root directory.\n\n```swift\n{\n\trequest, response in \n\tlet webRoot = request.documentRoot\n\tmustacheRequest(request: request, response: response, handler: TestHandler(), templatePath: webRoot + \"/test.html\")\n}\n```\n\nThe template page handler, which you would impliment, might look like the following.\n\n```swift\nstruct TestHandler: MustachePageHandler { // all template handlers must inherit from PageHandler\n\t// This is the function which all handlers must impliment.\n\t// It is called by the system to allow the handler to return the set of values which will be used when populating the template.\n\t// - parameter context: The MustacheWebEvaluationContext which provides access to the HTTPRequest containing all the information pertaining to the request\n\t// - parameter collector: The MustacheEvaluationOutputCollector which can be used to adjust the template output. For example a `defaultEncodingFunc` could be installed to change how outgoing values are encoded.\n\tfunc extendValuesForResponse(context contxt: MustacheWebEvaluationContext, collector: MustacheEvaluationOutputCollector) {\n\t\tvar values = MustacheEvaluationContext.MapType()\n\t\tvalues[\"value\"] = \"hello\"\n\t\t/// etc.\n\t\tcontxt.extendValues(with: values)\n\t\tdo {\n\t\t\ttry contxt.requestCompleted(withCollector: collector)\n\t\t} catch {\n\t\t\tlet response = contxt.webResponse\n\t\t\tresponse.status = .internalServerError\n\t\t\tresponse.appendBody(string: \"\\(error)\")\n\t\t\tresponse.completed()\n\t\t}\n\t}\n}\n```\n\nLook at the [UploadEnumerator](https://github.com/PerfectExamples/Perfect-UploadEnumerator) example for a more concrete example.\n\n**Tag Support**\n\nThis mustache template processor supports:\n\n* {{regularTags}}\n* {{{unencodedTags}}}\n* {{\u0026 unescapedTags}}\n* {{# sections}} ... {{/sections}}\n* {{^ invertedSections}} ... {{/invertedSections}}\n* {{! comments}}\n* {{\u003e partials}}\n* lambdas\n\n**Partials**\n\nAll files used for partials must be located in the same directory as the calling template. Additionally, all partial files *must* have the file extension of **mustache** but this extension must not be included in the partial tag itself. For example, to include the contents of the file *foo.mustache* you would use the tag ```{{\u003e foo }}```.\n\n**Encoding**\n\nBy default, all encoded tags (i.e. regular tags) are HTML encoded and \u0026lt; \u0026amp; \u0026gt; entities will be escaped. In your handler you can manually set the ```MustacheEvaluationOutputCollector.defaultEncodingFunc``` function to perform whatever encoding you need. For example when outputting JSON data you would want to set this function to something like the following:\n\n```swift\ncollector.defaultEncodingFunc = { \n\tstring in \n\treturn (try? string.jsonEncodedString()) ?? \"bad string\"\n}\n```\n\n**Lambdas**\n\nFunctions can be added to the values dictionary. These will be executed and the results will be added to the template output. Such functions should have the following signature:\n\n```swift\n(tag: String, context: MustacheEvaluationContext) -\u003e String\n```\n\nThe ```tag``` parameter will be the tag name. For example the tag {{name}} would give you the value \"name\" for the tag parameter.\n\n## Further Documentation\nFor more information, please visit [perfect.org](http://www.perfect.org/docs/mustache.html).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-mustache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperfectlysoft%2Fperfect-mustache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-mustache/lists"}