当前位置: 动力学知识库 > 问答 > 编程问答 >

smarty3 - Smarty check if is array to fix error cannot use string offset as an array

问题描述:

Depending on the amount of crumbs I have either:

[trail] => Array

(

[crumb] => Array

(

[url] => /app/system

[label] => System

)

[trail] => Array

(

[crumb] => Array

(

[0] => Array

(

[url] => /app/system

[label] => Sytem

)

[1] => Array

(

[url] => /app/system/edit

[label] => System Edit

)

)

)

In smarty I have:

{if $trail.crumb}

<ul class="breadcrumbs">

{foreach $trail.crumb as $crum}

<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb">

<a href="{$crum.url}" itemprop="url" title="{$crum.label}"><span itemprop="title">{$crum.label|truncate:20}</span></a>

</li>

{/foreach}

</ul>

{/if}

In the first example this shows:

Fatal error: Cannot use string offset as an array

Is there a way to do this without duplicating the HTML?

网友答案:

Figured it out, would not of been able to do this without smarty3.

    {if isset($trail.crumb.0)}
        {assign var="crumbs" value=$trail.crumb}
    {else}
        {assign var="crumbs" value=[$trail.crumb]}
    {/if}
    {foreach $crumbs as $crum}

Smarty 3 is amazing, they fixed everything that was missing from Smarty 2.

分享给朋友:
您可能感兴趣的文章:
随机阅读: