Skip to content

Latest commit

 

History

History
117 lines (83 loc) · 2.97 KB

component-renderer.mdx

File metadata and controls

117 lines (83 loc) · 2.97 KB

ComponentRenderer

ComponentRenderer resolves and renders a component template. It is the engine behind @ctrlfSection, ctrlf_component(), and ctrlfield_render_component().


How templates are resolved

For each component, CtrlField looks for a template in this priority order:

Priority File Engine
1 index.blade.php Blade (via ViewServiceProvider)
2 index.twig Twig (via Timber)
3 index.php Plain PHP with extract()

The template path is resolved once at ComponentRegistry::discover() time and cached in ComponentDefinition::$templatePath.


Data filter pipeline

Before the template receives data, two filters are applied:

$data → ctrlfield/component_data/{key} → ctrlfield/component_data → template

After rendering, one filter is applied:

$html → ctrlfield/component_render/{key} → caller

Static API

ComponentRenderer::render(array $section): string

Renders a flexible-content section. $section must have a '_layout' key matching a registered component. Returns '' if the component is not registered or has no template.

ComponentRenderer::renderByName(string $name, array $data = []): string

Renders a named component with explicit data. Useful outside of a flexible-content loop.


Usage in templates

Blade:

{{-- Renders each section using its registered component template --}}
@foreach(ctrlfield_get('page_builder') as $section)
    @ctrlfSection($section)
@endforeach

{{-- Or render a named component directly --}}
@ctrlfRender('cta', ['headline' => 'Sign Up Today'])

Twig:

{% for section in ctrlfield_get('page_builder') %}
    {{ ctrlf_component(section)|raw }}
{% endfor %}

PHP:

foreach (ctrlfield_get('page_builder') as $section) {
    ctrlfield_render_component($section);
}

// Or with explicit data
ctrlfield_render('cta', ['headline' => 'Sign Up']);

Writing a component template

When CtrlField renders a component, it extracts the section data so all field values are available as local variables:

{{-- components/hero/index.blade.php --}}
<section class="hero theme-{{ $theme ?? 'light' }}">
    <{{ $heading_level ?? 'h1' }}>{{ $headline }}</{{ $heading_level ?? 'h1' }}>
    @if(!empty($subtext))
        <p class="hero-subtext">{{ $subtext }}</p>
    @endif
    @if(!empty($cta['url']))
        <a href="{{ $cta['url'] }}" class="btn">{{ $cta['title'] }}</a>
    @endif
</section>

The _layout key is available as $_layout but is rarely needed in templates.


Optional component files

File Purpose
fields.php Required — defines the FlexLayout and its sub-fields
index.blade.php Blade template
index.twig Twig template
index.php PHP template
functions.php Auto-loaded — register filters, data transforms
script.js Lazy-loaded frontend script
screenshot.png Thumbnail shown in the component picker