========== Components ========== Components are the core building block of django-palette. Learn how to create and use them effectively. What is a Component? ==================== A component is a reusable piece of UI defined once and rendered multiple times with different data. It's similar to a function in programming - you define it once, then call it with different arguments. **Basic Structure:** .. code-block:: django {% palette_component component_name %} {% palette_block block_name %} Default content {% endpalette_block %} {% endpalette_component %} **Then render it:** .. code-block:: django {% palette_ui file="path/to/template.html" component="component_name" with var1=value1 %} Why Use Components? =================== **Don't Repeat Yourself (DRY)** Define a card once, use it 10 times: .. code-block:: django {% palette_ui file="palette/components/card.html" component="card" with title="Users" value="1,234" %} {% palette_ui file="palette/components/card.html" component="card" with title="Orders" value="567" %} {% palette_ui file="palette/components/card.html" component="card" with title="Revenue" value="$89,000" %} **Consistency** All instances look and behave the same way. **Maintainability** Change the component once, all instances are updated. **Flexibility** Override specific parts when needed without duplicating code. Creating Your First Component ============================= Let's create a simple card component. **Step 1: Create the Template File** Create ``templates/components/card.html``: .. code-block:: django {% load palette %} {% palette_component card %}
{{ description }}
{% endpalette_block %}{{ content }}
{% endpalette_block %}{{ label }}
{% endpalette_block %}{{ content }}
{% endpalette_block %} {% palette_block actions %} {{ button_text }} {% endpalette_block %}Default content
{% endpalette_block %} **4. Use Variables Effectively** Pass all data as variables, don't hardcode: .. code-block:: django {% palette_ui file="palette/components/card.html" component="card" with title=my_title content=my_content %} {% palette_ui file="palette/components/card.html" component="card" with title="Hard coded" %} **5. Keep Components Focused** Each component should do one thing well: .. code-block:: django {% palette_component card %}{{ message }}
{% endpalette_block %}