2017 © Pedro Peláez
 

symfony-bundle template-cache-bundle

Symfony template caching functionality.

image

eglobal/template-cache-bundle

Symfony template caching functionality.

  • Tuesday, May 29, 2018
  • by eglobal-it
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3,240 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

EGlobalTemplateCacheBundle

Build Status, (*1)

Installation

Install this bundle using Composer. Add the following to your composer.json:, (*2)

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

Register bundle in the app/AppKernel.php:, (*3)

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

Update config:, (*4)

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, (*5)

<?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, (*6)

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

Add assets to your template, (*7)

...
<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, (*8)

// 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');

The Versions