Dark Inspiration Dark Inspiration - 11 days ago 6
HTML Question

Trouble replicating HTML code in a Smarty foreach

I have a block of code I'm trying to replicate using a foreach, but I can't get around on how to do this the code is as follows:

<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">Content</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<table class="table">
<tr>
<td>
<span class="glyphicon glyphicon-pencil text-primary"></span><a href="">Articles</a>
</td>
</tr>
<tr>
<td>
<span class="glyphicon glyphicon-flash text-success"></span><a href="#">News</a>
</td>
</tr>
<tr>
<td>
<span class="glyphicon glyphicon-file text-info"></span><a href="#">Newsletters</a>
</td>
</tr>
<tr>
<td>
<span class="glyphicon glyphicon-comment text-success"></span><a href="#">Comments</a>
<span class="badge">42</span>
</td>
</tr>
</table>
</div>
</div>
</div>


This is part of a collapsible sidebar, and ideally the EG and the AG have a section to themselves

This is the array I'm trying to display on this block

$smarty -> assign('produtosGC1',
array('EG' => array('Tipo ST','Tipo LP','Tipo SG','Hollowbody'),
'AG' => array('Clássicas','Aço')
));


This is my attempt, almost there:

<div class="panel panel-default">

{foreach $produtosGC1 as $produto}
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">{$produto@key}</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<table class="table">
{foreach $produto as $item}
<tr>
<td>
<a href="">{$item}</a>
</td>
</tr>
{/foreach}
</table>
</div>
</div>
{/foreach}
</ul>
</div>


The thing is, the second title (the AG) does not link properly with its sub-categories and doesn't collapse correctly

Edit:changed the question title because it wasn't correct

Answer

After tinkering and thanks to @Darren H's tip I got it working and it's as follows:

{foreach $produtos as $produto}
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapse{$produto@index}">{$produto@key}</a>
      </h4>
    </div>
    <div id="collapse{$produto@index}" class="panel-collapse collapse in">
      <div class="panel-body">
        <table class="table sidebar">
          {foreach $produto as $item}
          <tr>
            <td>
              <a href="{$item}">{$item@key}</a>
            </td>
          </tr>
          {/foreach}
        </table>
      </div>
    </div>
    {/foreach}
Comments