### Navigate and Install Example App (Bash)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/WARP.md
Commands to navigate to the example application directory, install its dependencies, and notes on running the application.
```bash
# Navigate to example directory
cd example/
# Install example dependencies
pip install -e .
# Run example application (requires additional setup)
# See example/README for full setup instructions
```
--------------------------------
### Install Dependencies (Bash)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/WARP.md
Installs Python dependencies for development and the package itself in editable mode.
```bash
pip install -r requirements.txt
pip install -r requirements-dev.txt
pip install -e .
```
--------------------------------
### Nested Partials Example (HTML/Chameleon)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/WARP.md
An example of a Chameleon template (`.pt` file) that uses `render_partial` to include another partial, demonstrating recursive rendering capabilities.
```html
```
--------------------------------
### Handling PartialsException in Python
Source: https://context7.com/mikeckennedy/chameleon_partials/llms.txt
Illustrates how to catch and handle `PartialsException` raised by the chameleon_partials library. Examples cover scenarios like calling `render_partial` before extension registration, specifying an invalid template folder, or passing a non-dictionary to `extend_model`.
```python
import chameleon_partials
# Raised when render_partial is called before register_extensions
try:
chameleon_partials.render_partial('some/template.pt')
except chameleon_partials.PartialsException as e:
print(e) # "You must call register_extensions() before this function can be used."
# Raised when template_folder is not specified or invalid
try:
chameleon_partials.register_extensions('')
except chameleon_partials.PartialsException as e:
print(e) # "The template_folder must be specified."
try:
chameleon_partials.register_extensions('/nonexistent/path')
except chameleon_partials.PartialsException as e:
print(e) # "The specified template folder must be a folder, it's not: /nonexistent/path"
# Raised when extend_model receives a non-dictionary
try:
chameleon_partials.extend_model(['not', 'a', 'dict'])
except chameleon_partials.PartialsException as e:
print(e) # "The model must be a dictionary."
```
--------------------------------
### Chameleon Composite Partial Template using Another Partial
Source: https://context7.com/mikeckennedy/chameleon_partials/llms.txt
An example of a composite Chameleon partial template that utilizes another partial (`video_image.pt`). This template renders a link to a YouTube video, embeds the video thumbnail using `render_partial`, and displays the author and view count.
```html
```
--------------------------------
### Run Tests (Bash)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/WARP.md
Executes tests using pytest, with options for running all tests, specific tests, or with verbose output.
```bash
# Run all tests
python3 -m pytest tests/
# Run specific test
python3 -m pytest tests/test_rendering.py::test_render_empty
# Run tests with verbose output
python3 -m pytest tests/ -v
```
--------------------------------
### Register Chameleon Partials Extensions (Python)
Source: https://context7.com/mikeckennedy/chameleon_partials/llms.txt
Initializes the chameleon_partials library by registering the template folder and configuring the template loader. This function must be called once at application startup. It supports options for auto-reloading templates during development and caching for production.
```python
from pathlib import Path
import chameleon_partials
# Basic registration with a template folder
template_folder = (Path(__file__).parent / "templates").as_posix()
chameleon_partials.register_extensions(template_folder, auto_reload=True, cache_init=True)
# Parameters:
# - template_folder (str): Path to the templates directory (required)
# - auto_reload (bool): Reload templates on change, useful for development (default: False)
# - cache_init (bool): Cache initialization to prevent re-registration (default: True)
# Full Pyramid application setup example
from pyramid.config import Configurator
def main(global_config, **settings):
"""Returns a Pyramid WSGI application."""
with Configurator(settings=settings) as config:
config.include('pyramid_chameleon')
config.include('.routes')
config.scan()
# Register chameleon_partials with auto-reload for development
folder = (Path(__file__).parent / "templates").as_posix()
chameleon_partials.register_extensions(folder, auto_reload=True, cache_init=True)
return config.make_wsgi_app()
```
--------------------------------
### Chameleon Main Template Using Partials in a Loop
Source: https://context7.com/mikeckennedy/chameleon_partials/llms.txt
A main Chameleon template that demonstrates how to use partials within a loop. It utilizes a layout macro and iterates over a list of 'videos', rendering each video using the `video_square.pt` partial via the `render_partial` function.
```html
All video listing
${ render_partial('shared/partials/video_square.pt', video=v) }
```
--------------------------------
### Code Quality Checks (Bash)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/WARP.md
Utilizes Ruff for checking code style, identifying issues, auto-fixing them, and formatting the code.
```bash
# Check code style and issues
ruff check .
# Auto-fix code style issues
ruff check . --fix
# Format code
ruff format .
```
--------------------------------
### Manual Model Extension for Partials (Python)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/WARP.md
Demonstrates manual extension of the view model to include the `render_partial` function for frameworks other than Pyramid. This is achieved using the `extend_model` helper function.
```python
# For non-Pyramid frameworks
@view_config(route_name='listing')
def listing(request):
videos = video_service.all_videos()
model = dict(videos=videos)
return chameleon_partials.extend_model(model) # Injects render_partial
```
--------------------------------
### Register Chameleon Partials Extension (Python)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/WARP.md
One-time registration of the chameleon_partials extension, typically done during application startup. It sets the template folder and enables features like auto-reloading and caching.
```python
import chameleon_partials
from pathlib import Path
# One-time registration (typically in app startup)
folder = (Path(__file__).parent / "templates").as_posix()
chameleon_partials.register_extensions(folder, auto_reload=True, cache_init=True)
# In templates, use render_partial function
# ${render_partial('shared/partials/video_image.pt', video=video, classes=[])}
```
--------------------------------
### Pyramid BeforeRender Event Subscriber (Python)
Source: https://context7.com/mikeckennedy/chameleon_partials/llms.txt
Integrates `render_partial` into all Pyramid templates automatically by using a `BeforeRender` event subscriber. This approach avoids the need to manually extend each view model with the `render_partial` function.
```python
# views/partials_middleware.py
from pyramid.events import subscriber, BeforeRender
import chameleon_partials
@subscriber(BeforeRender)
def add_global(event):
event['render_partial'] = chameleon_partials.render_partial
```
--------------------------------
### Python View Configuration with Chameleon Partials
Source: https://context7.com/mikeckennedy/chameleon_partials/llms.txt
Demonstrates how to configure a view in a Python web application to use chameleon_partials. The middleware automatically makes the `render_partial` function available in the template context, eliminating the need for explicit `extend_model` in the view function.
```python
from pyramid.view import view_config
@view_config(route_name='index', renderer='templates/home/index.pt')
def index(request):
videos = video_service.top_videos()
return dict(rows=[videos]) # render_partial is automatically available
```
--------------------------------
### Chameleon Partial Template for YouTube Image
Source: https://context7.com/mikeckennedy/chameleon_partials/llms.txt
A simple Chameleon partial template designed to render a YouTube thumbnail. It takes a 'video' object and an optional list of 'classes' as input, constructing an `
` tag with the appropriate source, CSS classes, and alt/title attributes.
```html
```
--------------------------------
### Pyramid Integration for Partials (Python)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/WARP.md
Integrates chameleon_partials into a Pyramid application using a `BeforeRender` event subscriber. This makes the `render_partial` function available globally within templates.
```python
# views/partials_middleware.py
from pyramid.events import subscriber, BeforeRender
import chameleon_partials
@subscriber(BeforeRender)
def add_global(event):
event['render_partial'] = chameleon_partials.render_partial
```
--------------------------------
### View Method Using extend_model (Python)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/README.md
A typical view method that prepares data and extends the model with `render_partial` before returning it. This ensures that the `render_partial` function is accessible within the associated Chameleon template.
```python
@view_config(route_name='listing', renderer='demo_chameleon_partials:templates/home/listing.pt')
def listing(_):
videos = video_service.all_videos()
model = dict(videos=videos)
return chameleon_partials.extend_model(model)
```
--------------------------------
### YouTube Thumbnail Partial Template (HTML)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/README.md
An HTML fragment template for rendering a YouTube video thumbnail. It takes a `video` object and a list of `classes` as input, dynamically constructing the image source and CSS classes.
```html
```
--------------------------------
### Register Chameleon Partials Extension (Python)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/README.md
Registers the chameleon_partials extension with Chameleon for template rendering. It takes a folder path for templates and optional arguments for auto-reloading and cache initialization. This is typically done once at application startup.
```python
from pathlib import Path
import chameleon_partials
def main(_, **settings):
""" This function returns a Pyramid WSGI application.
"""
with Configurator(settings=settings) as config:
config.include('pyramid_chameleon')
config.include('.routes')
config.scan()
# Register the extension for working with Chameleon.
folder = (Path(__file__).parent / "templates").as_posix()
chameleon_partials.register_extensions(folder, auto_reload=True, cache_init=True)
return config.make_wsgi_app()
```
--------------------------------
### Render Partial Template (Python)
Source: https://context7.com/mikeckennedy/chameleon_partials/llms.txt
Renders a specified partial template file, optionally with data passed as keyword arguments. The function makes itself available within the rendered template for nested partial rendering. It returns an HTML object that can be integrated into templates.
```python
import chameleon_partials
# Simple partial rendering without data
html = chameleon_partials.render_partial('render/bare.pt')
print(html.html_text)
# Output: This is bare HTML fragment
# Rendering with data passed as keyword arguments
html = chameleon_partials.render_partial('render/with_data.pt', name='Sarah', age=32)
print(html.html_text)
# Output:
#
This is bare HTML fragment
# Your name is Sarah and age is 32
#
# Nested partial rendering (partials can call other partials)
html = chameleon_partials.render_partial(
'render/recursive.pt',
message='The message is clear',
inner='The message is recursive'
)
# The outer template can call inner templates:
# ${ render_partial('render/recursive_inner.pt', nested_value=inner) }
# Returns an HTML object with __html__() method for template integration
class HTML:
def __init__(self, html_text: str):
self.html_text = html_text
def __html__(self):
return self.html_text
```
--------------------------------
### Extend Model with render_partial (Python)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/README.md
Extends a model dictionary with the `render_partial` function. This is a utility function for frameworks other than Pyramid to ensure the `render_partial` function is available in the template context.
```python
chameleon_partials.extend_model(model)
```
--------------------------------
### Add render_partial to Template Context (Pyramid Middleware - Python)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/README.md
A Pyramid subscriber that adds the `render_partial` function to the template rendering context. This makes the function globally available within Chameleon templates when using the Pyramid framework.
```python
# views/partials_middleware.py
from pyramid.events import subscriber, BeforeRender
import chameleon_partials
@subscriber(BeforeRender)
def add_global(event):
event['render_partial'] = chameleon_partials.render_partial
```
--------------------------------
### Render Partial Template (Chameleon/HTML)
Source: https://github.com/mikeckennedy/chameleon_partials/blob/main/README.md
Renders a partial HTML template within a main Chameleon template. The `render_partial` function takes the template path and any model data as keyword arguments. This allows for modular template design.
```html
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.