我希望深入了解视图助手和部分助手的主题。我尝试了一个教程的treeview元素,它工作得很好。这是一个很好的例子来尝试我认为的视图助手,但我迷失了。在我的理解中,递归部分应该是部分的?! 有人可以解释如何在不同的帮助器部分中拆分以下功能吗?以及如何在我看来实施?
function treeview($array, $currentParent, $currLevel = 0, $prevLevel = -1) {
foreach ($array as $categoryId => $category) {
if ($currentParent == $category['parent_id']) {
if ($currLevel > $prevLevel) echo " <ol class='tree'> ";
if ($currLevel == $prevLevel) echo " </li> ";
echo '<li> <label for="subfolder2">'.$category['name'].'</label> <input type="checkbox" name="subfolder2"/>';
if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
$currLevel++;
treeview ($array, $categoryId, $currLevel, $prevLevel);
$currLevel--;
}
}
if ($currLevel == $prevLevel) echo " </li> </ol> ";
}
我的观点如下:
require_once('../application/library/Treeview.php');
header("Content-Type: text/html; charset=utf-8");
echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/tree.css');
$this->title = "Treeview Test";
$this->headTitle($this->titel);
$arrayCategories = array();
foreach($this->treeviewitems as $row) :
$arrayCategories[$row['id']] = array("parent_id" => $row['parent_id'], "name" =>$row['name']);
endforeach;
treeview($arrayCategories, 0);
编辑,因为命名和路径问题:
新的观察者头:
class Application_View_Helper_Treeview extends Zend_View_Helper_Abstract
{
public function treeview($array, $currentParent, $currLevel = 0, $prevLevel = -1) {
我的application.ini中的路径(你可以看到我之前尝试过命名的东西)
resources.view.helperPath.Company_View_Helper = "Company/View/Helper"
resources.view.helperPath.Britta_View_Helper = "Britta/View/Helper"
resources.view.helperPath.Application_View_Helper = APPLICATION_PATH "/views/helpers"
resources.view.helperPath.Zend_Dojo_View_Helper = "Zend/Dojo/View/Helper"
现在是保存viewhelper文件的路径:
name:Treeview.php 路径:C:\ wamp \ www \ riba_doc \ application \ views \ helpers
错误:致命错误:在第13行的C:\ wamp \ www \ riba_doc \ application \ views \ scripts \ treeview \ index.phtml中调用未定义的函数treeview()
答案 0 :(得分:1)
您可以采用类似ZF1的方式执行此操作,如下所示。
使用以下内容创建文件./library/My/View/Helper/Treeview.php
<?php
class My_View_Helper_Treeview extends Zend_View_Helper_Abstract
{
public function treeview($array, $currentParent, $currLevel = 0, $prevLevel = -1)
{
foreach ($array as $categoryId => $category) {
if ($currentParent == $category['parent_id']) {
if ($currLevel > $prevLevel) {
echo " <ol class='tree'> ";
}
if ($currLevel == $prevLevel) {
echo " </li> ";
}
echo '<li> <label for="subfolder2">' . $category['name'] . '</label> <input type="checkbox" name="subfolder2"/>';
if ($currLevel > $prevLevel) {
$prevLevel = $currLevel;
}
$currLevel++;
$this->treeview($array, $categoryId, $currLevel, $prevLevel);
$currLevel--;
}
}
if ($currLevel == $prevLevel) {
echo " </li> </ol> ";
}
}
}
接下来,我们需要告诉ZF如何找到我们的Treeview类并将其视为一个视图助手。这是通过将以下内容添加到我们的./application/config/application.ini
文件中来完成的:
resources.view.helperPath.My_View_Helper_ = "My/View/Helper/"
我们可能 - 不确定,奥! - 还必须将My_
命名空间添加到自动加载器:
autoloaderNameSpaces[] = "My_"
最后,我们可以使用以下命令在视图脚本中调用Treeview帮助器:
<? $this->treeview($arrayCategories, 0) ?>