PHP在Smarty中追加儿童模拟

时间:2017-05-18 05:30:35

标签: php smarty

我的目标是在Smarty内部的foreach循环中只插入一次HMTL元素,但插入子元素与foreach循环一样多的循环。使用PHP,工作代码如下所示:

<button onclick={{action 'buttonClicked'}}>Press</button>

但是对于Smarty我坚持这个:

$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);
        }
      }
   }
}

问题是在捕获组中代码也只执行一次。 Smarty版本也是2,而不是3。

1 个答案:

答案 0 :(得分:0)

嗯。答案很简单,我怎么错过了? 将第一个foreach循环置于{capture}

中解决了问题
{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>