Skip to content

eglobal-it/EGlobalTemplateCacheBundle

Repository files navigation

EGlobalTemplateCacheBundle

Build Status

Installation

Install this bundle using Composer. Add the following to your composer.json:

{
    "require": {
        "eglobal/template-cache-bundle": "~1.0"
    }
}

Register bundle in the app/AppKernel.php:

public function registerBundles()
{
    $bundles = [
        // ...
        new EGlobal\Bundle\TemplateCacheBundle\EGlobalTemplateCacheBundle(),
    ];
}

Update config:

parameters:
    locales:
        - en
        - es
        - de

# If using Assetic, add the bundle to the config
assetic:
    bundles:
        - EGlobalTemplateCacheBundle

eglobal_template_cache:
    # Locales to be cached
    locales: "%locales%"
    # Cache only exposed routes
    exposed_routes_only: false
    # Public directory to store cached templates
    cache_dir: '%kernel.root_dir%/../web/templates'
    # Public prefix of cached templates
    public_prefix: '/templates'
    # Directories to search cacheable templates in
    root_dirs:
        - "@AcmeFooBundle/Controller"
        - "@AcmeBarBundle/Controller/Cacheable"

Example usage

Mark controller routes as cacheable

<?php

namespace Acme\FooBundle\Controller;

use EGlobal\Bundle\TemplateCacheBundle\Annotation\CacheableTemplate;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller
{
    /**
     * @Route(methods={"GET"}, path="/my/foo.html", name="my.template.foo", options={"expose"=true})
     * @CacheableTemplate("AcmeFooBundle:Template:foo.html.twig")
     */
    public function fooAction()
    {
        // Your controller logic
    }
    
    /**
     * @Route(methods={"GET"}, path="/my/bar.svg", name="my.template.bar", options={"expose"=true})
     * @CacheableTemplate("AcmeFooBundle:Template:bar.svg.twig")
     */
    public function barAction()
    {
        // Your controller logic
    }
}

Dump templates into cache files

$ php bin/console eglobal:template-cache:dump

Add assets to your template

...
<head>
    <script type="text/javascript" src="{{ asset('bundles/eglobaltemplatecache/js/template-cache.js') }}"></script>
    
    {% if not app.debug %}
        <script type="text/javascript" src="{{ asset(jsTemplateMapFileName(app.request.locale)) }}"></script>
    {% endif %}
</head>
...

Use cacheable template paths in your code

// This will return some path like '/templates/en/f9d15a8be554432de01799a8c51d123f.html'
var fooCachedUrl = TemplateCache.get('my.template.foo');

// This will return some path like '/templates/en/f9d15a8be554432de01799a8c51d123f.svg'
var barCachedUrl = TemplateCache.get('my.template.bar');