{"id":13737164,"url":"https://github.com/soasme/nim-mustache","last_synced_at":"2025-04-01T10:58:57.246Z","repository":{"id":48073511,"uuid":"208428639","full_name":"soasme/nim-mustache","owner":"soasme","description":"Mustache in Nim","archived":false,"fork":false,"pushed_at":"2023-03-19T19:53:59.000Z","size":161,"stargazers_count":64,"open_issues_count":2,"forks_count":6,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-15T05:32:19.957Z","etag":null,"topics":["mustache","mustache-implementations","nim","template","template-engine","template-library"],"latest_commit_sha":null,"homepage":"https://mustache.github.io/","language":"Nim","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soasme.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":["soasme"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2019-09-14T11:09:19.000Z","updated_at":"2024-09-12T13:19:51.000Z","dependencies_parsed_at":"2024-01-06T12:04:06.386Z","dependency_job_id":"d7ea5aeb-d769-4dd4-9048-1ca58d32a11c","html_url":"https://github.com/soasme/nim-mustache","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soasme%2Fnim-mustache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soasme%2Fnim-mustache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soasme%2Fnim-mustache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soasme%2Fnim-mustache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soasme","download_url":"https://codeload.github.com/soasme/nim-mustache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246628227,"owners_count":20808106,"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-implementations","nim","template","template-engine","template-library"],"created_at":"2024-08-03T03:01:36.548Z","updated_at":"2025-04-01T10:58:57.237Z","avatar_url":"https://github.com/soasme.png","language":"Nim","funding_links":["https://github.com/sponsors/soasme"],"categories":["Web"],"sub_categories":["Template Engines"],"readme":"# Nim-mustache\n\n[Mustache](https://mustache.github.io/mustache.1.html) in [Nim](https://nim-lang.org).\n\nNim-mustache is a Nim implementation of Mustache.\nMustache is a logic-less templating system inspired by ctemplate and et.\nMustache \"emphasizes separating logic from presentation: it is impossible to embed application logic in this template language.\"\nNim-mustache is a full implementation of mustache [spec] v1.2.1.\n\n![Build Status](https://travis-ci.org/soasme/nim-mustache.svg?branch=master)\n\n## Status\n\nNim-mustache is stable now. Welcome to contribute, comment, and report issues. ❤️\n\n## Features\n\n- ✨ Support `Static Text`.\n- ✨ Support `{{Variables}}`.\n- ✨ Support `{{# Section }} {{/ Section }}`.\n- ✨ Support `{{^ InvertedSection }} {{/ InvertedSection }}`.\n- ✨ Support `{{! Comments }}`.\n- ✨ Support `{{=\u003c% %\u003e=}} \u003c% SetDelimiter %\u003e`.\n- ✨ Support `{{\u003e Partial }}`.\n- ✨ Support Inheritance `{{\u003c Parent}}{{$ foo}}Substitute{{/ foo}}{{/ Parent}}`.\n- ✨ Support Lambda.\n- ✨ Passed all mustache specs.\n\n## Getting Started\n\nNim-mustache requires Nim \u003e= 1.0.0.\n\n```bash\n$ nimble install mustache\n```\n\n## Usage\n\n```nim\n# Step 1.\nimport mustache, tables\n\n# Step 2.\nvar c = newContext()\nc[\"i\"] = 1\nc[\"f\"] = 1.0\nc[\"s\"] = \"hello world\"\nc[\"a\"] = @[{\"k\": \"v\"}.toTable]\nc[\"t\"] = {\"k\": \"v\"}.toTable\nc[\"l\"] = proc(s: string, c: Context): string = \"\u003cb\u003e\" \u0026 s.render(c) \u0026 \"\u003c/b\u003e\"\n\n# Step 3.\nlet s = \"\"\"\n{{i}} {{f}} {{s}}\n{{#a}}\n  {{k}}\n{{/a}}\n\n{{#t}}\n  {{k}}\n{{/t}}\n\n{{#l}}\n  {{s}}\n{{/l}}\n\"\"\"\necho(s.render(c))\n```\n\n## Advanced Usage\n\n### Set Arbitrary Objects in Context\n\nConsider you have your own object `Stock`.\n\n```nim\ntype Stock = object\n  name*: string\n  price*: int\n\nlet stock = Stock(name: \"NIM\", price: 1000)\n```\n\nIt would be convenient if you can set it to context:\n\n```nim\nlet c = newContext()\nc[\"stock\"] = stock\nlet s = \"{{#stock}}{{name}}: {{price}}{{/stock}}\"\necho(s.render(c))\n```\n\nThe trick is to define a `castValue` proc for your type.\nBelow is an example of how to define a `castValue` for `Stock`.\n\n```nim\nproc castValue(value: Stock): Value =\n  let newValue = new(Table[string, Value])\n  result = Value(kind: vkTable, vTable: newValue)\n  newValue[\"name\"] = value.name.castValue\n  newValue[\"price\"] = value.price.castValue\n```\n\n### Change Partials Searching Dir\n\nBy default, partials are located in `cwd`.\n\n```\n$ ls\nmain.mustache    partial.mustache\n$ cat main.mustache\n{{\u003e partial}}\n```\n\nBut what if mustache files are in other locations?\n\n```\n$ pwd\n/tmp/path/to\n$ ls -R\nmain.mustache\ntemplates/partial.mustache\n```\n\nIn this case, You can specify other searching dirs:\n\n```nim\nlet c = newContext(searchDirs=@[\"/path/to/templates\", \"./\"])\nlet s = readFile(\"main.mustache\")\necho(s.render(c))\n```\n\n### Read Partials From Memory\n\nYou can also read mustache partials from an in-memory table:\n\n```nim\nimport tables\nlet partials = {\n  \"something\": \"This is something\"\n}.toTable()\nlet c = newContext(partials=partials)\nlet s = \"something: {{\u003e something}}\"\necho(s.render(c))\n```\n\n### Read Partials From Multiple Sources\n\nYou can read mustache partials from both directory and in-memory table in different orders.\nThe first source that have the partial key wins.\n\n\n```nim\nimport tables\n\nlet partials1 = {\n  \"something\": \"This is something\"\n}.toTable()\n\nlet partials2 = {\n  \"something\": \"This is something else\"\n}.toTable()\nlet c = newContext()\nc.searchTable(partials1)\nc.searchDirs([\"/path/to/partials\"])\nc.searchTable(partials2)\nlet s = \"result: {{\u003e something}}\"\necho(s.render(c)) # result: This is something\n```\n\n### Use Mustache With Jester\n\nIt's recommended using Mustache with Jester, a sinatra-like web framework for Nim, when writing a web application.\n\nImagine you have a template file named `hello.mustache`. Rendering the template can be as simple as calling it in a route.\n\n```nim\nimport jester\nimport mustache\n\nroutes:\n  get \"/hello/@name\":\n    let c = newContext()\n    c[\"name\"] = @\"name\"\n    resp \"{{ \u003ehello }}\".render(c)\n```\n\nIn `hello.mustache`, it's just mustache template content.\n\n```mustache\n\u003chtml\u003e\n  \u003cbody\u003e\n    \u003ch1\u003eHello, {{ name }}\u003c/h1\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\nIf you have a dedicated directory template directory, you can specify it as:\n\n```\nlet c = newContext(searchDirs = @[\"/path/to/templates\"])\n```\n\n\n## Develop\n\nBuild the binary.\n\n```\n$ nimble build\n```\n\nRun test cases.\n\n```bash\n$ nimble test\n```\n\n## Changelog\n\n* v0.4.3, 9 Aug 2021, export searchDirs \u0026 searchTable.\n* v0.4.2, 10 Jun 2021, support casting types float32, float64, int8, int16, int32, int64.\n* v0.4.1, 16 May 2021, support mustache inheritance tag.\n* v0.4.0, 8 May 2021, build on github action \u0026\u0026 support mustache spec v1.2.1.\n* v0.3.3, 7 May 2021, add optional parameter `values` for `newContext` \u0026 support JsonNode as a value.\n* v0.3.2, 25 Jan 2021, support numeric indexing from list using dot syntax.\n* v0.3.1, 24 Jan 2021, fix version issue.\n* v0.3.0, 2 Dec 2020, support loading partials from multiple in-memory tables/directories.\n* v0.2.2, 1 Dec 2020, support in-memory table for partial resolving.\n* v0.2.1, 20 Sep 2019, fix locks level warnings.\n* v0.2.0, 20 Sep 2019, support setting arbitrary objects in context.\n* v0.1.0, 19 Sep 2019, initial release.\n\n## Alternatives\n\n* [moustachu](https://github.com/fenekku/moustachu). Moustachu doesn't implement some mustache features, such as lambda, set delimiters, while Nim-mustache supports all mustache features.\n\n## References\n\n* [spec], (see also [a document](https://pietroppeter.github.io/nblog/drafts/mustache_specs.html) with the results of running the specs using nim-mustache)\n* [syntax], (see also [a document](https://pietroppeter.github.io/nimib/mostaccio.html) reproducing the syntax using nim-mustache)\n\n\n[spec]: https://github.com/mustache/spec\n[syntax]: http://mustache.github.com/mustache.5.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoasme%2Fnim-mustache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoasme%2Fnim-mustache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoasme%2Fnim-mustache/lists"}