Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top