【问题标题】:Smarty header location PHPSmarty 标头位置 PHP
【发布时间】:2015-06-07 04:48:19
【问题描述】:

一周以来,我一直在使用 Smarty 模板引擎,我发现它非常酷,因为它的应用程序逻辑和设计分离。我现在的问题是如何使用 smarty 做一个header('Location: somepage.php');?我知道我可以在我的 php 页面中执行此操作,但我在我的 tpl 文件中执行此操作。这就是我的代码的样子..

<div class="row">
   <div class="large-4 columns">
{if isset($smarty.get.success) or !empty($smarty.get.success)}
{if ($smarty.get.success eq '')}
<div data-alert class="alert-box warning radius">
  Warning: No value found.
  <a href="#" class="close">&times;</a>
</div>
{else}
      <div data-alert class="alert-box success radius">
      {if ($smarty.get.success eq 'updatecontact')} 
      Contact successfully updated.
      {elseif ($smarty.get.success eq 'addcontact')} 
      Contact successfully added.
      {else}
      <?php header('Location: somepage.php'); ?>
      {/if}
      <a href="#" class="close">&times;</a>
      </div>
{/if}
{/if}
   </div>
</div

我使用 zurb-foundation 作为我的 css 框架。如您所见,我正在使用&lt;?php header('Location: somepage.php'); ?&gt;,这不适合smarty。有没有办法在 smarty 中做到这一点?

【问题讨论】:

  • 我认为将header重定向放在业务逻辑类中而不是视图是一个好习惯。
  • 我不明白你为什么要在 tpl 文件中这样做。不仅破坏了您喜欢的酷逻辑和设计分离,而且是不好的做法,并且大多数时候可能会产生“标头已发送”错误。在处理模板之前在php中检查$_GET['success']有什么问题?

标签: php smarty smarty3


【解决方案1】:

将代码附在{php}中:

{php} header ('Location: somepage.php'); {/php}

【讨论】:

  • 我也在使用这个.. 但如果可能的话,我实际上不想在我的 php 页面中使用 php 标签:D
  • 这不是 。这是 smarty 自己的 php 标签。你必须让 smarty 知道你打算让它运行 PHP 代码,对吧?在这里阅读更多:smarty.net/docsv2/en/language.function.php.tpl
  • 是的先生,我已经通过使用 new SmartyBC();而不是 new Smarty();只要。无论如何,谢谢。
【解决方案2】:

用 javascript 来做:

<script>
window.location = 'somepage.php';
</script>

【讨论】:

    猜你喜欢
    • 2013-09-15
    • 2014-08-25
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多