【问题标题】:example for view and partial helpers视图和部分助手的示例
【发布时间】:2015-10-13 15:40:09
【问题描述】:

我想深入了解视图助手和部分助手的主题。我尝试使用教程的树视图元素,效果很好。这将是尝试使用我认为的视图助手的一个很好的例子,但我迷路了。在我的理解中,递归部分应该是部分的?! 有人可以解释如何在不同的辅助部分中拆分以下功能吗?在我看来如何实现?

    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);

编辑,因为命名和路径问题:

新的viewhelper头:

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 文件的路径:

名称:Treeview.php 路径:C:\wamp\www\riba_doc\application\views\helpers

错误:致命错误:在第 13 行调用 C:\wamp\www\riba_doc\application\views\scripts\treeview\index.phtml 中未定义的函数 treeview()

【问题讨论】:

    标签: zend-framework zend-view


    【解决方案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) ?>
    

    【讨论】:

    • 不,它不起作用,在加载我的旧功能之前,请查看我上面的编辑。命名和路径应该有问题。
    • @pia-sophie:您在视图脚本中的调用实际上是 $this-&gt;treeview() 还是只是 treeview()?在视图脚本的上下文中,$this 是视图对象,因为我们已经通过 resources.view 配置对其进行了配置,所以它知道如何解析类名和路径以找到我们想要调用的方法。
    • 当然你是对的,我什么都没看到;-)
    • 但是现在,我有一个错误:警告:第 11 行 C:\wamp\www\riba_doc\application\views\helpers\Treeview.php 中的非法字符串偏移 'parent_id'
    • 好的,现在我们进入您的特定代码。但希望 ZF 级别的架构——视图助手、自动加载、路径映射器等——应该更清晰一些。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多