【问题标题】:smarty include_php exceptionsmarty include_php 异常
【发布时间】:2018-07-01 18:55:00
【问题描述】:

我想在我的模板中包含一个 php 文件。但有时 php 文件不存在。 我尝试通过函数“file_exists”检查它,但它不起作用。

{if file_exists("`$plugin`/button.php")}
    {include_php "`$plugin`/button.php"}
{/if}

致命错误:未捕获 --> Smarty 编译器:模板中的语法错误 第 456 行的“文件:/home/user/www/site/template/template.tpl” "{include_php "$plugin/button.php"}" {include_php} 文件 '/button.php' 不可读

有没有办法检查php文件是否存在?

【问题讨论】:

  • 这是什么:`$plugin`/button.php 真的是文件路径的名称吗?
  • @Andreas 我在 $plugin 变量中使用文件夹名称,它在文件路径中工作正常
  • it works ok in file path 但在问题中你写了but it doesn't work.。是哪个?
  • @Andreas 如果文件存在,它运行良好:) 但我想检查文件是否存在,因为如果文件不存在则会出现致命错误
  • 确定不是 $plugin 变量为空或未设置导致错误?

标签: php smarty


【解决方案1】:

在尝试使用变量之前,请务必检查它们。

在这种情况下,是 $plugin 变量为空导致字符串连接失败。

如果您首先确保您的变量已设置,那么如果这是真的,请检查文件是否存在,它应该可以按预期工作。

{if isset($plugin) && file_exists("`$plugin`/button.php")}
    {include_php "$plugin/button.php"}
{/if}

在纯 PHP 中这应该可以工作,因为如果 isset 失败,它甚至不会尝试 file_exist。
虽然不确定 smarty。
如果它不能以这种方式工作,则需要将其单独设置为 if。

【讨论】:

  • 很遗憾,它在 Smarty 中不起作用,但感谢您的帮助。我已经编辑了代码,现在我不再需要 include_php 了。此外,include_php 将在几个下一个版本中被删除。
猜你喜欢
  • 2013-11-17
  • 2018-05-02
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 2021-12-05
相关资源
最近更新 更多