xorock/zend-expressive-phptalrenderer

Expressive 的 PHPTAL 集成

0.1.1 2017-02-05 08:21 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:03:58 UTC


README

提供 PHPTAL 集成,用于 Expressive

安装

使用 composer 安装此库

$ composer require xorock/zend-expressive-phptalrenderer

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

配置

以下为 PHPTAL 特定的详细配置,由 PhptalRendererFactory 使用

use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Expressive\Phptal\HelperManager;
use Zend\Expressive\Phptal\Helper;
use Zend\Expressive\Phptal\PhptalEngineFactory;
use PHPTAL as PhptalEngine;

return [
    'dependencies' => [
        'factories' => [
            'Zend\Expressive\FinalHandler' =>
                Zend\Expressive\Container\TemplatedErrorHandlerFactory::class,
            
            Zend\Expressive\Template\TemplateRendererInterface::class =>
                Zend\Expressive\Phptal\PhptalRendererFactory::class,
            PhptalEngine::class => PhptalEngineFactory::class,

            HelperManager::class => InvokableFactory::class,
            Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
            Helper\ServerUrlHelper::class => Helper\ServerUrlHelperFactory::class,
        ],
    ],

    // if enabled, forces to reparse templates every time
    'debug' => boolean,
    
    'templates' => [
        'extension' => 'file extension used by templates; defaults to html',
        'paths' => [
            // Paths may be strings or arrays of string paths.
        ],
        'paths' => 'templates' // Defaults to `templates` directory
    ],

    'phptal' => [
        'cache_dir' => 'path to cached templates',
        // if enabled, delete all template cache files before processing
        'cache_purge_mode' => boolean,
        // set how long compiled templates and phptal:cache files are kept; in days 
        'cache_lifetime' => 30,
        'encoding' => 'set input and ouput encoding; defaults to UTF-8',
        // one of the predefined constants: PHPTAL::HTML5,  PHPTAL::XML, PHPTAL::XHTML
        'output_mode' => PhptalEngine::HTML5,
        // set whitespace compression mode
        'compress_whitespace' => boolean,
        // strip all html comments
        'strip_comments' => boolean,
        'helpers' => [
            // helper service names or instances
        ]
    ],
];

包含的助手和函数

包含的 HelperManager 添加了对在模板中调用自定义函数的支持,通过将内置的 helper 自定义表达式修改器代理到用户类。用户类必须实现 HelperInterface__invoke() 方法。

如果容器中注册了 UrlHelper 和 ServerUrlHelper,以下模板助手将自动激活

  • url: UrlHelper 的快捷方式

    <a tal:attributes="href helper:url('article_show', ['id' => 3])">Link</a>
    Generates: /article/3
  • serverurl: ServerUrlHelper 的快捷方式

    <a tal:attributes="href helper:serverurl('/foo')">Link</a>
    Generates: /foo

例如,我们可以基于 DateTime 对象创建自己的助手

use DateTime;
use Zend\Expressive\Phptal\Helper\HelperInterface;

class DateTimeHelper implements HelperInterface
{
    const HELPER_NAME = 'datetime';
    
    public function __invoke(DateTime $datetime = null)
    {
        if ($datetime === null) {
            $datetime = new DateTime();
        }
        return $datetime->format(DateTime::ISO8601);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getHelperName()
    {
        return self::HELPER_NAME;
    }
}

现在我们需要将其传递给配置数组

'dependencies' => [
    'aliases' => [
        'dateTimeHelper' => DateTimeHelper::class,
    ],
    'factories' => [
        DateTimeHelper::class => DateTimeHelperFactory::class,
    ],
],

'phptal' => [
    'helpers' => [
        DateTimeHelper::class, // or 'dateTimeHelper' alias
    ]
]

然后,将 SomeAction 中的新 DateTime 传递给我们的模板

$date = new \DateTime();
$data['date'] = $date;
$this->template->render('app::home-page', $data)

然后在模板内部

${helper:datetime(date)}
Will show current date in ISO 8601 format