oxidite-template
Template engine for server-side rendering in Oxidite.
Overview
oxidite-template provides a powerful and flexible template engine for server-side rendering in the Oxidite web framework. It offers Jinja2-inspired syntax with features like template inheritance, includes, filters, and automatic HTML escaping for security.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Features
- Template inheritance - Base templates with block placeholders for content
- Template includes - Reusable partial templates
- Auto-escaping - Protection against XSS attacks
- Filters - Transform values (uppercase, lowercase, truncate, etc.)
- Loops and conditionals - Control flow in templates
- Custom filters - Extend functionality with custom filters
- Template caching - Improved performance with compiled template caching
- Internationalization support - Multi-language template capabilities
Usage
Basic Setup
Initialize the template engine and register templates:
use ;
// Create a new template engine
let mut engine = new;
// Load all templates from the directory
engine.load_templates.await?;
Creating Templates
Create your base template (templates/base.html):
{% block title %}Default Title{% endblock %}
Navigation here
{% block content %}{% endblock %}
© 2025 My App
Create a child template (templates/page.html) that extends the base:
{% extends "base.html" %}
{% block title %}{{ page_title }}{% endblock %}
{% block content %}
{{ heading }}
{{ content }}
{% if show_button %}
{{ button_text }}
{% endif %}
{% for item in items %}
{{ item.name | upper }}
{% endfor %}
{% endblock %}
Rendering Templates
Render templates with dynamic data:
use ;
// Create context with data
let mut context = new;
context.insert;
context.insert;
context.insert;
context.insert;
context.insert;
// Add a vector of items
let items = vec!;
context.insert;
// Render the template
let html = engine.render.await?;
Template Filters
Apply transformations to values in templates:
<!-- Convert to uppercase -->
{{ title | upper }}
<!-- Convert to lowercase -->
{{ description | lower }}
<!-- Truncate text -->
{{ long_text | truncate(100) }}
<!-- Escape HTML (done automatically by default) -->
{{ user_input | escape }}
<!-- URL encode -->
Search
Conditional Logic
Handle conditional rendering in templates:
{% if user.is_authenticated %}
Welcome back, {{ user.name }}!
{% if user.is_admin %}
Dashboard
{% endif %}
{% else %}
Log In
{% endif %}
Loops
Iterate over collections in templates:
{% for product in products %}
{{ product.name }}
Price: ${{ product.price }}
{% if product.on_sale %}
On Sale!
{% endif %}
{% else %}
No products available.
{% endfor %}
Template Includes
Include reusable template parts:
<!-- Include a header component -->
{% include "partials/header.html" %}
<!-- Include with context -->
{% include "components/alert.html" with { type: "info", message: "Hello" } %}
Integration with Oxidite
Using templates with Oxidite's response utilities:
use *;
async
Security
The template engine includes built-in security features:
- Automatic HTML escaping prevents cross-site scripting (XSS) attacks
- Sandboxed execution prevents arbitrary code execution in templates
- Whitelisted filters only safe operations are allowed
Performance
- Template caching compiled templates are cached for improved performance
- Efficient rendering optimized algorithms for fast template processing
- Memory management efficient memory usage during rendering
License
MIT