我可以在Zend View Helper中使用核心HTML代码吗?如果是,那么我如何在视野中调用该助手?

时间:2012-02-24 06:48:26

标签: php zend-framework

我在视图(.phtml)文件中有一个java脚本定义。此java脚本具有动态参数,如控制器名称和操作名称。为了避免重复代码,我想将此代码放在帮助文件中。

现在我可以在帮助文件中使用纯HTML和javascript代码吗?如果是,那么我如何在我的视图文件中调用该帮助程序?

或者还有其他最佳方式吗?

谢谢...

1 个答案:

答案 0 :(得分:3)

在这种情况下,view helper不是解决方案。您更有可能想要使用partial(这是一个视图助手)。视图助手通常用于return视图的一段数据。 partial()partialLoop()用于在视图中显示常见的html(可在多个视图中重复使用的html片段)。

以下是一个简单视图助手的示例,请注意它返回结果:

<?php

class Zend_View_Helper_FormatDate extends Zend_View_Helper_Abstract
{
    public function FormatDate($date) {

        if ($date != NULL) {
            $formatedDate = new DateTime($date);
            $format = $formatedDate->format('M d, Y');

            return $format;
        } else {
            return '';
        }
    }

}
//This is called in the view like any other function
<?php echo $this->FormatDate($date) ?>

现在部分将包含Html(我很确定JS也能正常工作)

以下是使用视图助手的部分内容

fieldset><legend>Dates and Qualifications</legend>
    <table>
        <tr>
            <td>Birth Date: </td><td><?php echo $this->escape($this->FormatDate($this->bdate)) ?></td>
        </tr>
        <tr>
            <td>Seniority Date: </td><td><?php echo $this->escape($this->FormatDate($this->sendate)) ?></td>
        </tr>
    </table>
    <table>
        <tr>
            <td>I'm a Lead:</td><td><?php echo $this->escape(ucfirst($this->ToBool($this->lead))) ?></td>
        </tr>
        <tr>
            <td>Lead Date:</td><td><?php echo $this->escape($this->FormatDate($this->ldate)) ?></td>
        </tr>
        <tr>
            <td>I'm an Inspector:</td><td><?php echo $this->escape(ucfirst($this->toBool($this->inspector))) ?></td>
        </tr>
        <tr>
            <td>Admin Login:</td><td><?php echo $this->escape(ucfirst($this->toBool($this->admin))) ?></td>
        </tr>
    </table>
</fieldset>

在您的视图脚本中调用此partial,第一个参数是partial的路径 第二个参数是部分

中使用的数据
<?php echo $this->partial('_dates.phtml', $this->memberData) ?>

数据从控制器操作照常发送$this->view->memberData = $memberData
这是常见用法,但不是获取部分数据的唯一方法。

在此示例中,partial位于默认位置/application/views/scripts

希望这有帮助

相关问题