A lightweight and extensible templating engine for rendering dynamic content with an intuitive syntax. Designed to support variables, object access, control flow, and more.
- Variable Substitution: Render variables in templates using the syntax
{{ var }}
. - Object Access: Access object properties and nested keys with
{{ obj.key }}
and{{ obj.key.subkey }}
. - Nested Loops: Use nested loop constructs with
{{#loop items as item}} ... {{/loop}}
. - Conditional Logic: Add simple conditional rendering using
{% if condition %} ... {% endif %}
. - Whitespace Handling: Flexible options for preserving or trimming whitespace in templates.
- Comments: Support inline comments with
{# comment #}
syntax (non-rendered).
- Advanced Filters: Add support for chaining operations on variables like
{{ var | filter }}
. - Custom Filters: Define and use custom operations in templates, e.g.,
{{ value | uppercase }}
. - Internationalization (i18n): Add native support for language localization.
- Plugin System: Allow users to extend the engine with custom plugins.
To use the Templating Engine in your project:
- Clone the repository:
git clone https://github.com/alhassan-albadri/templating-engine.git
- Navigate into the project directory:
cd templating-engine
- Install the dependencies:
pnpm install
Here’s a basic example of using the templating engine:
import { TemplateRenderer } from './src/index';
const template = `
Hello, {{ user.name }}!
{{#if user.isActive}}
Welcome back!
{{/if}}
{{#loop user.todos as todo}}
- {{ todo }}
{{/loop}}
`;
const context = {
user: {
name: 'Alice',
isActive: true,
todos: ['Buy groceries', 'Clean the house', 'Read a book']
}
};
const renderer = new TemplateRenderer(template, context);
const result = renderer.render();
console.log(result);
Hello, Alice!
Welcome back!
- Buy groceries
- Clean the house
- Read a book
- Complete inline comment functionality.
- Expand test coverage for edge cases and invalid input handling.
- Enhance performance for large templates and deep nested loops.
- Custom Filters: Define and use custom operations in templates, e.g.,
{{ value | uppercase }}
. - Internationalization (i18n): Add native support for language localization.
- Plugin System: Allow users to extend the engine with custom plugins.
- Template Partials: Enable inclusion of partial templates for modularity.
This project uses Jest for unit testing. To run the tests:
npm test
describe('TemplateRenderer', () => {
it('should render variables correctly', () => {
const template = 'Hello, {{ name }}!';
const context = { name: 'Alice' };
const renderer = new TemplateRenderer(template, context);
expect(renderer.render()).toBe('Hello, Alice!');
});
});
We welcome contributions! Here’s how you can get involved:
- Fork the repository on GitHub.
- Create a new branch for your feature or bugfix:
git checkout -b feature/your-feature-name
- Commit your changes with clear and descriptive messages:
git commit -m "Add support for custom filters"
- Push your branch to your forked repository:
git push origin feature/your-feature-name
- Submit a pull request and explain your changes in detail.
- Follow the coding style of the project.
- Include tests for new features or bug fixes.
- Ensure all tests pass before submitting your pull request.
This project is licensed under the MIT License. You are free to use, modify, and distribute this project under the terms of the license.
Happy templating! 🎉