2017-05-18 1 views
0

내 목표는 Smarty 내부의 foreach 루프에 한 번만 HMTL 요소를 삽입하는 것이지만 foreach 루프가 가진만큼 많은 자식 요소를 삽입합니다. PHP로 작업 코드는 다음과 같습니다Smarty의 PHP appendChild 아날로그

$counter1 = 0; 
foreach ($secCont->getContent() as $row) { 
if ($row->getType() == "head") { 
    $counter1++; 
    if ($counter1 == 1) { 
     $theadNode = $html->createElement("thead"); 
     $tableNode->appendChild($theadNode); 
    } 
    $tr = $html->createElement("tr"); 
    $theadNode->appendChild($tr); 
    foreach ($row->getContent() as $cell) { 
     $thElement = $html->createElement("th"); 
     $thElement->setAttribute("colspan", $cell->getColspan()); 
     $thElement->setAttribute("rowspan", $cell->getRowspan()); 
     $tr->appendChild($thElement); 
     foreach ($cell->getContent() as $parInCell) { 
      self::paragraphWriting($html, $parInCell, $thElement); 
     } 
     } 
    } 
} 

그러나 스마티의 경우 I이 붙어있다 :

{assign var="counter1" value=0} 
{foreach from=$secCont->getContent() item=row} 
    {if $row->getType() == "head"} 

     {** capture group for adding inside thead*} 
     {capture name="insideTableHead"} 
      <tr> 
       {foreach from=$row->getContent() item=cell} 
        <th colspan="{$cell->getColspan()}" rowspan="{$cell->getRowspan}"> 
         {foreach from=$cell->getContent() item=parCont} 
          {include file="`$path_template`/paragraph.tpl"} 
         {/foreach} 
        </th> 
       {/foreach} 
      </tr> 
     {/capture} 

     {** we need thead only once if exists *} 
     {assign var="counter1" value=$counter1+1} 
     {if $counter1 == 1} 
      <thead> 
      {$smarty.capture.insideTableHead} 
      </thead> 
     {/if} 
{/foreach} 

문제는 캡처 그룹의 코드는 한 번만 실행된다는 점이다. 또한 Smarty 버전은 3이 아니라 2입니다.

+0

나는 당신이 {> $ 로우 - 경우 getType로() == "머리"}에 대한 귀하의 경우 조건을 끝낼 생각합니다. 캡쳐 그룹에 도움이 될지도 모릅니다. –

+0

여기에 코드를 복사하는 동안 기계적 실수 만합니다. 문제는이 캡처 그룹이 한 번만 실행된다는 것입니다. 그리고 각 foreach 루프에 추가하고 싶습니다. – Vitaliy

답변

0

흠. 그 대답은 너무 간단해서 내가 어떻게 그것을 놓쳤는가? {capture} 내부에 첫 번째 foreach 루프를 배치하여 해결 문제 :

{capture name="insideTableHead"} 
    {foreach from=$secCont->getContent() item=row} 
     {if $row->getType() == "head"} 
      <tr> 
       {foreach from=$row->getContent() item=cell} 
        <th colspan="{$cell->getColspan()}" rowspan="{$cell->getRowspan}"> 
         {foreach from=$cell->getContent() item=parCont} 
          {include file="`$path_template`/paragraph.tpl"} 
         {/foreach} 
        </th> 
       {/foreach} 
      </tr> 
     {/if} 
    {/foreach} 
{/capture} 

{** we need thead only once if exists *} 

<thead> 
{$smarty.capture.insideTableHead} 
</thead>