Installation
composer require a-proud/twiew-bundle:dev-main
Ensure TwigBundle is installed (dependency) and registered in config/bundles.php.
Configure Twig Paths
Add the Twiew template path to config/packages/twig.yaml:
twig:
paths:
"%kernel.project_dir%/vendor/a-proud/twiew-bundle/templates": twiew
First Use Case: Render a Basic Page
Define a YAML config (e.g., config/twiew/page.yml):
default:
css: ["css/main.css"]
js_top: []
header: { tpl: "header.twig", blocks: [] }
main: { tpl: "main.twig", blocks: [{ tpl: "content_block.twig", cols: 12 }] }
footer: { tpl: "footer.twig", blocks: [] }
js_bottom: ["js/script.js"]
Render it in a controller:
use AProud\TwiewBundle\Renderer\TwiewRenderer;
public function renderPage(TwiewRenderer $renderer)
{
$config = yaml_parse_file(__DIR__.'/../../config/twiew/page.yml');
return $renderer->render($config['default']);
}
Modular Configuration
Split page logic into reusable YAML files (e.g., header.yml, footer.yml) and merge them:
$config = array_merge(
yaml_parse_file('config/twiew/header.yml'),
yaml_parse_file('config/twiew/main.yml')
);
Dynamic Content Injection
Pass variables to templates via data key in YAML:
main:
tpl: "dashboard.twig"
blocks: [{ tpl: "stats_block.twig", data: { stats: $stats } }]
Access in Twig:
{% for stat in stats %}
{{ stat.value }}
{% endfor %}
Layout Inheritance
Extend base templates (e.g., base.twig) and override sections:
extends: "base.twig"
header: { tpl: "custom_header.twig" }
Asset Management
Use css/js_top/js_bottom for global assets. For dynamic scripts:
js_bottom:
- "js/vendor/jquery.js"
- { src: "js/app.js", defer: true }
Component Reusability
Create shared blocks (e.g., sidebar.twig) and reuse across pages:
main:
blocks:
- { tpl: "sidebar.twig", cols: 3 }
- { tpl: "content.twig", cols: 9 }
Template Path Resolution
paths in twig.yaml is misconfigured.vendor/a-proud/twiew-bundle/templates and clear cache:
php bin/console cache:clear
YAML Parsing Quirks
yaml_parse_file() with error handling:
$config = yaml_parse_file('config.yml') ?: throw new \RuntimeException('Invalid YAML');
Block Column Constraints
cols exceeding 12 (default grid) may render incorrectly.cols: 6 + cols: 6 = 12).Circular Dependencies
extends: "nonexistent.twig").extends: "base.twig" as a fallback and validate template existence.Asset Loading Order
js_bottom before js_top).priority keys in config.Template Dumping
Enable Twig debug mode in config/packages/dev/twig.yaml:
twig:
debug: true
strict_variables: true
Access debug toolbar at /_profiler.
Config Validation Add runtime checks for mandatory keys:
$mandatory = ['css', 'js_top', 'header', 'main', 'footer', 'js_bottom'];
foreach ($mandatory as $key) {
if (!isset($config[$key])) {
throw new \InvalidArgumentException("Missing mandatory key: {$key}");
}
}
Extension Points
templates/twiew/ (higher priority than vendor).twiew.render):
// src/EventListener/TwiewListener.php
public function onRender(TwiewEvent $event) {
$event->getConfig()->set('custom_key', 'value');
}
Register in services.yaml:
services:
App\EventListener\TwiewListener:
tags:
- { name: kernel.event_listener, event: twiew.render, method: onRender }
Performance
$renderer->render($config, ['cache_key' => 'homepage']);
asset() function with versioning for JS/CSS:
js_bottom:
- { src: "{{ asset('js/app.js', 'sha1') }}" }
How can I help you explore Laravel packages today?