增加模板精简版中的值

时间:2012-05-09 08:43:32

标签: php template-lite

我在下面写了代码,

我想要的是当变量为4时,创建一个新的<li>。在下面的代码中,{$ counter}总是1为什么?什么我不能增加它

  <div id="slider">
            <ul>
                <li>
                {foreach from=$books value=book}
                {counter start=0 print=false assign=mycounter}
            { math equation="x + 1" x=$mycounter assign="counter" }


                {$counter}

                     <div class="video2">

                        <div class="thumb">
                            <div class="kitap"><a href="http://cmkzl.nokta.com/kitaplar/{$book.title}"><img src="{$imageUrl}/{$book.image}" width="145" height="205"></a></div>
                            <div class="kitap_baslik"><strong>{$book.title}</strong></div>

                            <div class="alt_baslik"><em>{$book.description|truncate:65:"..."}</em></div>
                            <div class="yorumlar_btn"><a href="b_u_tirmanirken"><img src="{$imageUrl}/yorumlar_btn.jpg" width="145" height="50"></a></div>
                        </div>
                    </div>
                </li>
                {if $counter is div by 4}
                <li>
                    {/if}
            {/foreach}

             </ul>

      </div>

1 个答案:

答案 0 :(得分:0)

我怎么用PHP给你答案

<div id="slider">
<ul>
    <li>
<?php
    $counter = 0;
    foreach( $book in $books)
    { ?>              
         <div class="video2">

            <div class="thumb">
                <div class="kitap"><a href="http://cmkzl.nokta.com/kitaplar/<?= $book.title ?>"><img src="<?=$imageUrl?>/<?=$book.image?>" width="145" height="205"></a></div>
                <div class="kitap_baslik"><strong><?=$book.title?></strong></div>

                <div class="alt_baslik"><em><?= truncateString($book.description,65,true,"...")?></em></div>
                <div class="yorumlar_btn"><a href="b_u_tirmanirken"><img src="<?=$imageUrl?>/yorumlar_btn.jpg" width="145" height="50"></a></div>
            </div>
        </div>

    <?php 
        $counter++;
        if($counter == 4){
            $counter = 0; ?>
            </li>
            <li>
    <?php
        }

    }

 function truncateString($str, $chars, $to_space, $replacement="...") {
   if($chars > strlen($str)) return $str;

   $str = substr($str, 0, $chars);

   $space_pos = strrpos($str, " ");
   if($to_space && $space_pos >= 0) {
      $str = substr($str, 0, strrpos($str, " "));
   }

   return($str . $replacement);
 }

    ?>
    </li>
 </ul>