Skip to main content

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

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crya_loom-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crya_loom-0.1.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file crya_loom-0.1.0.tar.gz.

File metadata

  • Download URL: crya_loom-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for crya_loom-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6cc39880bff62bbccdd754bd5567943fe87d2085d410b2fa547c7c164638f4f0
MD5 f996526a765f2ec6c8d5ad3605c9ce87
BLAKE2b-256 548a0521b829969376b9d52655bce5d3a08d7c293906463be1380617eed6d16b

See more details on using hashes here.

File details

Details for the file crya_loom-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: crya_loom-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for crya_loom-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d42b232743bd9a340e826bdd7af0d9cd6d2059ccbef8f33148e839ddd2a217c5
MD5 ff7aa5d8dc40ada88afa982bb120f3c1
BLAKE2b-256 543fd6a01bcfad2b375a87ee072b6f18316d739729b25b8ca3c0dd4456b5ad99

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page