Skip to content

beastbytes/latte-use-extension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This package is a Latte template engine extension that emulates PHP's use operator 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:

Either:

composer require beastbytes/latte-use-extension

or add the following to the require section of your composer.json

"beastbytes/latte-use-extension": "<version.constraint>"

Configuration

The extension is added to the Latte Engine using the engine's addExtension() method.

$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 at the start of the template.

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

  • There is no as clause when defining an alias
  • Group use definitions are not supported.

{use} Tag

{use Framework\Module\NamespacedClass}

<p>The value is {(new NamespacedClass)->getValue()}</p>
<p>The constant is {NamespacedClass::CONSTANT}</p>

{use} Tag with Alias

{use Framework\Module\Aliased\NamespacedClass AliasedClass}

<p>The value is {(new AliasedClass)->getValue()}</p>
<p>The constant is {AliasedClass::CONSTANT}</p>

Multiple {use} Tags

{use Framework\Module\Aliased\NamespacedClass AliasedClass}
{use Framework\Module\NamespacedClass}

{varType int $arg}
{varType string $testString}

<p>The value is {(new NamespacedClass($arg))->getValue()}</p>
<p>The constant is {NamespacedClass::CONSTANT}</p>
<p>{$testString|replace: AliasedClass::CONSTANT}</p>

License

The BeastBytes Latte Use Extension is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

About

A Latte template engine extension that emulates the PHP use operator

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages