smarty include_php 异常

smarty include_php exception

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

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

Fatal error: Uncaught --> Smarty Compiler: Syntax error in template "file:/home/user/www/site/template/template.tpl" on line 456 "{include_php "$plugin/button.php"}" {include_php} file '/button.php' is not readable <-- thrown in /home/user/www/site/smarty/sysplugins/smarty_internal_templatecompilerbase.php on line 456

有什么方法可以检查 php 文件是否存在?

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

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

如果您首先确保变量已设置,然后如果是,则检查文件是否存在,它应该会按预期工作。

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

在纯 PHP 中,这应该可以工作,因为如果 isset 失败,它甚至不会尝试 file_exist。
不过不确定是否聪明。
如果这种方式不起作用,您需要将其作为单独的 if.