一键导入
kida-custom-filters
Add custom Kida filters with proper type coercion. Use when adding filters, extending templates, or handling YAML/config int/str.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Add custom Kida filters with proper type coercion. Use when adding filters, extending templates, or handling YAML/config int/str.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Convert Jinja2 templates to Kida syntax. Use when migrating from Jinja2, Django templates, or similar engines.
Use Kida with Chirp for HTMX partials, fragment rendering, block validation. Use when building Chirp web apps.
Write correct Kida templates from scratch. Use when creating templates, writing views, or authoring template content.
Use Kida with Bengal SSG: block caching, incremental builds, template context. Use when building Bengal sites or optimizing build performance.
Understand Kida expression behavior: `+` operator, list vs string, export accumulation. Use when debugging unexpected output or expression behavior.
Use Kida static analysis for pre-render validation, incremental builds, block caching. Use when validating template context, optimizing build times, or implementing caching.
| name | kida-custom-filters |
| description | Add custom Kida filters with proper type coercion. Use when adding filters, extending templates, or handling YAML/config int/str. |
env.add_filter("double", double)
# Or decorator
@env.filter()
def double(value):
return value * 2
@env.filter("twice") # Custom name
def my_double(value):
return value * 2
env.update_filters({"double": lambda x: x * 2, "triple": lambda x: x * 3})
@env.filter()
def truncate_words(value, count=10, end="..."):
words = str(value).split()
return " ".join(words[:count]) + end if len(words) > count else value
{{ text | truncate_words(5) }}
{{ text | truncate_words(10, end="[more]") }}
Values from YAML, config, and cache can arrive as strings. Filters that accept numeric params must coerce at entry to avoid TypeError:
def truncate_chars(text: str, length: int = 120) -> str:
length = int(length) if length is not None else 120
return text[:length] + "..." if len(text) > length else text
Or use a helper:
def coerce_int(value, default: int) -> int:
if value is None:
return default
try:
return int(value)
except (TypeError, ValueError):
return default
@env.filter()
def truncate_words(value, count=30):
count = coerce_int(count, 30)
# ...
For HTML output, return Markup to prevent double-escaping:
from kida import Markup
@env.filter()
def bold(value):
escaped = Markup.escape(str(value))
return Markup(f"<b>{escaped}</b>")
Make filters None-resilient:
@env.filter()
def upper_safe(value):
return "" if value is None else str(value).upper()
"30" not 30