0

我需要一些帮助来解决这个问题,我完全停电了,无法弄清楚我做错了什么。

我使用 TemplatePower 作为模板引擎。我想像这样显示我的新闻档案:

23-09-2013
> Update nieuwssectie
21-09-2013
> Nog toe te voegen
20-09-2013
> Nieuwe functie nieuws
> Eerste nieuwsbericht

但这就是我得到的

23-09-2013
21-09-2013
20-09-2013
> Update nieuwssectie
> Nog toe te voegen
> Nieuwe functie nieuws
> Eerste nieuwsbericht

这是模板:

<!-- START BLOCK : newsArchiv -->
<h6 class="newsItem">Ons nieuwsarchief:</h6>
<ul class="newsArchiv">
    <!-- START BLOCK : newsArchivDateRow -->
    <li class="newsArchivRow"><span>{date}</span></li>
    <!-- END BLOCK : newsArchivDateRow -->
    <!-- START BLOCK : newsArchivItemRow -->
    <li><a href="pagina/nieuws/{url}/">{title}</a></li>
    <!-- END BLOCK : newsArchivItemRow -->
</ul>
<!-- END BLOCK : newsArchiv -->

和 PHP

<?
$tpl2->newBlock( 'newsArchiv');
$item_sql = $sql->query( "SELECT * FROM news ORDER BY id DESC" );
if( $sql->num( $item_sql ) > 0 )
{
    $date1 = "";
    while( $item = $sql->fetch( $item_sql ) )
    {
        $date2 = date( "d-m-Y", $item['date'] );
        if( $date2 != $date1 )
        {
            $tpl2->newBlock( 'newsArchivDateRow');
            $tpl2->assign( "date", $date2 );
            $date1 = $date2;
        }
        $tpl2->newBlock( 'newsArchivItemRow');
        $tpl2->assign( array(
            "url" => $item['url'],
            "title" => $item['title']
        ));
    }
}
?>

我只知道有一个逻辑解决方案,我只是看不到它,需要朝着正确的方向推动。

[编辑]

根据 Bart 的回答,我构建了这样的代码并解决了问题。

模板:

<!-- START BLOCK : newsArchiv -->
<h6 class="newsItem">Ons nieuwsarchief:</h6>
<ul class="newsArchiv">
    <!-- START BLOCK : newsArchivRow -->
    <li class="newsArchivRow"><span>{date}</span></li>
    <!-- START BLOCK : newsArchivItemRow -->
    <li><a href="pagina/nieuws/{url}/">{title}</a></li>
    <!-- END BLOCK : newsArchivItemRow -->
    <!-- END BLOCK : newsArchivRow -->
</ul>
<!-- END BLOCK : newsArchiv -->

PHP:

<?php
$tpl2->newBlock( 'newsArchiv');
$item_sql = $sql->query( "SELECT * FROM news ORDER BY id DESC" );
if( $sql->num( $item_sql ) > 0 )
{
    $itemsArr = array();
    while( $itemdata = $sql->fetch( $item_sql ) )
        $itemsArr[ date( "d-m-Y", $itemdata['date'] ) ][] = array( "url"=> $itemdata['url'], "title" => $itemdata['url'] );

    foreach( $itemsArr as $date => $items )
    {
        $tpl2->newBlock( 'newsArchivRow');
        $tpl2->assign( "date", $date );
        foreach( $items as $item )
        {
            $tpl2->newBlock( 'newsArchivItemRow');
            $tpl2->assign( array(
                "url" => $item['url'],
                "title" => $item['title']
                ));
        }
    }
}
?>
4

1 回答 1

0

我根本不知道 TemplatePower,但看起来模板引擎一次获取所有具有给定名称的块并呈现它们。如果这是真的,您可能需要在一个块中构建特定日期的完整档案。

于 2013-09-23T06:38:36.483 回答