{"name":"rendu","version":"0.0.2","license":"MIT","_id":"rendu@0.0.2","maintainers":[{"name":"pi0","email":"pyapar@gmail.com"}],"homepage":"https://github.com/h3js/rendu#readme","bugs":{"url":"https://github.com/h3js/rendu/issues"},"bin":{"rendu":"dist/cli.mjs"},"dist":{"shasum":"c71e4e7d8a54fdbabbc8e71d92cf663e4701dfe3","tarball":"http://registry-npm.ywwl.com/rendu/download/rendu-0.0.2.tgz","fileCount":7,"integrity":"sha512-syQtJEmTABYWO/K8EbT5SQ6lly0fMoENr5T4BP9diPtjBFArPTlRjwewH2Za9L8rB6D46sBt0tYLgNVjqdENHQ==","signatures":[{"sig":"MEUCIE4PfTnc5yQ6SKXlT5j6quPMWbs+K07HKnOdecu2L8jnAiEAsr0ooysCB1V7hws3o5ccr7QziAat1kMQvP3J3MP2DXo=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":23077,"size":5868,"noattachment":false,"key":"/rendu/-/rendu-0.0.2.tgz"},"type":"module","types":"./dist/index.d.mts","exports":{".":"./dist/index.mjs"},"gitHead":"b39da1a7a8f54c05cde99fa579596ece05e1bc7a","scripts":{"dev":"vitest dev","lint":"eslint . && prettier -c .","play":"pnpm rendu playground","test":"pnpm lint && pnpm test:types && vitest run --coverage","build":"obuild","rendu":"node ./src/cli.ts","prepack":"pnpm build","release":"pnpm test && pnpm build && changelogen --release && npm publish && git push --follow-tags","lint:fix":"automd && eslint . --fix && prettier -w .","test:types":"tsc --noEmit --skipLibCheck"},"_npmUser":{"name":"pi0","email":"pyapar@gmail.com"},"repository":{"url":"git+https://github.com/h3js/rendu.git","type":"git"},"_npmVersion":"11.6.0","description":"🏎️ JavaScript Hypertext Preprocessor.","directories":{},"sideEffects":false,"_nodeVersion":"24.9.0","dependencies":{"srvx":"^0.8.14"},"_hasShrinkwrap":false,"packageManager":"pnpm@10.15.0","devDependencies":{"rendu":"workspace:*","automd":"^0.4.0","eslint":"^9.33.0","obuild":"^0.2.1","vitest":"^3.2.4","prettier":"^3.6.2","typescript":"^5.9.2","@types/node":"^24.3.0","changelogen":"^0.6.2","eslint-config-unjs":"^0.5.0","@vitest/coverage-v8":"^3.2.4"},"_npmOperationalInternal":{"tmp":"tmp/rendu_0.0.2_1759881361204_0.3948712533717713","host":"s3://npm-registry-packages-npm-production"},"readme":"# rendu\n\n🏎️ JavaScript Hypertext Preprocessor.\n\nRendu is a lightweight toolkit for mixing HTML and JavaScript with a focus on simplicity, standards and progressive rendering.\n\n> [!NOTE]\n> See [playground](./playground/) ([online playground](https://stackblitz.com/github/h3js/rendu/tree/main/playground?file=index.html)) for demos and [syntax](#syntax) section for usage.\n\n## CLI\n\nUsing the `rendu` CLI, you can start a local web server to serve static files and render `.html` files as templates (powered by [srvx](https://srvx.h3.dev)).\n\n```sh\nnpx rendu\n```\n\n## Programmatic API\n\n<!-- automd:jsdocs src=\"./src/index.ts\" -->\n\n### `compileTemplate(template, opts)`\n\nCompile a template string into a render function.\n\n**Example:**\n\n```ts\nimport { compileTemplate } from \"rendu\";\n\nconst template = `\n  <h1>{{ title }}</h1>\n  <ul>\n  <? for (const item of items) { ?>\n    <li>{{ item }}</li>\n  <? } ?>\n  </ul>\n`;\n\nconst render = compileTemplate(template, { stream: false });\n\nconst html = await render({ title: \"My List\", items: [\"Item 1\", \"Item 2\", \"Item 3\"] });\nconsole.log(html);\n// Output:\n// <h1>My List</h1>\n// <ul>\n//   <li>Item 1</li>\n//   <li>Item 2</li>\n//   <li>Item 3</li>\n// </ul>\n```\n\n### `compileTemplateToString(template, opts, asyncWrapper?)`\n\nCompile a template string into a render function code string.\n\n**Note:** This function is for advanced use cases where you need the generated code as a string.\n\n### `createRenderContext(options)`\n\n### `hasTemplateSyntax(template)`\n\nCheck if a template string contains template syntax.\n\n### `parseTemplate(template)`\n\nParse a template string into `text`, `code` and `expr` tokens.\n\n### `RENDER_CONTEXT_KEYS`\n\n- **Type**: `array`\n- **Default**: `[\"htmlspecialchars\",\"setCookie\",\"redirect\",\"$REQUEST\",\"$METHOD\",\"$URL\",\"$HEADERS\",\"$COOKIES\",\"$RESPONSE\"]`\n\n### `renderToResponse(htmlTemplate, opts)`\n\nRenders an HTML template to a Response object.\n\n**Example:**\n\n```ts\nimport { compileTemplate, renderToResponse } from \"rendu\";\n\nconst render = compileTemplate(template, { stream: true });\n\nconst response = await renderToResponse(render, { request });\n```\n\n<!-- /automd -->\n\n## Syntax\n\nRendu uses PHP-style tags to embed JavaScript within HTML templates:\n\n### Server Scripts\n\nUse `<script server>` to execute JavaScript on the server where it appears:\n\n```html\n<script server>\n  globalThis.visitedPagesCount ??= 0;\n  globalThis.visitedPagesCount++;\n</script>\n```\n\n### Output Expressions\n\nUse `{{ expression }}` for HTML-escaped output, or `{{{ expression }}}` or `<?= expression ?>` for unescaped (raw) output:\n\n```html\n<h1><?= title ?></h1>\n<div>Page visited: {{ visitedPagesCount }}</div>\n```\n\n### Control Structures\n\nUse `<? ... ?>` for JavaScript control flow:\n\n```html\n<? if (items.length === 0) { ?>\n<p>No items found.</p>\n<? } ?> <? for (const item of items) { ?>\n<li>{{ item.name }}</li>\n<? } ?>\n```\n\n### Streaming Content\n\nUse the `echo()` function for streaming content. Accepts: strings, functions, Promises, Response objects, or ReadableStreams:\n\n**Examples:**\n\n```html\n<!-- Simple string output -->\n<script server>\n  echo(\"Welcome to our site!\");\n</script>\n\n<!-- Async content from API (non-blocking)-->\n<script server>\n  echo(\"Hello\");\n  echo(async () => fetch(\"https://api.example.com/data\"));\n  echo(() => \"World\");\n</script>\n```\n\n### Global Variables\n\nAccess request context and global state:\n\n- `$REQUEST`: The incoming Request object\n- `$METHOD`: HTTP method (GET, POST, etc.)\n- `$URL`: Request URL object\n- `$HEADERS`: Request headers\n- `$RESPONSE`: Response configuration object\n- `$COOKIES`: Read-only object containing request cookies\n\n### Cookie Management\n\nUse `setCookie()` function to set cookies in the response:\n\n```html\n<script server>\n  setCookie(\"user\", \"RenduUser\");\n  setCookie(\"session\", \"abc123\", { maxAge: 3600, httpOnly: true });\n</script>\n```\n\nAccess cookies from the request using `$COOKIES`:\n\n```html\n<div>Welcome, <?= $COOKIES[\"user\"] || \"Guest\" ?>!</div>\n```\n\n### Response Control\n\nUse `redirect()` function to redirect the user:\n\n```html\n<script server>\n  if (!$COOKIES[\"auth\"]) {\n    redirect(\"/login\");\n  }\n</script>\n```\n\n### HTML Escaping\n\nThe `htmlspecialchars()` function is available for escaping HTML content:\n\n> [!TIP]\n> When using curly `{{ }}` syntax, `htmlspecialchars` will be automatically applied.\n\n```html\n<div><?= htmlspecialchars(userInput) ?></div>\n```\n\n## Development\n\n<details>\n\n<summary>local development</summary>\n\n- Clone this repository\n- Install the latest LTS version of [Node.js](https://nodejs.org/en/)\n- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`\n- Install dependencies using `pnpm install`\n- Run interactive tests using `pnpm dev`\n\n</details>\n\n## Prior Art\n\n- [jaubourg/jhp](https://github.com/jaubourg/jhp)\n- [atinux/pjs](https://github.com/atinux/pjs)\n- [mde/ejs](https://github.com/mde/ejs)\n\n## License\n\nPublished under the [MIT](https://github.com/unjs/rendu/blob/main/LICENSE) license.\n","publish_time":1759881361413,"_cnpm_publish_time":1759881361413,"dist-tags":{"latest":"0.1.1"}}