relaxtemplates


Namerelaxtemplates JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttps://github.com/Ravikisha/relaxtemplates
SummaryA Python-based template engine that features variables, conditionals, loops, call, extends, includes, comments, and more, making it easy to create dynamic web pages.
upload_time2024-10-26 07:21:59
maintainerNone
docs_urlNone
authorRavi Kishan
requires_python>=3.8
licenseMIT
keywords template engine python dynamic templates web development variables conditionals loops includes extends comments
VCS
bugtrack_url
requirements asgiref Django Jinja2 MarkupSafe sqlparse tzdata
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![RelaxTemplates Banner](https://ravikisha.github.io/assets/relaxtemplates.jpg)

# Relaxtemplates

<p float="left">
<img src="https://img.shields.io/badge/Python-3.6%2B-blue" alt="Python 3.6+"> 
<img src="https://img.shields.io/badge/PyPI-v24.2-blue" alt="PyPI v24.2">
<img src="https://img.shields.io/badge/Version-1.0.0-blue" alt="Version 1.0.0">
<img src="https://img.shields.io/badge/License-MIT-green" alt="MIT License"> 
<img src="https://img.shields.io/badge/Status-Active-green" alt="Active Status">
</p>

Relaxtemplates is a simplified, educational template engine designed to help developers understand the inner workings of template rendering. This project is not production-ready but instead serves as a hands-on exploration of template rendering with fundamental features commonly found in templating systems, such as variable substitution, control flow with conditionals, loops, callable functions, template inheritance, and includes.

## Why Relaxtemplates?

Relaxtemplates was created to provide a playground for experimenting with templating features and to gain insight into template engine design. It showcases key templating principles and techniques while allowing flexibility for customizations and feature expansions.

## Features

Relaxtemplates supports the following features:

1. **Variable Substitution**: Dynamically replace variables with values from the context.
2. **Control Flow (Conditionals)**: Use `if` conditions to control the flow of template content.
3. **Loops**: Use `each` to iterate over lists and collections.
4. **Callable Functions**: Pass and invoke callable functions within templates.
5. **Template Inheritance**: Extend base templates with `extend` and `block` to achieve layout inheritance.
6. **Includes**: Insert reusable template snippets with `include`.

## Getting Started

To use Relaxtemplates, include the files in your project and create a simple template. Templates are HTML files with special syntax for variables, blocks, and includes.

### Template Syntax

Relaxtemplates uses three main syntaxes: `{{ variable }}`, `{% block %}...{% endblock %}`, and `{% include %}`. These enable flexible, structured templates.

### Variables

Variables are enclosed in `{{ }}` and are replaced by corresponding values in the provided context.

```html
<div>Hello, {{ user_name }}!</div>
```

### Blocks

Blocks are enclosed in `{% %}` tags, allowing for control structures like conditionals and loops. 

#### Conditionals

Relaxtemplates supports conditionals for control flow with operators such as `>`, `<`, `>=`, `<=`, `==`, and `!=`. For instance:

```html
{% if user_age > 18 %}
    <p>Welcome, adult user!</p>
{% else %}
    <p>Welcome, young user!</p>
{% end %}
```

#### Loops

The `{% each %}` block allows you to iterate over a collection. The `it` variable represents each item in the iteration, and `..` accesses attributes from the outer scope.

```html
{% each items %}
    <p>{{ it }}</p>
{% end %}
```

Example of referencing the outer context within a loop:

```html
{% each items %}
    <p>Outer name: {{ ..name }}</p>
    <p>Item: {{ it }}</p>
{% end %}
```

#### Callable Functions

The `{% call %}` block allows for invoking functions with both positional and keyword arguments:

```html
<p>{% call format_date date_created %}</p>
<p>{% call log 'Event logged' level='debug' %}</p>
```

### Template Inheritance

Templates can extend a base template, providing a layout structure that child templates can override within defined blocks.

Base template (`base.html`):
```html
<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}Default Title{% endblock %}</title>
</head>
<body>
    <div id="content">
        {% block content %}Default content.{% endblock %}
    </div>
</body>
</html>
```

Child template (`child.html`):
```html
{% extend 'base' %}
{% block title %}Custom Page Title{% endblock %}
{% block content %}
    <p>This is custom content for the child template.</p>
{% endblock %}
```

### Includes

Relaxtemplates supports includes for inserting reusable template snippets, such as headers and footers, using `{% include 'template_name' %}`.

```html
{% include 'header' %}
<p>Welcome to the page!</p>
{% include 'footer' %}
```

### Example Usage

1. **Define Template and Context**:
   Create a template file, e.g., `my_template.html` with variables, loops, and conditionals.

   ```html
   <h1>Welcome, {{ user_name }}!</h1>
   {% if is_member %}
       <p>Thanks for being a member!</p>
   {% else %}
       <p>Please sign up to become a member.</p>
   {% end %}
   ```

2. **Rendering**:
   Use the `Template` class to load, compile, and render the template with a given context.

   ```python
   template = Template('my_template', {'user_name': 'Alice', 'is_member': True})
   print(template.render())
   ```

## Performance

While Relaxtemplates is a simple engine, benchmarks indicate it performs efficiently. However, it lacks the optimization layers present in more mature engines like Django or Jinja2, so it is best suited for educational use and smaller projects.

| Template                | Runs       | Time Taken (ms) |
|------------------------|------------|-----------------|
| relaxtemplates         | 10,000     | 0.19            |
| django                 | 10,000     | 0.39            |
| django_default_loader  | 10,000     | 0.22            |
| jinja2                 | 10,000     | 3.28            |
| jinja2_env             | 10,000     | 0.10            |


## Contribution

Feel free to fork and explore the code. Improvements and experiments are welcome, as this project is meant for exploration and learning in template engine design.

Happy templating with Relaxtemplates!

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Ravikisha/relaxtemplates",
    "name": "relaxtemplates",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "template engine, python, dynamic templates, web development, variables, conditionals, loops, includes, extends, comments",
    "author": "Ravi Kishan",
    "author_email": "ravikishan63392@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/b3/af/ed24d4e3cfae3ad0b7dced501f92e4754095c5059e4eb4f325f3ef18eab4/relaxtemplates-1.0.2.tar.gz",
    "platform": null,
    "description": "![RelaxTemplates Banner](https://ravikisha.github.io/assets/relaxtemplates.jpg)\r\n\r\n# Relaxtemplates\r\n\r\n<p float=\"left\">\r\n<img src=\"https://img.shields.io/badge/Python-3.6%2B-blue\" alt=\"Python 3.6+\"> \r\n<img src=\"https://img.shields.io/badge/PyPI-v24.2-blue\" alt=\"PyPI v24.2\">\r\n<img src=\"https://img.shields.io/badge/Version-1.0.0-blue\" alt=\"Version 1.0.0\">\r\n<img src=\"https://img.shields.io/badge/License-MIT-green\" alt=\"MIT License\"> \r\n<img src=\"https://img.shields.io/badge/Status-Active-green\" alt=\"Active Status\">\r\n</p>\r\n\r\nRelaxtemplates is a simplified, educational template engine designed to help developers understand the inner workings of template rendering. This project is not production-ready but instead serves as a hands-on exploration of template rendering with fundamental features commonly found in templating systems, such as variable substitution, control flow with conditionals, loops, callable functions, template inheritance, and includes.\r\n\r\n## Why Relaxtemplates?\r\n\r\nRelaxtemplates was created to provide a playground for experimenting with templating features and to gain insight into template engine design. It showcases key templating principles and techniques while allowing flexibility for customizations and feature expansions.\r\n\r\n## Features\r\n\r\nRelaxtemplates supports the following features:\r\n\r\n1. **Variable Substitution**: Dynamically replace variables with values from the context.\r\n2. **Control Flow (Conditionals)**: Use `if` conditions to control the flow of template content.\r\n3. **Loops**: Use `each` to iterate over lists and collections.\r\n4. **Callable Functions**: Pass and invoke callable functions within templates.\r\n5. **Template Inheritance**: Extend base templates with `extend` and `block` to achieve layout inheritance.\r\n6. **Includes**: Insert reusable template snippets with `include`.\r\n\r\n## Getting Started\r\n\r\nTo use Relaxtemplates, include the files in your project and create a simple template. Templates are HTML files with special syntax for variables, blocks, and includes.\r\n\r\n### Template Syntax\r\n\r\nRelaxtemplates uses three main syntaxes: `{{ variable }}`, `{% block %}...{% endblock %}`, and `{% include %}`. These enable flexible, structured templates.\r\n\r\n### Variables\r\n\r\nVariables are enclosed in `{{ }}` and are replaced by corresponding values in the provided context.\r\n\r\n```html\r\n<div>Hello, {{ user_name }}!</div>\r\n```\r\n\r\n### Blocks\r\n\r\nBlocks are enclosed in `{% %}` tags, allowing for control structures like conditionals and loops. \r\n\r\n#### Conditionals\r\n\r\nRelaxtemplates supports conditionals for control flow with operators such as `>`, `<`, `>=`, `<=`, `==`, and `!=`. For instance:\r\n\r\n```html\r\n{% if user_age > 18 %}\r\n    <p>Welcome, adult user!</p>\r\n{% else %}\r\n    <p>Welcome, young user!</p>\r\n{% end %}\r\n```\r\n\r\n#### Loops\r\n\r\nThe `{% each %}` block allows you to iterate over a collection. The `it` variable represents each item in the iteration, and `..` accesses attributes from the outer scope.\r\n\r\n```html\r\n{% each items %}\r\n    <p>{{ it }}</p>\r\n{% end %}\r\n```\r\n\r\nExample of referencing the outer context within a loop:\r\n\r\n```html\r\n{% each items %}\r\n    <p>Outer name: {{ ..name }}</p>\r\n    <p>Item: {{ it }}</p>\r\n{% end %}\r\n```\r\n\r\n#### Callable Functions\r\n\r\nThe `{% call %}` block allows for invoking functions with both positional and keyword arguments:\r\n\r\n```html\r\n<p>{% call format_date date_created %}</p>\r\n<p>{% call log 'Event logged' level='debug' %}</p>\r\n```\r\n\r\n### Template Inheritance\r\n\r\nTemplates can extend a base template, providing a layout structure that child templates can override within defined blocks.\r\n\r\nBase template (`base.html`):\r\n```html\r\n<!DOCTYPE html>\r\n<html>\r\n<head>\r\n    <title>{% block title %}Default Title{% endblock %}</title>\r\n</head>\r\n<body>\r\n    <div id=\"content\">\r\n        {% block content %}Default content.{% endblock %}\r\n    </div>\r\n</body>\r\n</html>\r\n```\r\n\r\nChild template (`child.html`):\r\n```html\r\n{% extend 'base' %}\r\n{% block title %}Custom Page Title{% endblock %}\r\n{% block content %}\r\n    <p>This is custom content for the child template.</p>\r\n{% endblock %}\r\n```\r\n\r\n### Includes\r\n\r\nRelaxtemplates supports includes for inserting reusable template snippets, such as headers and footers, using `{% include 'template_name' %}`.\r\n\r\n```html\r\n{% include 'header' %}\r\n<p>Welcome to the page!</p>\r\n{% include 'footer' %}\r\n```\r\n\r\n### Example Usage\r\n\r\n1. **Define Template and Context**:\r\n   Create a template file, e.g., `my_template.html` with variables, loops, and conditionals.\r\n\r\n   ```html\r\n   <h1>Welcome, {{ user_name }}!</h1>\r\n   {% if is_member %}\r\n       <p>Thanks for being a member!</p>\r\n   {% else %}\r\n       <p>Please sign up to become a member.</p>\r\n   {% end %}\r\n   ```\r\n\r\n2. **Rendering**:\r\n   Use the `Template` class to load, compile, and render the template with a given context.\r\n\r\n   ```python\r\n   template = Template('my_template', {'user_name': 'Alice', 'is_member': True})\r\n   print(template.render())\r\n   ```\r\n\r\n## Performance\r\n\r\nWhile Relaxtemplates is a simple engine, benchmarks indicate it performs efficiently. However, it lacks the optimization layers present in more mature engines like Django or Jinja2, so it is best suited for educational use and smaller projects.\r\n\r\n| Template                | Runs       | Time Taken (ms) |\r\n|------------------------|------------|-----------------|\r\n| relaxtemplates         | 10,000     | 0.19            |\r\n| django                 | 10,000     | 0.39            |\r\n| django_default_loader  | 10,000     | 0.22            |\r\n| jinja2                 | 10,000     | 3.28            |\r\n| jinja2_env             | 10,000     | 0.10            |\r\n\r\n\r\n## Contribution\r\n\r\nFeel free to fork and explore the code. Improvements and experiments are welcome, as this project is meant for exploration and learning in template engine design.\r\n\r\nHappy templating with Relaxtemplates!\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Python-based template engine that features variables, conditionals, loops, call, extends, includes, comments, and more, making it easy to create dynamic web pages.",
    "version": "1.0.2",
    "project_urls": {
        "Homepage": "https://github.com/Ravikisha/relaxtemplates"
    },
    "split_keywords": [
        "template engine",
        " python",
        " dynamic templates",
        " web development",
        " variables",
        " conditionals",
        " loops",
        " includes",
        " extends",
        " comments"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c13b6bbef721af3ffab8b99e4837c3c055f5305e9332d1c516b8d5c73d78bac1",
                "md5": "2a9d4cc590f9e7a6eb94f010028aac18",
                "sha256": "c91848f6d5be155f44e51a6ba872507228f85b689c41f5446d8bb46aa902d23d"
            },
            "downloads": -1,
            "filename": "relaxtemplates-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2a9d4cc590f9e7a6eb94f010028aac18",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7812,
            "upload_time": "2024-10-26T07:21:58",
            "upload_time_iso_8601": "2024-10-26T07:21:58.094585Z",
            "url": "https://files.pythonhosted.org/packages/c1/3b/6bbef721af3ffab8b99e4837c3c055f5305e9332d1c516b8d5c73d78bac1/relaxtemplates-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b3afed24d4e3cfae3ad0b7dced501f92e4754095c5059e4eb4f325f3ef18eab4",
                "md5": "474b650579a5e5bb53974c6822e35387",
                "sha256": "bbc5c6ad09562b78beb87ec5eade34994067ff7d495984d0a1a411962cb14fc9"
            },
            "downloads": -1,
            "filename": "relaxtemplates-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "474b650579a5e5bb53974c6822e35387",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 8027,
            "upload_time": "2024-10-26T07:21:59",
            "upload_time_iso_8601": "2024-10-26T07:21:59.189956Z",
            "url": "https://files.pythonhosted.org/packages/b3/af/ed24d4e3cfae3ad0b7dced501f92e4754095c5059e4eb4f325f3ef18eab4/relaxtemplates-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-26 07:21:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Ravikisha",
    "github_project": "relaxtemplates",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "asgiref",
            "specs": [
                [
                    "==",
                    "3.8.1"
                ]
            ]
        },
        {
            "name": "Django",
            "specs": [
                [
                    "==",
                    "5.1.2"
                ]
            ]
        },
        {
            "name": "Jinja2",
            "specs": [
                [
                    "==",
                    "3.1.4"
                ]
            ]
        },
        {
            "name": "MarkupSafe",
            "specs": [
                [
                    "==",
                    "3.0.2"
                ]
            ]
        },
        {
            "name": "sqlparse",
            "specs": [
                [
                    "==",
                    "0.5.1"
                ]
            ]
        },
        {
            "name": "tzdata",
            "specs": [
                [
                    "==",
                    "2024.2"
                ]
            ]
        }
    ],
    "lcname": "relaxtemplates"
}
        
Elapsed time: 1.58036s