hitsi/mezzio-smarty-renderer

Smarty integration for Mezzio

1.0.1 2021-09-05 17:39 UTC

This package is auto-updated.

Last update: 2024-04-05 23:29:35 UTC


README

Provides Smarty integration for Mezzio.

Installation

Install this library using composer:

$ composer require hitsi/mezzio-smarty-renderer

Mezzio team recommend using a dependency injection container, and typehint against container-interop. We can recommend the following implementations:

Configuration

You need to include the Hitsi\Mezzio\SmartyRenderer\ConfigProvider in your config/config.php. Optional configuration can be stored in 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://www.smarty.net/docs/en/variable.compile.check.tpl
    'force_compile' => false, // https://www.smarty.net/docs/en/variable.force.compile.tpl
    'caching' => false, // https://www.smarty.net/docs/en/variable.caching.tpl
    'debugging' => false, // https://www.smarty.net/docs/en/variable.debugging.tpl
],

Paths

You can use multiple paths (namespaces) for templates

'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',
    ...
]

And render them

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

OR

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

In template

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

OR short description

{extend 'layout:main.tpl'}

Smarty Plugin

The included Smarty plugin adds support for url generation.

  • path: Render the relative path for a given route and parameters. If there is no route, it returns the current path.

    {#path route='article_show' id='3'}
    Generates: /article/3
  • url: Render the absolute url for a given route and parameters. If there is no route, it returns the current url.

    {url route='article_show' id='3'}
    Generates: http://example.com/article/3
  • absolute_url: Render the absolute url from a given path. If the path is empty, it returns the current url.

    {absolute_url route='path/to/something'}
    Generates: http://example.com/path/to/something
  • asset Render an (optionally versioned) asset url.

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

    To get the absolute url for an asset:

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

Configuration Plugin

You need to include the Hitsi\Mezzio\SmartyRenderer\Plugins\ConfigProvider in your config/config.php. Optional you can add you own plugin like this.

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

Then use them

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