hipnaba/indigo-view

Zend View 扩展

1.0.0 2018-02-04 11:56 UTC

This package is auto-updated.

Last update: 2024-09-09 01:55:53 UTC


README

Indigo View 扩展了 Zend View。它简化了与非 ZF 项目集成的过程,并提供了一套通用的视图助手。

视图助手

缩进

缩进多行字符串。有助于生成美观的输出。

<?php
$content = <<< EOS
Line 1
EOS;

$content1 = <<< EOS
Line 1.1
Line 1.2
EOS;

echo '<div>' . PHP_EOL 
        . ($this->indent($content)) . PHP_EOL
            . ($this->indent($content1, 2)) . PHP_EOL
    . '</div>';

上面的示例将输出...

<div>
    Line 1
        Line 1.1
        Line 1.2
</div>

渲染对象

尝试渲染一个对象。这主要用于渲染实现了 HelperPluginAwareInterface 的对象。

如果传入的对象没有实现 HelperPluginAwareInterface,助手将尝试调用对象的 __toString() 方法。如果该方法不存在,它将只返回对象的类。

实现 HelperPluginAwareInterface 很简单。只需让实现的对象返回所需的助手插件。助手插件可以是任何可调用的。

<?php
class RenderableObject implements \Indigo\View\HelperPluginAwareInterface
{
    public function getHelperPlugin()
     {
        return function ($object) {
            return '<h1>' . get_class($object) . '</h1>';
        };
    }
}

$object = new RenderableObject();

echo $this->renderObject($object);

上面的示例将输出

<h1>RenderableObject</h1>

或者我们可以使用已注册在插件管理器中的插件名称。

<?php
class RenderableObject implements \Indigo\View\HelperPluginAwareInterface
{
    public function getHelperPlugin() 
    {
        return 'pluginName';
    }
}