CakePHP:在同一视图中两次渲染相同的元素时遇到问题

时间:2015-01-09 18:06:13

标签: cakephp elements

我正在使用CakePHP创建一个网站。在我的主页上,我想显示六个最新视频和六个最受欢迎的视频。我创建了一个名为'itemList'的元素,我传递了一个类型参数和一个带有items的数组。

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

$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();
?>

2 个答案:

答案 0 :(得分:1)

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

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

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

unset($items);

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

答案 1 :(得分:0)

经过一些研究后,我在CakePHP Cookbook中发现了以下内容:

// Clear the previous content from the sidebar block.
$this->assign('sidebar', '');

果然这就行了。

感谢你们的帮助!