{"id":13630306,"url":"https://github.com/terkelg/cantinflas","last_synced_at":"2025-05-12T15:32:29.725Z","repository":{"id":57193659,"uuid":"121933398","full_name":"terkelg/cantinflas","owner":"terkelg","description":"Tiny mustache-like template engine in ~50 LOC.","archived":false,"fork":false,"pushed_at":"2019-08-05T08:37:27.000Z","size":617,"stargazers_count":65,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-08T21:38:42.382Z","etag":null,"topics":["mustache","template","template-engine"],"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/terkelg.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":"2018-02-18T08:33:46.000Z","updated_at":"2024-02-22T13:40:07.000Z","dependencies_parsed_at":"2022-09-15T22:20:18.070Z","dependency_job_id":null,"html_url":"https://github.com/terkelg/cantinflas","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terkelg%2Fcantinflas","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terkelg%2Fcantinflas/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terkelg%2Fcantinflas/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terkelg%2Fcantinflas/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/terkelg","download_url":"https://codeload.github.com/terkelg/cantinflas/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225141710,"owners_count":17427356,"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","template","template-engine"],"created_at":"2024-08-01T22:01:37.849Z","updated_at":"2024-11-18T06:54:39.398Z","avatar_url":"https://github.com/terkelg.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/terkelg/cantinflas/raw/master/cantinflas.png\" alt=\"Cantinflas\" width=\"300\" /\u003e\n\u003c/div\u003e\n\n\u003ch1 align=\"center\"\u003e{{ Cantinflas }}\u003c/h1\u003e\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://npmjs.org/package/cantinflas\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/cantinflas.svg\" alt=\"version\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://travis-ci.org/terkelg/cantinflas\"\u003e\n    \u003cimg src=\"https://img.shields.io/travis/terkelg/cantinflas.svg\" alt=\"travis\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://npmjs.org/package/cantinflas\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/dm/cantinflas.svg\" alt=\"downloads\" /\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003eTiny mustache-like template engine in ~50 LOC\u003c/div\u003e\n\n\u003cbr\u003e\n\nCantinflas is a tiny, logic-less, template engine and subset of [Mustache](http://mustache.github.io/).\nPerfect for project scaffolding and other non-performance critical tasks.\n\n\n## Install\n\n```\n$ npm install cantinflas\n```\n\n## Core Features\n\n- **lightweight:** zero dependencies and only ~50 LOC\n- **powerful:** support all the features you'd expect from a template-engine\n- **safe**: No `eval()` or `new Function()`\n- **minimal API:** one function call, that's all it take\n- **familiar:** Cantinflas is almost a full mustache implementation\n- **formatting:** respects the formatting of your source files\n\n## Why\n\nI needed a simple way to replace a few placeholders as a part of a scaffolding tool. There's no need for big high-performance template engines with tons of dependencies. I see a lot of CLI tools depend on projects like handlebars for stuff like this which in my opinion is overkill. KISS. XO.\n\n\n## Usage\n\n```js\nimport cantinflas from 'cantinflas';\n\nconst template = `Hello {{name}}!`\nconst data = { name: 'World' };\n\nconst output = cantinflas(template, data);\n// =\u003e \"Hello World\"\n\n```\n\n## Tag Types\n\nJust as Mustache, tags are indicated by the double curly brackets. `{{person}}` is a tag, as is `{{#person}}`.\n\n### Variables\n\nThe most basic tag type is the variable. A `{{name}}` tag in a basic template will try to find the `name` key in the current context.\nIf there is no `name` key found, nothing will be rendered.\nKeys that are not found in the data object are ignored.\n\n##### Template\n\n```\n* {{name}}\n* {{age}}\n* {{company}}\n```\n\n##### Data\n\n```js\n{\n  \"name\": \"Robin\",\n  \"company\": \"Wayne Enterprises\"\n}\n```\n\n##### Output\n```\n* Robin\n*\n* Wayne Enterprises\n```\n\n\n### Sections\n\nSections render blocks of text one or more times, depending on the value of the key in the current context.\nA section begins with a pound and ends with a slash. That is, `{{#person}}` begins a \"person\" section while `{{/person}}` ends it.\nThe behavior of the section is determined by the value of the key.\n\n#### False Values or Empty Lists\nIf the `person` key exists and has a value of false or an empty list, the content between the pound and slash will not be displayed.\n\n##### Template\n\n```\nShown.\n{{#person}}\n  Never shown!\n{{/person}}\n```\n\n##### Data\n\n```js\n{\n  \"person\": false\n}\n```\n\n##### Output\n\n```\nShown.\n```\n\n#### Non-empty Lists\nIf the `person` key exists and has a non-false value, the content between the pound and slash will be rendered and displayed one or more times.\nWhen the value is a non-empty array or object, the text in the block will be displayed once for each item in the list. The context of the block will be set to the current item for each iteration. In this way we can loop over collections.\n\nWhen interating lists, there's four special variables availble for you.\n* `.`: This is the current item in the array.\n* `@index`: This is the index of the current item.\n* `@first`: True if the current iteration is the first.\n* `@last`: True if the current iteration is the last.\n\n##### Template\n\n```\n{{#repo}}\n - {{@index}} {{name}}\n{{/repo}}\n```\n\n##### Data\n\n```js\n{\n  \"repo\": [\n    { \"name\": \"resque\" },\n    { \"name\": \"hub\" },\n    { \"name\": \"rip\" }\n  ]\n}\n```\n\n##### Output\n\n```\n- 0 resque\n- 1 hub\n- 2 rip\n```\n\n### Function\nWhen the value is a function, it will be invoked and passed the block of text. The text passed is the literal block, unrendered.\n`{{tags}}` will not have been expanded.\n\n##### Template\n\n```\n{{#wrapped}}\n  {{name}} is awesome.\n{{/wrapped}}\n```\n\n##### Data\n\n```js\n{\n  \"name\": \"Willy\",\n  \"wrapped\": function(inner) {\n    return `${inner}`\n  }\n}\n```\n\n##### Output\n\n```\nWilly is awesome.\n```\n\n#### Non-False Values\nWhen the value is non-false but not a object, it will be used as the context for a single rendering of the block.\n\n##### Template\n\n```\n{{#person?}}\n  Hi {{name}}!\n{{/person?}}\n```\n\n##### Object\n\n```js\n{\n  \"person?\": { \"name\": \"Jon\" }\n}\n```\n\n##### Output\n\n```\nHi Jon!\n```\n\n### Inverted Sections\n\nAn inverted section begins with a caret (hat) and ends with a slash. That is `{{^person}}` begins a \"person\" inverted section while `{{/person}}` ends it.\nInverted sections render text once based on the inverse value of the key. That is, they will be rendered if the key doesn't exist, is false, or is an empty list.\n\n##### Template\n\n```\n{{#repo}}\n  - {{name}}\n{{/repo}}\n{{^repo}}\n  No repos :(\n{{/repo}}\n```\n\n##### Object\n\n```js\n{\n  \"repo\": []\n}\n```\n\n##### Output\n\n```\nNo repos :(\n```\n\n\n## API\n\n### cantinflas(template, data)\n\nReturn: `String`\n\nCompiled template.\n\n#### template\n\nType: `string`\n\nA string template. This the input you want to compile.\n\n\n#### data\n\nType: `Object`\n\nData object to use for the compiling.\n\n\n## License\n\nMIT © [Terkel Gjervig](https://terkel.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fterkelg%2Fcantinflas","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fterkelg%2Fcantinflas","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fterkelg%2Fcantinflas/lists"}