ComponentRenderer resolves and renders a component template. It is the engine behind @ctrlfSection, ctrlf_component(), and ctrlfield_render_component().
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.
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
ComponentRenderer::render(array $section): stringRenders 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 = []): stringRenders a named component with explicit data. Useful outside of a flexible-content loop.
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']);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.
| 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 |