Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ jobs:
python-version: ["3.12"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: ~/.cache/pypoetry
key: ${{ runner.os }}-poetry-${{ hashFiles('**/poetry.lock', '**/pyproject.toml') }}
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
Expand All @@ -19,7 +23,9 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install poetry pytest
poetry install
poetry install --no-root --no-interaction
- name: Remove Poetry cache
run: poetry cache clear . --all -n
- name: Lint with Ruff
run: |
pip install ruff
Expand All @@ -28,6 +34,3 @@ jobs:
- name: Test with pytest
run: |
poetry run pytest
#- name: Generate Coverage Report
# run: |
# coverage report -m
26 changes: 26 additions & 0 deletions examples/basic_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3
"""
Simple test to verify basic template functionality.
"""

from trim_template.trim import TrimTemplate

# Simple test with basic variables
template_vars = {
'page_title': 'Test Page',
'site_name': 'TestApp',
'main_heading': 'Hello World',
'content': 'This is a test.',
'current_year': 2025,
'user': type('User', (), {'logged_in': False})(),
'users': [],
}

def basic_example():
tmpl = TrimTemplate('examples/main.html.trim', vars=template_vars, pretty=True)
output = tmpl.render()
print("=== Basic Example Output ===\n")
print(output)

if __name__ == '__main__':
basic_example()
8 changes: 8 additions & 0 deletions examples/footer.html.trim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
footer.site-footer
.footer-content
p © {current_year} {site_name}. All rights reserved.

.footer-links
a href='/privacy' Privacy Policy
a href='/terms' Terms of Service
a href='/contact' Contact Us
11 changes: 11 additions & 0 deletions examples/header.html.trim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
header.navbar
.nav-brand
a href='/' {site_name}

nav.nav-menu
- if user.logged_in
a href='/profile' Profile
a href='/logout' Logout
- else
a href='/login' Login
a href='/register' Register
21 changes: 21 additions & 0 deletions examples/main.html.trim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
doctype html

html
head
title {page_title}
stylesheet src='/styles.css'
meta charset='utf-8'

body
- render header.html.trim

main.container
h1 {main_heading}
p {content}

.user-list
h2 Users
- for user in users
- render user_card.html.trim

- render footer.html.trim
81 changes: 81 additions & 0 deletions examples/sub_template_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python3
"""
Example demonstrating sub-template usage with TrimTemplate.
This shows how to include multiple sub-templates in a main template.
"""

from trim_template.trim import TrimTemplate
from datetime import datetime

# Simple class to represent a user object
class User:
def __init__(self, id, name, email, role, avatar_url, is_admin=False):
self.id = id
self.name = name
self.email = email
self.role = role
self.avatar_url = avatar_url
self.is_admin = is_admin

# Simple class to represent the current user
class CurrentUser:
def __init__(self, logged_in=True, name="", email=""):
self.logged_in = logged_in
self.name = name
self.email = email

# Sample data for the template
template_vars = {
'page_title': 'User Dashboard',
'site_name': 'MyAwesomeApp',
'main_heading': 'Welcome to the Dashboard',
'content': 'This page demonstrates sub-template usage with header, user cards, and footer.',
'current_year': datetime.now().year,
'user': CurrentUser(
logged_in=True,
name='John Doe',
email='john@example.com'
),
'users': [
User(
id=1,
name='Alice Johnson',
email='alice@example.com',
role='Admin',
avatar_url='/avatars/alice.jpg',
is_admin=True
),
User(
id=2,
name='Bob Smith',
email='bob@example.com',
role='User',
avatar_url='/avatars/bob.jpg',
is_admin=False
),
User(
id=3,
name='Carol Davis',
email='carol@example.com',
role='Moderator',
avatar_url='/avatars/carol.jpg',
is_admin=True
)
]
}

def main():
# Create template instance with pretty formatting
tmpl = TrimTemplate('examples/main.html.trim', vars=template_vars, pretty=True)

# Render the template
output = tmpl.render()

print("=== Sub-Template Example Output ===\n")
print(output)

print("\n=== Template Debug Info ===")
tmpl.debug()

if __name__ == '__main__':
main()
13 changes: 13 additions & 0 deletions examples/user_card.html.trim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.user-card
.user-avatar
img src={user.avatar_url} alt='{user.name} avatar'

.user-info
h3 {user.name}
p.user-email {user.email}
p.user-role {user.role}

.user-actions
a.btn.btn-primary href='/users/{user.id}' View Profile
- if user.is_admin
a.btn.btn-secondary href='/admin/users/{user.id}' Admin
103 changes: 71 additions & 32 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "trim-template"
version = "0.1.3"
version = "1.0.0"
description = "A templating engine inspired by Ruby's Slim template engine"
authors = ["David Kelly"]
license = "MIT License"
Expand Down
Loading