【问题标题】:Integrate PHP code in Smarty templates在 Smarty 模板中集成 PHP 代码
【发布时间】:2011-01-09 13:30:31
【问题描述】:

我使用 Smarty 来实现我的模板。这个PHP语句怎么写。

<?php if(empty($_GET['action'])) { echo class="current"; } ?>

【问题讨论】:

    标签: php smarty template-engine


    【解决方案1】:

    发明模板引擎是为了将业务逻辑(以及请求本身)与表示逻辑分开。模板引擎不应该处理 $_GET、$_POST、$_SESSION 等。

    如果您需要根据 $_GET['action'] 做出决定 - 只需使用调节器 smarty 的 assign 从控制器传递它。

    无论如何,如果您不希望您的代码更符合逻辑和更明显,您可以使用$smarty.get.action

    {if empty($smarty.get.action)}class="current"{/if}
    

    【讨论】:

    • 谢谢!我想给$_GET['action'] = 'detail' 赋值 我写了:{if ($smarty.get.action='detail') class="current"{/if} 但我得到一个错误
    • @Mini: {if ($smarty.get.action eq 'detail') class="current"{/if}
    • @zerkms:谢谢我使用{if ($smarty.get.action=='details')} class="current"{/if} 什么是最好的 == 或 eq ?谢谢
    • @Mini:它们是同义词。我以前用eq
    • @zerkms 谢谢 :) 在那里我可以找到所有函数和参数。
    【解决方案2】:

    像这样:

    {if !isset($smarty.get.action)}class="current"{/if}
    

    【讨论】:

    • 谢谢!我想用if($_GET['action'] == 'open') {} 有手册或文档吗?
    • 是的。以下是一些有用的文档:smarty.net/docsv2/en/language.function.if.tplsmarty.net/docsv2/en/…
    • 哇,这么简单。我刚刚找到了正确的代码{if ($smarty.get.action=='details')} class="current"{/if} :)
    • 完美!虽然如果您想在键入时保存两个字符,则不需要括号:D
    猜你喜欢
    • 2011-01-17
    • 2017-11-11
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    相关资源
    最近更新 更多