PHPackages                             cloudbase/latte-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. cloudbase/latte-helper

ActiveLibrary[Templating &amp; Views](/categories/templating)

cloudbase/latte-helper
======================

An unofficial helper package for rendering Latte templates in Symfony applications.

1.2.0(6mo ago)017512GPL-3.0-or-laterPHPPHP ^8.3

Since Oct 16Pushed 6mo agoCompare

[ Source](https://github.com/Winning-Software/latte-helper)[ Packagist](https://packagist.org/packages/cloudbase/latte-helper)[ RSS](/packages/cloudbase-latte-helper/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (16)Used By (2)

Latte Helper
============

[](#latte-helper)

[![Version 1.2.0](https://camo.githubusercontent.com/ca0025f692de0b903ddd8c48f0c774183f0b112ac12023c686be684012779a1a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f56657273696f6e2d312e322e302d626c7565)](https://camo.githubusercontent.com/ca0025f692de0b903ddd8c48f0c774183f0b112ac12023c686be684012779a1a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f56657273696f6e2d312e322e302d626c7565)[![License GPL-3.0-or-later](https://camo.githubusercontent.com/1f081152ea1147a9fac48726615f330f4a74ce18c28d10e42bf4f62b9ecb6009/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d47504c2d2d332e302d2d6f722d2d6c617465722d343061646263)](https://camo.githubusercontent.com/1f081152ea1147a9fac48726615f330f4a74ce18c28d10e42bf4f62b9ecb6009/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d47504c2d2d332e302d2d6f722d2d6c617465722d343061646263)[![Coverage 54.55%](https://camo.githubusercontent.com/371351b20f425729f543b43900d6a661bc9a3e1d7c31fbdcfb3b238ac9b2f766/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f7665726167652d35342e35352532352d636239623163)](https://camo.githubusercontent.com/371351b20f425729f543b43900d6a661bc9a3e1d7c31fbdcfb3b238ac9b2f766/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f7665726167652d35342e35352532352d636239623163)

An unofficial helper package for rendering Latte templates in Symfony applications.

🚀 Installation
--------------

[](#-installation)

```
composer require cloudbase/latte-helper
```

Then add the following to the imports section in your `config/services.yaml`:

```
imports:
  - { resource: '../vendor/cloudbase/latte-helper/config/services.yaml' }
```

You'll also need to add some configuration for your Latte controllers:

```
services:
  App\Controller\:
    resource: '../src/Controller'
    tags: [ 'controller.service_arguments' ]
    calls:
      - method: setLatteFactory
        arguments:
          - '@CloudBase\LatteHelper\Classes\Latte\LatteEngineFactory'
```

> You don't need to add this for every single controller, this is just a reference to the directory where your controllers are stored. Add this definition for each directory containing Latte controllers. This does however mean that **every** controller inside that directory is expected to be an extension of `AbstractLatteController` controller.

🧩 Usage
-------

[](#-usage)

This package provides an abstract controller to simplify rendering Latte templates in Symfony. To get started, extend `AbstractLatteController` and use `renderTemplate()`:

```
class IndexController extends AbstractLatteController
{
    #[Route('/', name: 'app_index')]
    public function index(): Response
    {
        return $this->renderTemplate('index.latte', [
            'appName' => 'Test',
        ]);
    }
}
```

By default, templates are loaded from your project’s `/views` directory. You can change this by setting the `$templateDir`property on your controller.

🏗️ Custom Template Directory
----------------------------

[](#️-custom-template-directory)

If you use a different directory, it’s recommended to create a base controller that others extend:

```
class BaseAppController extends AbstractLatteController
{
    // Path relative to your project root
    protected string $templateDir = '/templates/frontend';
}

class AppIndexController extends BaseAppController
{
    #[Route('/', name: 'app_index')]
    public function index(): Response
    {
        // You can omit the .latte suffix if desired
        return $this->renderTemplate('index');
    }
}
```

The example above renders `/templates/frontend/index.latte`.

`$templateDir` should always be relative to your project root.

💡 Template Variables
--------------------

[](#-template-variables)

Every template automatically receives an `$app` variable - an instance of `LatteAwareApplication`.

This provides access to *some* common Symfony features (similar to the `app` variable in Twig):

```
{varType CloudBase\LatteHelper\Classes\LatteAwareApplication $app}

    {$error}

```

⚙️ Using `$app` in Controllers
------------------------------

[](#️-using-app-in-controllers)

You can also access the same application instance within your controller:

```
class IndexController extends AbstractLatteController
{
    #[Route('/', name: 'app_index')]
    public function index(): Response
    {
        $welcomeText = sprintf(
            'Welcome to %s!',
            $this->getApp()->getEnvironmentOption('app_name') ?? 'Your App'
        );

        return $this->renderTemplate('index', [
            'text' => $welcomeText,
        ]);
    }
}
```

🌐 Global Template Data
----------------------

[](#-global-template-data)

Need data available across multiple templates?

Override the `globalData()` method in your base controller:

```
class BaseAppController extends AbstractLatteController
{
    protected string $templateDir = '/templates/frontend';

    protected function globalData(): array
    {
        return array_merge(
            parent::globalData(), // Keeps the $app variable
            [
                'myGlobal' => true,
            ]
        );
    }
}
```

All controllers inheriting from this class will have access to `$myGlobal` in addition to `$app` and local template variables.

🧩 Custom Latte Extensions
-------------------------

[](#-custom-latte-extensions)

To register your own Latte extensions, create a `config/latte.php` file in your Symfony project.

This file should return an array of class names and their constructor arguments:

```
