c33s/static-page-content-bundle
Installation:
composer require c33s/static-page-content-bundle
Add to config/bundles.php:
return [
// ...
C33s\StaticPageContentBundle\C33sStaticPageContentBundle::class => ['all' => true],
];
Configuration:
Define static pages in config/packages/c33s_static_page_content.yaml:
c33s_static_page_content:
pages:
about:
template: 'pages/about.html.twig'
route: 'about_page'
contact:
template: 'pages/contact.html.twig'
route: 'contact_page'
First Use Case:
Create a Twig template at templates/pages/about.html.twig:
<h1>About Us</h1>
<p>Static content here.</p>
Access via route about_page (auto-generated by the bundle).
config/packages/c33s_static_page_content.yaml (primary config file).templates/pages/ (default directory for Twig templates).Resources/doc/index.md (for advanced usage).Page Creation:
c33s_static_page_content.pages config./about).Dynamic Content Injection: Pass variables to Twig templates via config:
pages:
about:
template: 'pages/about.html.twig'
route: 'about_page'
vars:
title: 'About Us'
subtitle: 'Our Story'
Use in Twig:
<h1>{{ title }}</h1>
<p>{{ subtitle }}</p>
Route Customization:
Override auto-generated routes by defining a custom route in routes.yaml:
about_page:
path: /our-company
controller: C33s\StaticPageContentBundle\Controller\StaticPageController::renderAction
defaults:
page: 'about'
Caching:
Enable Twig cache for static pages in config/packages/twig.yaml:
twig:
cache: '%kernel.cache_dir%/twig'
%kernel.environment% in template paths:
pages:
home:
template: 'pages/home_%kernel.environment%.html.twig'
StaticPageManager service in controllers:
use C33s\StaticPageContentBundle\Manager\StaticPageManager;
public function __construct(StaticPageManager $staticPageManager) {
$this->staticPageManager = $staticPageManager;
}
Unmaintained Package:
renderWithoutController (docs).Route Conflicts:
/about) may clash with existing routes.routes.yaml or use _path helpers.Template Not Found:
templates/pages/ by default).Configuration Overrides:
# config/packages/c33s_static_page_content.yaml
c33s_static_page_content:
pages:
about:
template: 'custom/path/about.html.twig' # Override
Check Config: Dump the loaded config to verify settings:
use Symfony\Component\DependencyInjection\ContainerInterface;
public function __construct(ContainerInterface $container) {
dump($container->getParameter('c33s_static_page_content.pages'));
}
Route Debugging: Use Symfony’s route debugger or CLI:
php bin/console debug:router | grep about_page
Twig Errors:
Enable Twig debug mode in config/packages/dev/twig.yaml:
twig:
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
Custom Template Locations: Override the template directory via config:
c33s_static_page_content:
template_dir: '%kernel.project_dir%/templates/custom_static_pages'
Pre-Rendering Logic:
Extend the StaticPageManager to add logic before rendering:
// src/Service/CustomStaticPageManager.php
use C33s\StaticPageContentBundle\Manager\StaticPageManager;
class CustomStaticPageManager extends StaticPageManager {
public function getContent(string $pageName) {
$content = parent::getContent($pageName);
// Add custom logic (e.g., modify vars)
return $content;
}
}
Register as a service in config/services.yaml:
services:
C33s\StaticPageContentBundle\Manager\StaticPageManager:
alias: 'custom_static_page_manager'
public: true
Event Listeners:
Listen to the static_page.render event to modify content dynamically:
use C33s\StaticPageContentBundle\Event\StaticPageEvent;
use Symfony\Component\EventDispatcher\GenericEvent;
public function onStaticPageRender(StaticPageEvent $event) {
$event->setContent($event->getContent() . '<div>Custom footer</div>');
}
Register the listener in config/services.yaml:
services:
App\EventListener\StaticPageListener:
tags:
- { name: 'kernel.event_listener', event: 'static_page.render', method: 'onStaticPageRender' }
Disable Debug Mode:
Set kernel.debug: false in config/packages/dev/twig.yaml for production to leverage Twig caching.
HTTP Caching: Add cache headers in a base Twig template:
{% cache app.static_page('about') %}
{# Static content here #}
{% endcache %}
Asset Optimization: Use Symfony’s asset mapper for static assets in templates:
<img src="{{ asset('images/logo.png') }}" alt="Logo">
How can I help you explore Laravel packages today?