{# ================================================================================ Advanced Kida Patterns Showcase (v2) ================================================================================ This file demonstrates the full power of Kida's templating features. Import and use these components as building blocks for complex UIs. FEATURES DEMONSTRATED: - {% def %} Recursive macros for tree traversal - {% embed %} Template composition with block overrides (SEE: card-base.html) - {% cache %} Fragment caching for expensive computations - {% capture %} String building and buffering - {% filter %} Block-level transformations - {% match %} Advanced pattern matching with guards - {% while %} Condition-based loops (NEW in Kida 2.0!) - {% with %} Nil-resilient scope binding - {% unless %} Negative conditionals (cleaner than if not) - {% break %} / {% continue %} Loop control - Range expressions: 1..10, 1..100 by 10 - Pipeline operators (|>) for functional composition KIDA-NATIVE SYNTAX GUIDE: {% let x = 1 %} - Template-scoped variable {% set x = 1 %} - Block-scoped variable (re-assignable) {% export x = 1 %} - Export from inner scope x?.property - Optional chaining (null-safe) x ?? default - Null coalescing value |> filter - Pipeline operator {% end %} - Universal block end (like Go templates) USAGE: {% from 'partials/components/advanced-patterns.html' import tree_view, card_grid, smart_list %} {{ tree_view(site.sections, depth=3) }} ================================================================================ #} {# ============================================================================= RECURSIVE TREE VIEW MACRO Renders hierarchical data (sections, menus, TOC) with arbitrary depth. Demonstrates: {% def %} recursion, {% match %}, {% let %}, nil-resilience ============================================================================= #} {% def tree_view(nodes, depth=3, current_depth=0, node_template='default') %} {% if nodes | length > 0 and current_depth < depth %} {% end %} {% end %} {# ============================================================================= SMART CARD GRID MACRO Data-driven card grid with multiple layout variants and smart sizing. Demonstrates: {% match %}, data-driven rendering, {% cache %} ============================================================================= #} {# Card variant configurations - data-driven for extensibility #} {% let CARD_VARIANTS = { 'compact': {'cols': 4, 'show_desc': false, 'show_meta': false, 'max_title': 40}, 'default': {'cols': 3, 'show_desc': true, 'show_meta': true, 'max_title': 60}, 'feature': {'cols': 2, 'show_desc': true, 'show_meta': true, 'max_title': 80}, 'hero': {'cols': 1, 'show_desc': true, 'show_meta': true, 'max_title': 120} } %} {% def card_grid(items, variant='default', cache_key=none) %} {% let config = CARD_VARIANTS[variant] ?? CARD_VARIANTS.default %} {% let item_list = items ?? [] %} {% if item_list | length > 0 %} {# Use cache if key provided - great for expensive grids #} {% if cache_key %} {% cache cache_key %} {{ _render_card_grid(item_list, config, variant) }} {% end %} {% else %} {{ _render_card_grid(item_list, config, variant) }} {% end %} {% end %} {% end %} {# Internal: Render the actual card grid #} {% def _render_card_grid(items, config, variant) %}
{% for item in items %} {% let card_title = item?.title ?? 'Untitled' %} {% let card_desc = item?.description ?? item?.summary ?? '' %} {% let card_href = item?.href ?? '#' %} {% let card_icon = item?.icon ?? item?.metadata?.icon %} {% let card_date = item?.date %} {% let card_tags = item?.tags ?? [] %}
{# Card header with optional icon #}
{% if card_icon %}
{{ icon(card_icon, size=24) }}
{% end %}

{{ card_title |> truncate(config.max_title) }}

{# Description - conditionally shown #} {% if config.show_desc and card_desc %}

{{ card_desc |> truncate(150) }}

{% end %} {# Metadata row - conditionally shown #} {% if config.show_meta %} {% end %}
{% end %}
{% end %} {# ============================================================================= SMART LIST MACRO Flexible list with grouping, filtering, and sorting capabilities. Demonstrates: {% capture %}, {% filter %}, data-driven patterns ============================================================================= #} {% def smart_list(items, group_by=none, sort_by='weight,title', filter_fn=none, empty_message=none) %} {% let item_list = items ?? [] %} {# Apply filtering if function provided #} {% let filtered_items = item_list %} {# Sort items #} {% let sorted_items = filtered_items |> sort(attribute=sort_by) %} {% if sorted_items | length > 0 %}
{% if group_by %} {# Grouped rendering #} {% for group in sorted_items |> groupby(group_by) %}

{{ group.grouper ?? t('list.ungrouped', default='Other') }}

{{ _render_list_items(group.list) }}
{% end %} {% else %} {# Flat rendering #} {{ _render_list_items(sorted_items) }} {% end %}
{% else %} {# Empty state #}

{{ empty_message ?? t('list.empty', default='No items to display.') }}

{% end %} {% end %} {% def _render_list_items(items) %} {% end %} {# ============================================================================= BREADCRUMB BUILDER MACRO Builds breadcrumbs with smart truncation and overflow handling. Demonstrates: {% capture %}, {% spaceless %}, complex logic ============================================================================= #} {% def breadcrumb_builder(crumbs, max_visible=4, truncate_middle=true) %} {% let crumb_list = crumbs ?? [] %} {% let total_crumbs = crumb_list | length %} {% if total_crumbs > 0 %} {% spaceless %} {% end %} {% end %} {% end %} {% def _render_breadcrumb_item(crumb, is_last) %} {% end %} {# ============================================================================= STATS BADGE ROW MACRO Renders a row of stat badges with smart pluralization. Demonstrates: {% match %} for pluralization, data-driven rendering ============================================================================= #} {# Pluralization rules - extend for other languages #} {% let PLURAL_RULES = { 'page': 'pages', 'post': 'posts', 'section': 'sections', 'module': 'modules', 'class': 'classes', 'function': 'functions', 'command': 'commands', 'endpoint': 'endpoints', 'item': 'items' } %} {% def stats_badges(stats) %} {% let stat_list = stats ?? [] %} {% if stat_list | length > 0 %} {% spaceless %}
{% for stat in stat_list %} {% let value = stat?.value ?? 0 %} {% let label = stat?.label ?? 'item' %} {% let plural_label = PLURAL_RULES[label] ?? (label ~ 's') %} {{ value }} {% match value %} {% case 1 %}{{ label }} {% case _ %}{{ plural_label }} {% end %} {% end %}
{% end %} {% end %} {% end %} {# ============================================================================= CONDITIONAL WRAPPER MACRO Wraps content in a container only if condition is met. Demonstrates: {% caller %} pattern, conditional composition ============================================================================= #} {% def wrap_if(condition, tag='div', css_class='', attrs='') %} {% if condition %} <{{ tag }} class="{{ css_class }}"{{ (' ' ~ attrs) | safe if attrs else '' }}> {{ caller() }} {% else %} {{ caller() }} {% end %} {% end %} {# ============================================================================= RESPONSIVE IMAGE MACRO Generates responsive image with srcset and lazy loading. Demonstrates: {% capture %}, string building, data attributes ============================================================================= #} {% def responsive_image(src, alt='', sizes='100vw', widths=[320, 640, 1024, 1280], loading='lazy') %} {% if src %} {% capture srcset_value %} {% for width in widths %} {{ src }}?w={{ width }} {{ width }}w{% if not loop.last %}, {% end %} {% end %} {% end %} {{ alt }} {% end %} {% end %} {# ============================================================================= LOADING SKELETON MACRO Renders a loading skeleton placeholder. Demonstrates: Simple utility macro, CSS class generation ============================================================================= #} {% def skeleton(type='text', lines=3, width='100%') %} {% match type %} {% case 'card' %}
{% case 'avatar' %}
{% case 'image' %}
{% case _ %}
{% for _ in range(lines) %}
{% end %}
{% end %} {% end %} {# ============================================================================= TOOLTIP WRAPPER MACRO Wraps content in a tooltip container. Demonstrates: {% caller %}, attribute building ============================================================================= #} {% def tooltip(text, position='top') %} {{ caller() }} {% end %} {# ============================================================================= RANGE-BASED PAGINATION HELPER Generates pagination links using Kida range expressions. Demonstrates: Range expressions (1..N), {% match %} for edge cases ============================================================================= #} {% def range_pagination(current, total, base_url='/page/', max_visible=7) %} {% if total > 1 %} {% end %} {% end %} {# ============================================================================= STEP INDICATOR (For Loop with Index Example) Generates a numbered step indicator. Demonstrates: {% for %} with loop.index, {% match %} for state dispatch ============================================================================= #} {% def step_indicator(steps, current_step=1) %} {% let step_count = steps | length %} {% if step_count > 0 %}
    {% for step in steps %} {% let step_num = loop.index %} {% let is_complete = step_num < current_step %} {% let is_current = step_num == current_step %} {% let is_upcoming = step_num > current_step %}
  1. {% match is_complete, is_current %} {% case true, _ %}✓ {% case _, true %}{{ step_num }} {% case _ %}{{ step_num }} {% end %} {{ step?.label ?? step ?? 'Step ' ~ step_num }}
  2. {% end %}
{% end %} {% end %} {# ============================================================================= FILTER BLOCK EXAMPLE Demonstrates: {% filter %} for block-level transformations ============================================================================= #} {% def markdown_card(title) %}

{{ title }}

{% filter markdown %} {{ caller() }} {% end %}
{% end %} {# ============================================================================= CAPTURE + BUILD PATTERN Demonstrates: {% capture %} for complex string building ============================================================================= #} {% def meta_tags(page) %} {% capture og_title %}{{ page?.title ?? 'Untitled' }} | {{ site?.title ?? 'Site' }}{% endcapture %} {% capture og_description %}{{ page?.description ?? page?.excerpt ?? '' |> truncate(160) }}{% endcapture %} {% capture og_url %}{{ site?.baseurl ?? '' }}{{ page?.href ?? '/' }}{% endcapture %} {% end %} {# ============================================================================= EMBED USAGE EXAMPLE Demonstrates: How to use card-base.html with {% embed %} NOTE: {% embed %} blocks can ONLY contain {% block %} overrides. Configuration variables must be set in the parent context before embed. ============================================================================= #} {% def post_card_embed(post) %} {# Set context variables before embed - embed only accepts blocks #} {% let card_href = post?.href ?? '#' %} {% let card_variant = 'feature' if post?.featured else 'default' %} {% embed 'partials/components/card-base.html' with context %} {% block badges %} {% if post?.featured %} Featured {% end %} {% for tag in (post?.tags ?? []) |> take(2) %} {{ tag }} {% end %} {% end %} {% block header %}

{{ post?.title ?? 'Untitled' }}

{% end %} {% block body %} {% with post?.excerpt ?? post?.description as excerpt %}

{{ excerpt |> truncate(150) }}

{% end %} {% end %} {% block footer %} {% with post?.date as date %} {% end %} {% if post?.author %} by {{ post.author }} {% end %} {% end %} {% block actions %} Read more → {% end %} {% end %} {% end %} {# ============================================================================= WHILE LOOP EXAMPLE (Kida 2.0 Moonshot Feature!) Demonstrates: {% while %} condition-based loops with break/continue ============================================================================= #} {% def countdown_widget(start=10, step=1) %} {# While loops are perfect for condition-based iteration #}

Countdown from {{ start }}

🎉 Launch!
{% end %} {# While loop with break condition #} {% def find_first_featured(posts, max_search=100) %} {# Search through posts until we find a featured one #} {% let found = none %} {% let index = 0 %} {% let post_list = posts ?? [] %} {% while index < post_list | length and index < max_search %} {% let post = post_list[index] %} {% if post?.featured %} {% let found = post %} {% break %} {% end %} {% let index = index + 1 %} {% end %} {% if found %} {% else %} {% end %} {% end %} {# While loop with continue for filtering #} {% def filtered_render(items, min_score=0) %} {# Render items that meet minimum score, skip others #} {% end %}