beastbytes/latte-use-extension 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

beastbytes/latte-use-extension

Composer 安装命令:

composer require beastbytes/latte-use-extension

包简介

Latte Use Statement Extension

README 文档

README

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 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

{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 as AliasedClass}

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

Multiple {use} Tags

{use Framework\Module\Aliased\NamespacedClass as 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.

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2025-06-06