https://github.com/beastbytes/latte-use-extension
A Latte template engine extension that emulates the PHP use operator
https://github.com/beastbytes/latte-use-extension
Last synced: about 1 month ago
JSON representation
A Latte template engine extension that emulates the PHP use operator
- Host: GitHub
- URL: https://github.com/beastbytes/latte-use-extension
- Owner: beastbytes
- License: other
- Created: 2025-06-06T18:04:53.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2025-12-07T15:43:29.000Z (4 months ago)
- Last Synced: 2025-12-21T20:21:53.398Z (4 months ago)
- Language: PHP
- Size: 21.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
This package is a [Latte](https://latte.nette.org/) template engine extension that emulates
[PHP's `use` operator](https://www.php.net/manual/en/language.namespaces.importing.php)
and allows writing cleaner templates.
By default, Latte templates require the use of Fully Qualified CLass Names (FQCN); this can lead to cluttered templates.
The extension defines the `{use}` tag that allows templates to define the FQCN and optionally an alias,
and refer to the _used_ class by the alias if defined, or the base class name if not.
## Requirements
- PHP 8.1 or higher.
- Latte
## Installation
Install the package using [Composer](https://getcomposer.org):
Either:
```shell
composer require beastbytes/latte-use-extension
```
or add the following to the `require` section of your `composer.json`
```json
"beastbytes/latte-use-extension": ""
```
## Configuration
The extension is added to the Latte Engine using the engine's `addExtension()` method.
```php
$engine = new Engine();
$engine->addExtension(new UseExtension());
```
## Usage
The FQCN must be defined in a `{use}` tag before the base class name or alias is referenced in the template.
The best way to ensure this is to place `{use}` tags as near to the start of the template as possible.
The extension replaces the alias or base class name defined in the `use` tag with the FQCN in class instantation (`new`)
statements and class constants during compilation; it _does not_ import or alias the class.
#### Differences from PHP
* Group `use` definitions are _not_ supported.
#### {use} Tag
```latte
{use Framework\Module\NamespacedClass}
The value is {(new NamespacedClass)->getValue()}
The constant is {NamespacedClass::CONSTANT}
```
#### {use} Tag with Alias
```latte
{use Framework\Module\Aliased\NamespacedClass as AliasedClass}
The value is {(new AliasedClass)->getValue()}
The constant is {AliasedClass::CONSTANT}
```
#### Multiple {use} Tags
```latte
{use Framework\Module\Aliased\NamespacedClass as AliasedClass}
{use Framework\Module\NamespacedClass}
{varType int $arg}
{varType string $testString}
The value is {(new NamespacedClass($arg))->getValue()}
The constant is {NamespacedClass::CONSTANT}
{$testString|replace: AliasedClass::CONSTANT}
```
## License
The BeastBytes Latte Use Extension is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.