Smarty - foreach loop 10 times and stop

后端 未结 8 2301
执念已碎
执念已碎 2021-02-07 12:31

Im using the following Smarty code:

{foreach from=$entries key=i item=topic}
  {if $topic.topic_style == question}
    
  • 8条回答
    •  傲寒
      傲寒 (楼主)
      2021-02-07 13:00

      Small extend in smarty to limit foreach.

      On file : sysplugins/smarty_internal_compile_foreach.php

      Add limit to original attributes :

      public $optional_attributes = array('name', 'key','limit'); 
      

      Add after $output = " this >

      if (isset($_attr['limit'])) {
          $limit = $_attr['limit'];
          $output .= "\n \$_limitCnt = 0; \n \$_limit = $limit; \n";
      }
      

      Add before $output .= "?>"; this >

      if (isset($_attr['limit'])) {
           $output .= "\n if (\$_limitCnt == \$_limit) { break; }";
           $output .= "\n \$_limitCnt++;";
      }
      

      Use as usuall for each and add limit=# to limit your results.

      Hope i helped.

    提交回复
    热议问题