hitsi/mezzio-smarty-renderer

Mezzio 的 Smarty 集成

1.0.1 2021-09-05 17:39 UTC

This package is auto-updated.

Last update: 2024-09-06 00:25:43 UTC


README

提供 Smarty 集成给 Mezzio

安装

使用 composer 安装此库

$ composer require hitsi/mezzio-smarty-renderer

Mezzio 团队建议使用依赖注入容器,并使用对 container-interop 的类型提示。我们可以推荐以下实现

配置

您需要在您的 config/config.php 中包含 Hitsi\Mezzio\SmartyRenderer\ConfigProvider。可选配置可以存储在 config/autoload/templates.global.php 中。

'templates' => [
    'extension' => 'file extension used by templates; defaults to tpl',
    'paths' => [
        // namespace / path pairs
    ],
],
'smarty' => [
    'template_dir' => 'default/main templates',
    'cache_dir' => 'path to cached templates',
    'compile_dir' => 'path to compiled templates',
    'config_dir' => 'path to config',
    'assets_url' => 'base URL for assets',
    'assets_version' => 'base version for assets',
    'plugins' => [
        // your own plugins
    ],
    'globals' => [
        // Global variables passed to smarty templates
    ],
    'compile_check' => true, // https://smarty.php.ac.cn/docs/en/variable.compile.check.tpl
    'force_compile' => false, // https://smarty.php.ac.cn/docs/en/variable.force.compile.tpl
    'caching' => false, // https://smarty.php.ac.cn/docs/en/variable.caching.tpl
    'debugging' => false, // https://smarty.php.ac.cn/docs/en/variable.debugging.tpl
],

路径

您可以为模板使用多个路径(命名空间)

'templates' => [
    'extension' => 'file extension used by templates; defaults to tpl',
    'paths' => [
        'app' => __DIR__.'/../templates/app',
        'admin' => __DIR__.'/../templates/admin',
        'layout' => __DIR__.'/../templates/layout',
        ...
    ],
],
'smarty' => [
    'template_dir' => __DIR__.'/../templates',
    ...
]

并渲染它们

$this->template->render('app::index', $params);

$this->template->render('app/index.tpl', $params);

在模板中

{extend file='layout:main.tpl'}

或简短描述

{extend 'layout:main.tpl'}

Smarty 插件

包含的 Smarty 插件增加了 URL 生成支持。

  • path: 渲染给定路由和参数的相对路径。如果没有路由,则返回当前路径。

    {#path route='article_show' id='3'}
    Generates: /article/3
  • url: 渲染给定路由和参数的绝对 URL。如果没有路由,则返回当前 URL。

    {url route='article_show' id='3'}
    Generates: http://example.com/article/3
  • absolute_url: 从给定路径渲染绝对 URL。如果路径为空,则返回当前 URL。

    {absolute_url route='path/to/something'}
    Generates: http://example.com/path/to/something
  • asset 渲染一个(可选带有版本号的)资源 URL。

    {asset route='path/to/asset/name.ext' version='3'}
    Generates: path/to/asset/name.ext?v=3

    获取资源的绝对 URL

    {absolute_url route={asset route='path/to/asset/name.ext' version='3'}}
    Generates: http://example.com/path/to/asset/name.ext?v=3

配置插件

您需要在您的 config/config.php 中包含 Hitsi\Mezzio\SmartyRenderer\Plugins\ConfigProvider。您还可以添加自己的插件,如下所示。

'factories' => [
    Path\To\YourPlugin::class => Path\To\YourPluginFactory::class,
],
'smarty' => [
    'plugins' => [
        'myplugin' => Path\To\YourPlugin::class,
    ],
],

然后使用它们

{myplugin param1="test" param2="done"}