eglobal/template-cache-bundle
Installation Add the package via Composer:
composer require eglobal/template-cache-bundle:~1.0
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
EGlobal\Bundle\TemplateCacheBundle\EGlobalTemplateCacheBundle::class => ['all' => true],
Configure
Update config/packages/eglobal_template_cache.yaml (Symfony 4+) or app/config/config.yml (Symfony 3):
eglobal_template_cache:
locales: ['en', 'es', 'de']
cache_dir: '%kernel.project_dir%/var/cache/templates'
public_prefix: '/templates'
root_dirs: ['@AcmeDemoBundle/Resources/views']
First Use Case Annotate a controller action to cache its template:
use EGlobal\Bundle\TemplateCacheBundle\Annotation\CacheableTemplate;
use Symfony\Component\Routing\Annotation\Route;
class DemoController
{
/**
* @Route("/cached-page", name="cached_page")
* @CacheableTemplate()
*/
public function cachedPageAction()
{
return $this->render('AcmeDemo:Demo:page.html.twig');
}
}
Run the cache generation command:
php bin/console cache:template
Cache Generation
php bin/console cache:template).post-deploy scripts (e.g., Capistrano, Deployer) or CI/CD pipelines.CacheTemplateCommand programmatically in custom scripts.Template Annotations
@CacheableTemplate to controller actions or classes (caches all actions in the class).@CacheableTemplate(exclude=true).Locale-Specific Caching
en, es) by leveraging the locales config.Assetic Integration
config/packages/assetic.yaml:
assetic:
bundles: [EGlobalTemplateCacheBundle]
Dynamic Cache Invalidation
php bin/console cache:template:clear --route=cached_page --locale=en
$this->get('eglobal_template_cache.cache_manager')->clearRoute('cached_page', 'en');
config/packages/eglobal_template_cache.yaml.TemplateCacheExtension to add custom logic (e.g., dynamic cache keys).cache_dir and public_prefix per environment (e.g., dev, prod).dev environment by setting exposed_routes_only: true and whitelisting routes.Cache Directory Permissions
cache_dir is writable by the web server (e.g., chmod -R 775 var/cache/templates).%kernel.project_dir%/var/cache/templates to align with default cache paths.Route Scope Mismatch
exposed_routes_only: true, only routes marked @CacheableTemplate will cache.php bin/console debug:router to verify route exposure.Locale Configuration
parameters and eglobal_template_cache.locales will cause silent failures.php bin/console debug:config eglobal_template_cache
Template Paths
root_dirs must point to view directories (e.g., @Bundle/Resources/views), not controller paths.@Bundle notation for Symfony’s autoloader resolution.Assetic Conflicts
FrameworkBundle in bundles.php to avoid asset processing issues.Cache Validation:
php bin/console cache:template:validate --route=cached_page --locale=en
Outputs whether a route is cacheable and its cache status.
Log Level:
Set EGLOBAL_TEMPLATE_CACHE_DEBUG: true in .env for verbose logging:
EGLOBAL_TEMPLATE_CACHE_DEBUG=true
Cache Contents:
Inspect generated files in cache_dir (e.g., /web/templates/en/cached_page.html).
Cache Key Customization
Override the cache key logic by extending the CacheKeyGenerator service:
services:
app.template_cache_key_generator:
class: App\Service\CustomCacheKeyGenerator
decorates: eglobal_template_cache.cache_key_generator
arguments: ['@eglobal_template_cache.cache_key_generator.inner']
Partial Caching
For dynamic content in cached templates, use Twig’s {% block %} or embed partials:
{% block dynamic_content %}
{{ include('partials/dynamic.html.twig') }}
{% endblock %}
Performance
Symfony 5+ Compatibility
symfony/framework-bundle:^5.0 and patch if needed.symfony/var-dumper for debugging annotations in newer Symfony versions.Fallback Logic
Combine with Symfony’s built-in cache (e.g., HttpCache) for hybrid caching:
framework:
http_cache:
enabled: true
How can I help you explore Laravel packages today?