{"id":19423737,"url":"https://github.com/xiaocongdong/tempar","last_synced_at":"2026-04-11T16:41:25.362Z","repository":{"id":119592299,"uuid":"143838611","full_name":"XiaocongDong/tempar","owner":"XiaocongDong","description":"Lightweight template parser for anything.","archived":false,"fork":false,"pushed_at":"2018-08-16T08:15:11.000Z","size":36,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-25T19:43:31.659Z","etag":null,"topics":["ast","cli","compiler","high-performance","indentation","javascript","lightweight","nodejs","parser","template"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/XiaocongDong.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-07T07:47:56.000Z","updated_at":"2024-01-05T13:09:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"a1cd8e64-c5ea-4c83-a66d-951cfdf18241","html_url":"https://github.com/XiaocongDong/tempar","commit_stats":{"total_commits":17,"total_committers":2,"mean_commits":8.5,"dds":0.05882352941176472,"last_synced_commit":"4ce1473d00408040874afc443257338e8ced4807"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiaocongDong%2Ftempar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiaocongDong%2Ftempar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiaocongDong%2Ftempar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiaocongDong%2Ftempar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/XiaocongDong","download_url":"https://codeload.github.com/XiaocongDong/tempar/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240603911,"owners_count":19827797,"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":["ast","cli","compiler","high-performance","indentation","javascript","lightweight","nodejs","parser","template"],"created_at":"2024-11-10T13:40:34.090Z","updated_at":"2026-04-11T16:41:25.302Z","avatar_url":"https://github.com/XiaocongDong.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tempar - Lightweight Template Parser\n## Notes: 🚧🚧🚧Both the code and documentation of this project is under construction, try not to use it in your real project now.\n\nFor template parsing, we already got [handlebars.js](https://github.com/wycats/handlebars.js/) and [mustache.js](https://github.com/janl/mustache.js/), they are awesome and powerful, but sometime, they may be too `heavy` for our need, we just need a lightweight template parser for generating our text from a template to get the shit done! If you have the same problem as me, Tempar can save you from the headache, it is lightweight (no more than 500 lines of code, no dependencies), flexible and maybe powerful.\n\n## Installation\n```\nnpm install tempar\n```\n\n## Supported Environment\nBoth Node and most of the browser environment.\n\n## Usage\nBelow is a quick example for using Tempar.\n\nLet's say you have a book store template which shows all of the available book list of your store.\n```\n// src/templates/store\nWelcome to {{owner.name}}'s book store!\n\n{{#if open}}\nHere is the books available for renting!\n{{#each books}}\n{{printBook}}\n{{/each}}\n{{/if}}\n```\nHere is the javascript sample code:\n\n```\nconst fs = require('fs')\nconst Parser = require('tempar')\n\nconst store1 = {\n  owner: {\n    name: 'Shawn'\n  },\n  open: true,\n  printBook: (book) =\u003e `${book.name} written by ${book.author.name}`,\n  books: [\n    {\n      name: 'fantastic book',\n      author: {\n        name: 'fantastic author'\n      }\n    },\n    {\n      name: 'awesome book',\n      author: {\n        name: 'awesome author'\n      }\n    },\n    {\n      name: 'shitty book',\n      author: {\n        name: 'shitty author'\n      }\n    },\n  ]\n}\n\nconst store2 = {\n  owner: {\n    name: 'Tommy'\n  },\n  open: false,\n  books: [\n    {\n      name: 'unknown book',\n      author: {\n        name: 'no body cares.'\n      }\n    }\n  ]\n}\n\nconst storeTemplate = fs.readFileSync('src/templates/store')\n\nconst parser = new Parser(storeTemplate.toString())\nparser.parse()\n\nconst generatedStoreContent1 = parser.generate(store1)\nconsole.log(generatedStoreContent1)\n\nconst generatedStoreContent2 = parser.generate(store2)\nconsole.log(generatedStoreContent2)\n```\n\n```\n// store 1 output\nWelcome to Shawn's book store!\n\nHere is the book available for renting!\n\nfantastic book written by fantastic author\nawesome book written by awesome author\nshitty book written by shitty author\n\n// store2 output\nWelcome to Tommy's book store!\n\n```\n\n## API\nTempar provides a `Parser` Class, it has the following methods.\n```\nconstructor(template: String)\nparse() =\u003e void\ngenerate(context: Object) =\u003e String\n```\n\n## Semantics Blocks\nA tempar template is a string that contains any number of tempar tags. Tags are indicated by the double mustaches that surround them.\n\nOverall, there are two types of tags in tempar, they are closed tag and unclosed tag.\n\nUnclosed tags include `LITERAL` and `LITERAL_HELPER` tags, closed tags include `IF` and `EACH` tags.\n\n### LITERAL\nExample: {{name}}, {{author.name}}\n\nLiteral tag only contain one parameter, it's the key of the value you want to fill in this tag.\n\n### LITERAL_HELPER\nExample: {{uppercase name}}\n\nLiteral tag contains two parameters, the first one is the key of your helper function, the second one is the key of the value you want to pass to this helper function.\n\n### IF\nExample:\n{{#if hasMore}}\n  something happens if has more...\n{{/if}}\n\nDespite the `#if` statement, `if tag` needs the key of the value you want to evaluate, the key is just like the keys for literal and literal helper tags.\n\nNotes: If the value that are evaluated is a pure object, it will become the active context of its children blocks.\n\n### EACH\nExample:\n{{#each comments}}\n  {{athor.name}}: {{content}}\n{{/each}}\n\nEach tag starts with `#each` statement, and must be provided with the key of the object you want to iterate, it must be an array.\n\nNotes: each item of the iterated object will become the active context of each children block, for example, each comment in comments will be the active context for its children block.\n\n## Context\nEach block of tempar template will has a context stack, context stack is the place where template lookup the value for the key.\n\n## Indentation\nIndentation of the closed tags will be ignored.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiaocongdong%2Ftempar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxiaocongdong%2Ftempar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiaocongdong%2Ftempar/lists"}