CakePHP:在同一视图中两次呈现同一元素时出现问题

CakePHP:在同一视图中两次呈现同一元素时出现问题,cakephp,elements,Cakephp,Elements,我正在使用CakePHP创建一个网站。在我的主页上,我想显示六个最新的视频和六个最流行的视频。我创建了一个名为“itemList”的元素,通过它传递一个类型参数和一个包含项的数组 我对最新视频使用以下代码: $this->element('itemList', array('type' => "video", 'items' => $latestVideos)); echo $this->fetch('itemList'); 以及以下流行视频的代码: $this-&g

我正在使用CakePHP创建一个网站。在我的主页上,我想显示六个最新的视频和六个最流行的视频。我创建了一个名为“itemList”的元素,通过它传递一个类型参数和一个包含项的数组

我对最新视频使用以下代码:

$this->element('itemList', array('type' => "video", 'items' => $latestVideos));

echo $this->fetch('itemList');
以及以下流行视频的代码:

$this->element('itemList', array('type' => "video", 'items' => $popularVideos));

echo $this->fetch('itemList');
最新的视频会以应有的方式显示,但流行视频会先显示最新的视频(第二次),然后再显示流行视频

有人知道我如何“清除”或“取消设置”第一个itemList元素,以便在第二次使用它时从一个空元素开始吗

<?php
/* The following parameters are set when this element is called upon:
 * $type: article, video
 * $items: the list with items to be displayed
 */
$this->start('itemList');
echo $this->Html->script('equalColumns');
$itemCount = 0;
$mainType = $type;
$listType = null;

// If the article list is called, use wider columns css (.articleList)
if ($type == "article") {
    $listType = " articleList";
}
?>
<ul class="otherArticles<?php print($listType); ?>">
    <?php
    foreach ($items as $item) {
        $duration = null;
        $coverImage = null;
        $coverImageOffset = array("x" => 0, "y" => 0);

        /* If a list of tags is submitted, check for every item if the item is an article or a 
         * video
         */
        if ($mainType == "tag") {
            if ($item["TagRelationship"]["article_id"] != null) {
                $newItem["Article"]["id"] = $item["TagRelationship"]["article_id"];
                $newItem["Article"]["slug"] = $item["Article"]["slug"];
                $newItem["Article"]["title"] = $item["Article"]["title"];
                $newItem["Article"]["created"] = $item["Article"]["created"];
                $newItem["User"] = $item["Article"]["User"];
                $newItem["Album"]["Photo"] = $item["Article"]["Album"]["Photo"];
                $item = $newItem;

                $type = "article";
            } elseif ($item["TagRelationship"]["video_id"] != null) {
                $type = "video";
            }
        }

        // If a list with videos is supplied, format the duration
        if ($type == "video") {
            // Set the coverImage
            $coverImage = $this->CoverImage->getYouTubeCover($item["Video"]["youtubeId"]);

            // If a video lasts shorter than an hour, only show minutes/seconds
            if ($item[ucfirst($type)]["duration"] < 3600) {
                $duration = gmdate("i:s", $item[ucfirst($type)]["duration"]);
            }

            // Otherwise show hours as well
            else {
                $duration = gmdate("H:i:s", $item[ucfirst($type)]["duration"]);
            }
        } elseif ($type == "article") {
            $coverImage = $this->CoverImage->getArticleCover($item["Article"]["id"], $item["Album"]["Photo"]);
            $coverImageOffset = $this->CoverImage->getArticleCoverOffset($item["Article"]["id"], $item["Album"]["Photo"]);
        }
        ?>
        <li>
            <a href="/<?php print($type); ?>s/<?php print($item[ucfirst($type)]["id"]); ?>/<?php print($item[ucfirst($type)]["slug"]); ?>">
                <p class="addedDate">Added:
                    <?php
                    print($this->Time->timeAgoInWords($item[ucfirst($type)]["created"], array(
                                'accuracy' => array('minute' => 'minute', 'hour' => 'hour', 'week' => 'week', 'day' => 'day', 'month' => 'month', 'year' => 'year'),
                                'end' => 'never')));

                    if ($type == "article") {
                        ?>
                        by <?php print($item["User"]["username"]); ?>
                        <?php
                    }
                    ?>
                </p>
                <ul class="itemDetails">
                    <li class="thumb" style="background-image: url(<?php print($coverImage); ?>); background-position: <?php print($coverImageOffset["x"]); ?>% <?php print($coverImageOffset["y"]); ?>%">
                        <?php
                        if ($mainType == "tag") {
                            ?>
                            <p class="label"><?php print(ucfirst($type)); ?></p>
                            <?php
                        }

                        if ($type == "video") {
                            ?>
                            <p class="duration"><?php print($duration); ?></p>
                            <?php
                        }
                        ?>
                    </li>
                    <li>
                        <p><?php print($item[ucfirst($type)]["title"]); ?></p>
                    </li>
                </ul>
            </a>
        </li>
        <?php
    }
    ?>
</ul>
<?php
$this->end();
?>


很可能您没有取消设置在最后一个元素中设置的变量,它们在整个视图中都会被记住。尝试在第一次呼叫后取消设置项目-

$this->element('itemList', array('type' => "video", 'items' => $latestVideos));

echo $this->fetch('itemList');

unset($items);

正如评论所说,如果这不起作用,我们需要查看元素本身来诊断问题。如果在处理完所有内容后进入元素本身,请取消设置包含所显示的所有项目的数组。这应该可以解决问题。

经过进一步的研究,我在

果不其然,这就成功了


谢谢你们的帮助,伙计们

这可能是元素本身的问题。你能发布元素代码吗?这里的电话看起来不错。据我所知,没有重置元素的功能,所有元素调用都应该单独进行。是的,我只是猜测你的items变量被一次又一次地添加到其中,你能发布你的元素代码吗?我已经编辑了我的答案,希望它有帮助。仍然不起作用。。。我刚刚为元素中的items数组添加了一个print\r,我发现元素实际上被渲染了三次。首先是最新的视频,第二次是最新的视频,第三次是流行的视频。啊,好吧,是的,在看了元素之后,似乎你的变量应该正确重置,我希望/假设问题已经解决了?只要找出第二个你不想要的是渲染,不,不是。据我所知,第二次提取同时呈现第一个和第二个数组。没有线索知道为什么。。。
// Clear the previous content from the sidebar block.
$this->assign('sidebar', '');