如何将Zend PHTML文件包含到其他PHTML文件中。

3

我正在使用Zend Framework 1

我需要在一个文件中包含另一个文件,并将参数发送到第一个文件。

例如:

我有一个indexController.php文件

并且我在控制器中定义了$numberOfItem

我需要在index.phtml文件中渲染(包含)menu.phtml文件,并向其中发送$numberOfItem变量。

谢谢。

2个回答

7
您可以使用Zend Partial来完成这个操作。
在您的index.phtml中进行如下操作:
echo $this->partial('yourfolder/menu.phtml', array('numberOfItem' => $numberOfItem));

在你的 menu.phtml 文件中,你可以使用 print 命令打印变量。
$this->numberOfItem

在这种情况下,如何将menu.phtml中定义的变量传递到index.phtml中? - Srivishnu

1

这是 Zend partial

在IndexController中,您将像往常一样将numberOfItem值传递给相应的视图。

    $this->view->numberOfItem = $numberOfItem;

然后,在index.phtml中:
    echo $this->partial('viewfolder/menu.phtml', array('numberOfItem' => $this->numberOfItem));

在menu.phtml文件中:
     echo $this->numberOfItem;

viewfolder 在 partial 路径中的位置与相对于 "view/scripts" 的文件夹相同。例如,即使您的 index.phtml 和 menu.phtml 都在同一个文件夹 "application/views/scripts/index" 中,您也需要将路径传递给 partial,如 index/menu.phtml


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接