如何检查迭代得到名字?

时间:2016-08-01 15:23:20

标签: php foreach smarty

如何检查foreach项目的名称并显示它?

我当前的代码是,检查项目是偶数还是奇数:

{foreach $panels as $item}
{if $item@iteration is odd}
{outputHomePanels}
{/if}
{/foreach}

整个代码:

    {function name=outputHomePanels}
                <div menuItemName="{$item->getName()}" class="panel panel-default panel-accent-{$item->getExtra('color')}{if $item->getClass()} {$item->getClass()}{/if} {$item->getName()}"{if $item->getAttribute('id')} id="{$item->getAttribute('id')}"{/if}>
                    <div class="panel-heading">
                        <h3 class="panel-title">
                            {if $item->getExtra('btn-link') && $item->getExtra('btn-text')}
                                <div class="pull-right">
                                    <a href="{$item->getExtra('btn-link')}" class="more-link">
                                        {$item->getExtra('btn-text')}
                                    </a>
                                </div>
                            {/if}
                            <div class="clientarea-icon {$item->getName()}">
                            {if $item->hasIcon()}<i class="{$item->getIcon()}"></i>{/if}
                            </div>
                            <div class="clientarea-title">
                            {$item->getLabel()}
                            </div>
                            {if $item->hasBadge()}<span class="badge">{$item->getBadge()}</span>{/if}
                        </h3>
                    </div>
                    {if $item->hasBodyHtml()}
                        <div class="panel-body">
                            {$item->getBodyHtml()}
                        </div>
                    {/if}
                    {if $item->hasChildren()}
                        <div class="list-group{if $item->getChildrenAttribute('class')} {$item->getChildrenAttribute('class')}{/if}">
                            {foreach $item->getChildren() as $childItem}
                                {if $childItem->getUri()}
                                    <a menuItemName="{$childItem->getName()}" href="{$childItem->getUri()}" class="list-group-item{if $childItem->getClass()} {$childItem->getClass()}{/if}{if $childItem->isCurrent()} active{/if}"{if $childItem->getAttribute('dataToggleTab')} data-toggle="tab"{/if}{if $childItem->getAttribute('target')} target="{$childItem->getAttribute('target')}"{/if} id="{$childItem->getId()}">
                                        <div class="clientarea-icon {$childItem->getLabel()|stristr:'<' : true}">
                                            <i class="icon-clientarea-product"></i>
                                        </div>
                                        <div class="clientarea-title">
                                            {assign var="splitItem" value=" - "|explode:$childItem->getLabel()}
{$splitItem[1]}
                                        </div>
                                        {if $childItem->hasBadge()}&nbsp;<span class="badge">{$childItem->getBadge()}</span>{/if}
                                    </a>
                                {else}
                                    <div menuItemName="{$childItem->getName()}" class="list-group-item{if $childItem->getClass()} {$childItem->getClass()}{/if}" id="{$childItem->getId()}">
                                        {if $childItem->hasIcon()}<i class="{$childItem->getIcon()}"></i>&nbsp;{/if}
                                        {$childItem->getLabel()}
                                        {if $childItem->hasBadge()}&nbsp;<span class="badge">{$childItem->getBadge()}</span>{/if}
                                    </div>
                                {/if}
                            {/foreach}
                        </div>
                    {/if}
                </div>
            {/function}

我需要检查项目的名称是否为“活动产品/服务”。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

if ( $item == "Active Products/Services" ) {

  // code here

}

答案 1 :(得分:0)

这看起来像Smarty代码,而不是PHP代码。你的语义也有些令人困惑。如果你的意思是如何检查数组键值的匹配,那么......

{foreach $panels as $name=>$item}
  {if ( "Active Products/Services" == $name ) }
     ...
  {/if}
  {if $item@iteration is odd}
    {outputHomePanels}
  {/if}
{/foreach}

OTOH,如果您只想“outputHomePanels”,其名称为“Active Products / Services”,那么...

{ if ($name = isset($item['Active Products/Services']) ? $item['Active Products/Services'] : false ) }
    {outputHomePanels}
{/if}

...但你不应该在smarty中实现应用程序逻辑 - 这应该是在PHP中。