Symfony Integration Guide

How to integrate Tplix with Symfony 5, 6, and 7 using the service container and dependency injection.

Symfony · v5 / v6 / v7

Tplix + Symfony

Wire Tplix into the Symfony DI container via a YAML service factory definition.

composer require tplix/tplix
1

Register the Service

Add Tplix to config/services.yaml using the static factory method.

config/services.yaml
services:
    Tplix\Tplix:
        factory: ['Tplix\Tplix', 'getInstance']
        calls:
            - setTemplateDir: ['%kernel.project_dir%/templates/tplix']
            - setCompileDir:  ['%kernel.project_dir%/var/cache/tplix_c']
            - setCacheDir:    ['%kernel.project_dir%/var/cache/tplix']
        public: true
2

Inject into a Controller

src/Controller/HomeController.php
<?php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Tplix\Tplix;

class HomeController extends AbstractController
{
    public function __construct(private Tplix $tplix) {}

    public function index(): Response
    {
        $this->tplix->assign('title', 'Home');
        $this->tplix->assign('user',  $this->getUser());
        return new Response($this->tplix->fetch('home.tpl'));
    }
}