Spaces:
Sleeping
Sleeping
Download src/common/utils/template-render.ts from jinksQspider/openWa: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/common/utils/template-render.ts
- Command line
-
hf download hf://spaces/jinksQspider/openWa/src/common/utils/template-render.ts
-
curl -L -o template-render.ts https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/common/utils/template-render.ts
1.48 kB
| /** | |
| * Shared server-side text template renderer. | |
| * | |
| * Substitutes Handlebars-style `{{name}}` placeholders with values from the | |
| * provided `vars` map. Placeholders whose key is absent from `vars` are left | |
| * untouched (the literal `{{key}}` is preserved) so that missing variables are | |
| * visible rather than silently blanked. | |
| * | |
| * NOTE: bulk-message.service.ts ships an independent single-brace `{name}` | |
| * renderer (see `applyVariables`). The two placeholder conventions | |
| * (`{{name}}` here vs `{name}` there) should be reconciled into this shared | |
| * helper in a follow-up so the gateway exposes one consistent templating | |
| * syntax. See issue #69. | |
| */ | |
| const PLACEHOLDER_PATTERN = /\{\{\s*([\w.-]+)\s*\}\}/g; | |
| /** | |
| * Render a template body by replacing `{{key}}` placeholders with `vars[key]`. | |
| * | |
| * @param body Template text containing `{{key}}` placeholders. | |
| * @param vars Map of placeholder keys to substitution values. | |
| * @returns The rendered text with known placeholders substituted and unknown | |
| * placeholders left as literal `{{key}}`. | |
| */ | |
| export function renderTemplate(body: string, vars: Record<string, string> = {}): string { | |
| if (!body) { | |
| return body; | |
| } | |
| return body.replace(PLACEHOLDER_PATTERN, (match, key: string) => { | |
| if (Object.prototype.hasOwnProperty.call(vars, key) && vars[key] != null) { | |
| return String(vars[key]); | |
| } | |
| // Leave unmatched placeholders literal so missing variables stay visible. | |
| return match; | |
| }); | |
| } | |