dt614037527
dt614037527
2012-09-25 00:17

修改smarty中的数组内容?

已采纳

We are using smarty,

In one of our views we have:

{if $header.showNav}...{/if}

and if we want to set that we use:

$header = $smarty->getTemplateVars('header');
$header['showNav'] = true;
$smarty->assign('header', $header);

Which works, But it seems rather long winded.

Does smarty provide a way to manage this easier?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dph23577 dph23577 9年前

    Why not just roll your own function:

    function updateSmarty($smarty, $templateVar, $key, $value) {
        $templateVars = $smarty->getTemplateVars($templateVar);
        $templateVars[$key] = $value;
        $smarty->assign($templateVar, $templateVars);
    }
    

    Then you can one line it where you need it:

    updateSmarty($smarty, 'header', 'showNav', true);
    

    Or even:

    function showNav($smarty) {
        updateSmarty($smarty, 'header', 'showNav', true);
    }
    

    Then:

    showNav($smarty);
    
    点赞 评论 复制链接分享

为你推荐