【问题标题】:Smarty code for If/Then statement based on URL value基于 URL 值的 If/Then 语句的 Smarty 代码
【发布时间】:2015-01-31 18:08:50
【问题描述】:

网址如下:myweb.com/home/8-sticker-papdie.html

我使用:

{if $smarty.server.SCRIPT_URI|strstr:"home"}
Display message "YES"
{else}
None
{/if}

当我运行它时,我只能看到“无”。

我做错了什么?

【问题讨论】:

  • 这对我有用,以防其他人有同样的问题: {if strstr($smarty.server.REQUEST_URI, 'home') !== false} 显示消息“YES” {else} None {/if}
  • 正如您可能已经发现的那样,SCRIPT_URI 变量不再受 PHP 支持(在 docs 中没有它的踪迹)并且不能在某些范围的服务器或主机上工作——看herehere.

标签: php smarty


【解决方案1】:

你可以只使用$_SERVER['HTTP_HOST']的值:

{if strstr($_SERVER['HTTP_HOST'], 'home') !== false}
    Display message "YES"
{else}
    None
{/if}

【讨论】:

  • 我试过这个......不走运,我看到“无”。也试过 {if strstr($_SERVER['HTTP_HOST'], '/home/') !== false} 也没有结果
  • 尝试改用$_SERVER['REQUEST_URI']
  • 不工作。我正在使用 prestashop 1.6。 & 如果这些信息有帮助,我会在 footer.tpl 中测试这段代码。
  • 感谢您的尝试。我找到了解决方案。不知道它是否仅适用于 PRESTASHOP 或任何 smarty,但 $smarty.server.REQUEST_URI 成功了
  • {if strstr($smarty.server.REQUEST_URI, 'home') !== false} 显示消息“YES” {else} None {/if}
【解决方案2】:

如果您需要网址/域名

$_SERVER['HTTP_HOST']

【讨论】:

    【解决方案3】:

    试试这个对你有帮助:

    {if $smarty.server.REQUEST_URI|strstr:"home"}
    Display message "YES"
    {else}
    None
    {/if}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      相关资源
      最近更新 更多