{"id":23773517,"url":"https://github.com/beenotung/html-template-lite","last_synced_at":"2026-04-03T03:30:21.461Z","repository":{"id":57267641,"uuid":"433474554","full_name":"beenotung/html-template-lite","owner":"beenotung","description":"Simple template with html code escape","archived":false,"fork":false,"pushed_at":"2023-12-07T14:23:37.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-01T05:42:00.546Z","etag":null,"topics":["escape","html","sanitize","template"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/html-template-lite","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beenotung.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":"2021-11-30T15:03:41.000Z","updated_at":"2021-12-18T17:45:26.000Z","dependencies_parsed_at":"2025-01-01T05:41:05.331Z","dependency_job_id":"8ff914ac-2995-4c6d-bd92-0398171510e9","html_url":"https://github.com/beenotung/html-template-lite","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fhtml-template-lite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fhtml-template-lite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fhtml-template-lite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fhtml-template-lite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beenotung","download_url":"https://codeload.github.com/beenotung/html-template-lite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239961037,"owners_count":19725449,"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":["escape","html","sanitize","template"],"created_at":"2025-01-01T05:41:00.730Z","updated_at":"2026-04-03T03:30:21.391Z","avatar_url":"https://github.com/beenotung.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# html-template-lite\n\nSimple template with html code escape for both node.js and browser.\n\n[![npm Package Version](https://img.shields.io/npm/v/html-template-lite.svg?maxAge=3600)](https://www.npmjs.com/package/html-template-lite)\n\nLoops and conditions are intentionally not handled by this library. They can be handled natively with javascript expression _outside_ the template, e.g. in component's code.\n\n## Features\n\n- [x] Extremely simple, the [source code](./src/index.ts) is below 1KB\n- [x] Cross-Platform, works in browser and node.js\n- [x] Secure, prevent XSS attack (malicious script injection) with HTML escape characters\n- [x] 100% test coverage with ts-mocha\n\n## Installation\n\n### Install for npm projects:\n\n```bash\npnpm install html-template-lite\n```\n\nor\n\n```bash\nyarn add html-template-lite\n```\n\nor\n\n```bash\nnpm install html-template-lite\n```\n\n### Direct usage from browser (via script over CDN)\n\n```html\n\u003cmain class=\"demo\"\u003e\u003c/main\u003e\n\n\u003ctemplate class=\"demo\"\u003e\n  \u003cp\u003eusername: {username}\u003c/p\u003e\n  \u003cp\u003eversion: v{version}\u003c/p\u003e\n\u003c/template\u003e\n\n\u003cscript type=\"module\"\u003e\n  let main = document.querySelector('main.demo')\n  let template = document.querySelector('template.demo')\n  main.textContent = 'loading html-template-lite ...'\n\n  import { render } from 'https://cdn.jsdelivr.net/npm/html-template-lite@1/dist/esm/index.js'\n\n  main.innerHTML = render(template.innerHTML, {\n    username: '\u003cb\u003eo\u003c/b\u003e',\n    version: '1.2.3',\n  })\n\u003c/script\u003e\n```\n\nDetails see [example/demo.html](./example/demo.html)\n\n## Typescript Signature\n\n```typescript\nexport function render(\n  template: string,\n  data: object[] | object,\n  separator: string = '',\n): string\n```\n\n## Usage Guideline\n\nThe `render()` function takes a html `template` in string, and a `data`` object. It substitutes the object fields into the template and return composed html fragment in string.\n\nIf the `data` is an array of object, it will be mapped over the `render()` function again and joined with the given `separator`.\n\nIf the separator is not specified, each rendered html fragment will be joined without extra characters.\n\n### Placeholder Syntax in `template`\n\n- use `{field}` to sanitize and substitute literal (act like setting `element.textContent`)\n- use `[field]` to substitute trusted html fragment (act like setting `element.innerHTML`)\n\n## Usage Example\n\n### Substitute Context Object\n\n```typescript\nimport { render } from 'html-template-lite'\n\nlet html = render(\n  `\n\u003cdiv\u003eName: {name}\u003c/div\u003e\n\u003cdiv\u003eRank: {rank}\u003c/div\u003e\n`,\n  { name: \"\u003cb\u003eo's\u003c/b\u003e\", rank: 42 },\n)\nconsole.log(html)\n/*\n\u003cdiv\u003eName: \u0026lt;b\u0026gt;o\u0026apos;s\u0026lt;/b\u0026gt;\u003c/div\u003e\n\u003cdiv\u003eRank: 42\u003c/div\u003e\n*/\n```\n\nDetails see [example/demo.ts](./example/demo.ts)\n\n### Substitute List of Objects\n\n```typescript\nimport { render } from 'html-template-lite'\n\nlet listTemplate = `\u003cul\u003e\n[items]\n\u003c/ul\u003e`\nlet itemTemplate = `  \u003cli\u003e\u003ca href=\"{url}\"\u003e{title}\u003c/a\u003e\u003c/li\u003e`\n\nlet items = [\n  { url: 'https://github.com', title: 'Github' },\n  { url: 'https://gitlab.com', title: 'Gitlab' },\n  { url: 'https://bitbucket.org', title: 'Bitbucket' },\n]\n\nlet html = render(listTemplate, {\n  items: render(itemTemplate, items, '\\n'),\n})\nconsole.log(html)\n/*\n\u003cul\u003e\n  \u003cli\u003e\u003ca href=\"https://github.com\"\u003eGithub\u003c/a\u003e\u003c/li\u003e\n  \u003cli\u003e\u003ca href=\"https://gitlab.com\"\u003eGitlab\u003c/a\u003e\u003c/li\u003e\n  \u003cli\u003e\u003ca href=\"https://bitbucket.org\"\u003eBitbucket\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n*/\n```\n\nDetails see [example/demo.ts](./example/demo.ts)\n\n## License\n\nThis project is licensed with [BSD-2-Clause](./LICENSE)\n\nThis is free, libre, and open-source software. It comes down to four essential freedoms [[ref]](https://seirdy.one/2021/01/27/whatsapp-and-the-domestication-of-users.html#fnref:2):\n\n- The freedom to run the program as you wish, for any purpose\n- The freedom to study how the program works, and change it so it does your computing as you wish\n- The freedom to redistribute copies so you can help others\n- The freedom to distribute copies of your modified versions to others\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeenotung%2Fhtml-template-lite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeenotung%2Fhtml-template-lite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeenotung%2Fhtml-template-lite/lists"}