12 апреля 2018

В данном примере массив данных smarty для брендов разбивается на блоки по алфавиту. Поддерживает как английский, так и кириллицу. Данное решение можно использовать практически для любого массива smarty.

html

{$chars = ['А' => [],'Б' => [],'В' => [],'Г' => [],'Д' => [],'Е' => [],'Ё' => [],'Ж' => [],'З' => [],'И' => [],'Й' => [],'К' => [],'Л' => [],'М' => [],'Н' => [],'О' => [],'П' => [],'Р' => [],'С' => [],'Т' => [],'У' => [],'Ф' => [],'Х' => [],'Ц' => [],'Ч' => [],'Ш' => [],'Щ' => [],'Ъ' => [],'Ы' => [],'Ь' => [],'Э' => [],'Ю' => [], 
'Я' => [],'A' => [],'B' => [],'C' => [],'D' => [],'E' => [],'F' => [],'G' => [],'H' => [],'I' => [],'G' => [],'K' => [],'L' => [],'M' => [],'N' => [],'O' => [],'P' => [],'Q' => [],'R' => [],'S' => [],'T' => [],'U' => [],'V' => [],'W' => [],'X' => [],'Y' => [],'Z' => [] 
]} 
{foreach $brands as $b} 
    {$name = $b.name} 
    {$char = $name|truncate:1:""|upper} 
    {$chars[$char][] = $b} 
{/foreach} 
{foreach $chars as $key => $data} 
    {if $data} 
    <strong>{$key}</strong><hr/> 
    {foreach $data as $b} 
        <span class="brand"> 
        <a href="{$b.url}">{if $b.image}<img src="{$wa_url}wa-data/public/shop/brands/{$b.id}/{$b.id}{$b.image}">{else}{$b.name}{/if}</a> 
        {$b.summary} 
        </span> 
    {/foreach} 
    <hr/> 
    {/if} 
{/foreach}
Теги: smarty, webasyst