tomaj/nette-auto-variable-template 问题修复 & 功能扩展

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

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

tomaj/nette-auto-variable-template

最新稳定版本:3.0.0

Composer 安装命令:

composer require tomaj/nette-auto-variable-template

包简介

Auto variable template factory

README 文档

README

Latest Stable Version License

A lightweight Nette Framework library that automatically injects variables into all Latte templates, including component templates. This is useful when you need certain variables (such as application settings, user preferences, or global configuration) to be available across all templates without manually passing them from each presenter or component.

Requirements

  • PHP >= 8.1
  • Nette Application ^3.2
  • Nette Utils ^4.0

Installation

Install the package via Composer:

composer require tomaj/nette-auto-variable-template

Usage

Basic Configuration

Register the custom template factory in your config.neon file. The library wraps the default Latte template factory and automatically assigns your registered variables to every template:

services:
    latte.templateFactory:
        autowired: no

    templateFactory:
        factory: Tomaj\Latte\AutoVariableTemplateFactory(@latte.templateFactory)
        setup:
            - registerVariable('appName', 'My Application')
            - registerVariable('settings', @App\Model\SettingsService)

Using Variables in Templates

Once configured, the registered variables are automatically available in all your Latte templates:

<h1>{$appName}</h1>
<p>Version: {$settings->getVersion()}</p>

Registering Services

You can register any value as a template variable, including services from the DI container:

services:
    templateFactory:
        factory: Tomaj\Latte\AutoVariableTemplateFactory(@latte.templateFactory)
        setup:
            - registerVariable('user', @security.user)
            - registerVariable('translator', @translator)
            - registerVariable('config', @App\Model\ConfigService)

onCreate Event

The factory also provides an onCreate event that fires whenever a template is created, allowing you to perform additional template customization:

$templateFactory->onCreate[] = function (Template $template) {
    // Custom template initialization
};

How It Works

The AutoVariableTemplateFactory implements Nette\Application\UI\TemplateFactory and acts as a decorator around the default template factory. When a template is created:

  1. The inner (original) template factory creates the template
  2. All registered variables are automatically assigned to the template
  3. The onCreate event is triggered
  4. The template is returned ready to use

This approach ensures that variables are available in presenter templates, component templates, and any other templates created through the factory.

License

This library is licensed under the LGPL-2.0-or-later license. See the LICENSE file for details.

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-2.0-or-later
  • 更新时间: 2015-08-28