0

我关注了分配给 smarty 模板的数组标题 $value。

 Array
        (
            [id] => 175
            [start_date] => 2014-07-01
            [end_date] => 2014-07-31
            [count] => 3
            [rebate_type_id] => 2
            [created_at] => 1406031299
            [updated_at] => 1406031299
            [applicable_states] => Array
                (
                    [0] => Array
                        (
                            [state_id] => 0
                            [rebate_id] => 175
                            [state_name] => All States
                        )

                )
            [manufacturer_id] => 49
            [company_name] => Blue Nun
            [manufacturer_image_path] => 
        )

为了访问内部数组元素,我编写了以下代码,但它不起作用。你能纠正我在下面的代码中犯的错误吗?

{if $value.appplicable_states.0.state_name == 'All States'}
                  All States
                {/if}
4

2 回答 2

2

在 PHP 中你做了这样的分配(我已经简化了数组):

$data= array('applicable_states' => array(0 => array('state_name' => 'All States')));
$smarty->assign('value',$data);

在 Smarty 中,您可以使用:

{if $value.applicable_states.0.state_name =='All States'}
All States
{else}
Not All States
{/if}

输出是:

所有国家

于 2014-07-22T13:32:49.780 回答
0

自从我聪明以来已经有一段时间了,但我认为你需要先将数组分配给一个变量才能访问数组的元素:

{assign var=states value=$value.appplicable_states}
{$states[0].state_name}

或更可能:

{assign var=states value=$value.appplicable_states}
{foreach from=$states item=state}
    {$state.state_name}
{/foreach}
于 2014-07-22T13:29:08.883 回答