A lightweight utility library for constructing, extracting and rendering verifiable credential render templates.
Install the package:
# Yarn
yarn add @pyx-industries/vc-render-template-utils
# NPM
npm install @pyx-industries/vc-render-template-utilsimport {
constructRenderMethod,
extractRenderTemplate,
populateTemplate,
RenderMethodType,
TemplatingEngineType,
} from '@pyx-industries/vc-render-template-utils';
// Construct a render method
const template = '<div>Hello, {{name}}!</div>';
const renderMethod = constructRenderMethod(
template,
RenderMethodType.RenderTemplate2024,
);
// Extract the template
const extractedTemplate = await extractRenderTemplate(renderMethod);
// Populate the template with data
const data = { name: 'World' };
const result = populateTemplate(
TemplatingEngineType.Handlebars,
extractedTemplate,
data,
);
console.log(result); // Output: <div>Hello, World!</div>import {
constructRenderMethod,
extractRenderTemplate,
populateTemplate,
RenderMethodType,
TemplatingEngineType,
} from '@pyx-industries/vc-render-template-utils';
// Construct a render method with a URL
const renderMethod = constructRenderMethod(
'',
RenderMethodType.RenderTemplate2024,
{ url: 'http://example.com/template.html' },
);
// Extract the template from the URL
const extractedTemplate = await extractRenderTemplate(renderMethod);
// Populate the template
const data = { title: 'My Page' };
const result = populateTemplate(
TemplatingEngineType.Handlebars,
extractedTemplate,
data,
);
console.log(result); // Output: Rendered template contentconstructRenderMethod(template: string, renderMethodType: RenderMethodType, extra?: Record<string, unknown>)Constructs a render method object for the specified template and type.
template: The template string or empty if using a URL.renderMethodType: EitherRenderTemplate2024orWebRenderingTemplate2022.extra: Optional metadata (e.g.,urlormediaQuery).
extractRenderTemplate(renderMethod: RenderMethod)Extracts the template content, fetching from a URL if necessary.
renderMethod: The render method object created byconstructRenderMethod.
populateTemplate(templatingEngineType: TemplatingEngineType, template: string, data: Record<string, unknown>)Populates the template with data using the specified templating engine.
templatingEngineType: Currently supportsHandlebars.template: The template string to populate.data: The data object to populate the template.
RenderTemplate2024: Supports remote and embedded templates.WebRenderingTemplate2022: Designed for embedded templates.
Handlebars: A robust templating engine for dynamic content.
- Node.js (v22, as specified in
.nvmrc) - yarn
-
Clone the repository:
git clone https://github.com/pyx-industries/vc-render-template-utils.git cd vc-render-template-utils -
Install dependencies:
yarn install
-
Build the project:
yarn build
-
Run tests:
yarn test
yarn build: Compiles TypeScript to JavaScript.yarn test: Runs Jest tests.yarn test:ci: Runs tests with coverage.yarn format: Checks code formatting with Prettier.yarn format:fix: Auto-fixes formatting issues.yarn lint: Runs ESLint.yarn lint:fix: Auto-fixes linting issues.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -m 'Add your feature'). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.
Ensure your code adheres to the project's linting and formatting standards by running yarn lint and yarn format.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
Report bugs or suggest features by opening an issue on the GitHub Issues page.