STCMS Documentation
Complete guide to building modern, secure React/PHP applications with STCMS framework
Twig Functional Components
Create powerful, reusable UI elements with Twig macros
In STCMS, you can create powerful, reusable, and purely presentational UI elements using Twig's macro feature. We call this pattern "Functional Components."
1 Create a Component Macro
Define your components in components/components.twig. Here's a button component:
{# components/components.twig #}
{% macro button(text, url, variant='primary', extra_classes='') %}
{% set base_classes = 'inline-block text-white font-bold py-2 px-4 rounded-lg shadow-md ...' %}
{% set variant_classes = {
'primary': 'bg-red-600 hover:bg-red-700',
'secondary': 'bg-gray-700 hover:bg-gray-800'
} %}
<a href="{{ url }}" class="{{ base_classes }} {{ variant_classes[variant] }} {{ extra_classes }}">
{{ text }}
</a>
{% endmacro %}
info: Macros accept parameters like functions, with optional default values.
2 Import and Use the Component
Import the components file at the top of your page:
{% import "components.twig" as components %}
{# Now use the macro like a function #}
{{ components.button('Click Me', '/url', 'primary') }}
Live Button Examples
3 Advanced Example: Card Component
Create more complex components like cards:
{% macro card(title, description, image_url=null) %}
<div class="bg-white border border-gray-200 rounded-lg shadow-lg overflow-hidden">
{% if image_url %}
<img class="w-full h-48 object-cover" src="{{ image_url }}" alt="{{ title }}">
{% endif %}
<div class="p-6">
<h3 class="text-2xl font-bold text-gray-800 mb-2">{{ title }}</h3>
<p class="text-gray-600">{{ description }}</p>
</div>
</div>
{% endmacro %}
Live Card Examples
Reusable Components
Creating components directly in Twig is a great way to keep your templates clean and maintainable for presentational UI.
Powered by Tailwind CSS
All components are styled with utility classes, making them easy to customize without ever leaving your HTML.
Available Components
STCMS comes with many pre-built components in the /components folder:
Tip: Check the README.md for complete documentation of all available components.