Skip to main content

Loom

A simple and very flexible Python template engine heavily inspired by Laravel Blade.

Do not use in production. Just for fun, for now.

Contrary to other languages like jinja, it allows executing any Python code within the template. Therefore, this should not be used for templates that can be written by external users (prefer something like jinja for this).

Installation

pip install crya-loom

Usage

Basic Template Rendering

Render from file:

from pathlib import Path
from crya_loom import render

# Render a template file with context
output = render(Path("templates/index.loom"), {
    "title": "Welcome",
    "users": ["Alice", "Bob", "Charlie"]
})

Render from string:

from crya_loom import render_from_string

template = """
<h1>{{ title }}</h1>
@for(user in users)
    <p>{{ user }}</p>
@endfor
"""

output = render_from_string(template, {
    "title": "Users",
    "users": ["Alice", "Bob"]
})

Configuration

Component base directory:

from crya_loom import set_component_base_dir

# Set custom component directory (default: "components")
set_component_base_dir("templates/components")

Cache directory:

from crya_loom import set_cache_dir

# Set custom cache directory (default: "cache/compiled/templates")
set_cache_dir("tmp/loom_cache")

Template directory: Templates are not tied to a specific directory. Simply pass the full path to render():

from pathlib import Path
from crya_loom import render

# Render templates from any location
output = render(Path("/path/to/my/templates/page.loom"), context)

Syntax

Comments

{{-- This is a comment --}}

Comments can be multiline:

{{-- This is a multiline comment
     that spans multiple lines --}}

Print (escaped)

{{ user.name }}

Unsafe print

{!! user.name !!}

Condition

@if(time == "morning")
    <div>Good morning</div>
@elif(time == "afternoon")
    <div>Good afternoon</div>
@else
    <div>Good evening</div>
@endif

Loops

@for(user in users)
    ...
@endfor

Python blocks

@python
    my_var = "hello"
@endpython

{{ my_var }}

Non interpreted text

@verbatim
    I like to use these characters in my book: "{{" and "}}".
@endverbatim

Components

Create reusable components with <x-component-name> syntax.

Basic Usage

  1. Set component directory:
from crya_loom import set_component_base_dir
set_component_base_dir("templates/components")
  1. Create component file templates/components/alert.loom:
<div class="alert alert-{{ type }}">
    {{ slot }}
</div>
  1. Use in templates:
<x-alert type="danger">Error message</x-alert>

Nested Components

Use dot notation for subdirectories:

<x-card.header title="Welcome">Subtitle</x-card.header>

Maps to components/card/header.loom

Dynamic Attributes

Prefix with : to evaluate expressions:

<x-alert :type="error_level" :message="error_msg" />

Special Variables

Components receive:

  • slot - Content between opening/closing tags
  • attributes - Dict of all passed attributes
  • All parent template variables (inherited context)

Metadata

Release files for crya-loom 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for crya-loom 0.1.0
File Size Uploaded Provenance
crya_loom-0.1.0.tar.gz 5.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for crya-loom 0.1.0
File Interpreter ABI Platform
crya_loom-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.5 kB

Release files / crya_loom-0.1.0.tar.gz

Download URL crya_loom-0.1.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6cc39880bff62bbccdd754bd5567943fe87d2085d410b2fa547c7c164638f4f0
BLAKE2b-256 checksum
How to use checksums
548a0521b829969376b9d52655bce5d3a08d7c293906463be1380617eed6d16b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.7

Release files / crya_loom-0.1.0-py3-none-any.whl

Download URL crya_loom-0.1.0-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d42b232743bd9a340e826bdd7af0d9cd6d2059ccbef8f33148e839ddd2a217c5
BLAKE2b-256 checksum
How to use checksums
543fd6a01bcfad2b375a87ee072b6f18316d739729b25b8ca3c0dd4456b5ad99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.7

Security for crya-loom 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page